Skip to content

Metrics

siapy.models.metrics

ClassificationMetrics

Bases: NamedTuple

accuracy instance-attribute

accuracy: float

precision instance-attribute

precision: float

recall instance-attribute

recall: float

f1 instance-attribute

f1: float

to_dict

to_dict() -> dict
Source code in siapy/models/metrics.py
57
58
def to_dict(self) -> dict:
    return self._asdict()

RegressionMetrics

Bases: NamedTuple

mae instance-attribute

mae: float

mse instance-attribute

mse: float

rmse instance-attribute

rmse: float

r2 instance-attribute

r2: float

pe instance-attribute

pe: float

maxe instance-attribute

maxe: float

nrmse_mean instance-attribute

nrmse_mean: float

nrmse_range instance-attribute

nrmse_range: float

to_dict

to_dict() -> dict
Source code in siapy/models/metrics.py
83
84
def to_dict(self) -> dict:
    return self._asdict()

normalized_RMSE

normalized_RMSE(
    y_true: ndarray,
    y_pred: ndarray,
    normalize_by: Literal["range", "mean"] = "range",
)
Source code in siapy/models/metrics.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def normalized_RMSE(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    normalize_by: Literal["range", "mean"] = "range",
):
    rmse = mean_squared_error(y_true, y_pred, squared=False)
    if normalize_by == "range":
        normalizer = np.max(y_true) - np.min(y_true)
    elif normalize_by == "mean":
        normalizer = np.mean(y_true)
    else:
        raise InvalidInputError(
            input_value=normalize_by,
            message="Unknown normalizer. Possible values are: 'range' or 'mean'.",
        )
    return rmse / normalizer

calculate_classification_metrics

calculate_classification_metrics(
    y_true,
    y_pred,
    average: Literal[
        "micro", "macro", "samples", "weighted", "binary"
    ]
    | None = "weighted",
)
Source code in siapy/models/metrics.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def calculate_classification_metrics(
    y_true,
    y_pred,
    average: Literal["micro", "macro", "samples", "weighted", "binary"] | None = "weighted",
):
    accuracy = accuracy_score(y_true, y_pred)
    precision = precision_score(y_true, y_pred, average=average)
    recall = recall_score(y_true, y_pred, average=average)
    f1 = f1_score(y_true, y_pred, average=average)
    return ClassificationMetrics(
        accuracy=accuracy,
        precision=precision,
        recall=recall,
        f1=f1,
    )

calculate_regression_metrics

calculate_regression_metrics(y_true, y_pred)
Source code in siapy/models/metrics.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def calculate_regression_metrics(y_true, y_pred):
    mae = mean_absolute_error(y_true, y_pred)
    mse = mean_squared_error(y_true, y_pred, squared=True)
    rmse = mean_squared_error(y_true, y_pred, squared=False)
    r2 = r2_score(y_true, y_pred)
    pe = mean_absolute_percentage_error(y_true, y_pred)
    maxe = max_error(y_true, y_pred)
    nrmse_mean = normalized_RMSE(y_true, y_pred, normalize_by="mean")
    nrmse_range = normalized_RMSE(y_true, y_pred, normalize_by="range")
    return RegressionMetrics(
        mae=mae,
        mse=mse,
        rmse=rmse,
        r2=r2,
        pe=pe,
        maxe=maxe,
        nrmse_mean=nrmse_mean,
        nrmse_range=nrmse_range,
    )