Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Regression Metrics

API Reference

Signatures

sp.mean_squared_error(y_true, y_pred)                       -> float
sp.root_mean_squared_error(y_true, y_pred)                  -> float
sp.mean_absolute_error(y_true, y_pred)                      -> float
sp.median_absolute_error(y_true, y_pred)                    -> float
sp.r2_score(y_true, y_pred)                                 -> float
sp.explained_variance_score(y_true, y_pred)                 -> float
sp.max_error(y_true, y_pred)                                -> float
sp.mean_absolute_percentage_error(y_true, y_pred)           -> float
sp.mean_squared_log_error(y_true, y_pred)                   -> float
sp.root_mean_squared_log_error(y_true, y_pred)              -> float
sp.mean_pinball_loss(y_true, y_pred, alpha=0.5)             -> float
sp.d2_absolute_error_score(y_true, y_pred)                  -> float

Function summary

FunctionOutputDescription
mean_squared_errorfloatAverage squared error
root_mean_squared_errorfloat$\sqrt{\text{MSE}}$, in target units
mean_absolute_errorfloatAverage absolute error
median_absolute_errorfloatMedian of $
r2_scorefloatCoefficient of determination
explained_variance_scorefloatVariance ratio (allows bias)
max_errorfloatWorst residual
mean_absolute_percentage_errorfloatMAPE, scale-free
mean_squared_log_errorfloatMSE in log space, requires $y, \hat{y} \geq 0$
root_mean_squared_log_errorfloat$\sqrt{\text{MSLE}}$
mean_pinball_lossfloatQuantile loss (param alpha in $(0,1)$)
d2_absolute_error_scorefloat$R^2$ analogue using MAE
Example
import seraplot as sp

y_true = [3.0, -0.5, 2.0, 7.0, 5.0, 4.5]
y_pred = [2.5, 0.0, 2.1, 7.8, 4.7, 4.6]

print("MSE   :", sp.mean_squared_error(y_true, y_pred))
print("RMSE  :", sp.root_mean_squared_error(y_true, y_pred))
print("MAE   :", sp.mean_absolute_error(y_true, y_pred))
print("MedAE :", sp.median_absolute_error(y_true, y_pred))
print("R²    :", sp.r2_score(y_true, y_pred))
print("EVS   :", sp.explained_variance_score(y_true, y_pred))
print("MaxE  :", sp.max_error(y_true, y_pred))
print("MAPE  :", sp.mean_absolute_percentage_error(y_true, y_pred))
print("MSLE  :", sp.mean_squared_log_error([1,2,3], [1.1,2.1,3.1]))
print("Q90   :", sp.mean_pinball_loss(y_true, y_pred, alpha=0.9))
print("D²-AE :", sp.d2_absolute_error_score(y_true, y_pred))

Algorithmic Functioning

MSE / RMSE / MAE — pointwise error aggregates:

$$\text{MSE} = \frac{1}{n}\sum_i (y_i - \hat{y}_i)^2 \qquad \text{MAE} = \frac{1}{n}\sum_i |y_i - \hat{y}_i|$$

Median absolute error — robust to outliers:

$$\text{MedAE} = \mathrm{median}_i \, |y_i - \hat{y}_i|$$

MAPE — scale-free, undefined when $y_i = 0$:

$$\text{MAPE} = \frac{1}{n}\sum_i \left|\frac{y_i - \hat{y}_i}{y_i}\right|$$

MSLE — penalises under-prediction more than over-prediction; requires non-negative values:

$$\text{MSLE} = \frac{1}{n}\sum_i \big(\log(1+y_i) - \log(1+\hat{y}_i)\big)^2$$

Pinball loss — asymmetric quantile loss; minimised by the $\alpha$-quantile predictor:

$$L_\alpha = \frac{1}{n}\sum_i \big(\alpha \max(y_i - \hat{y}_i, 0) + (1-\alpha)\max(\hat{y}_i - y_i, 0)\big)$$

Explained variance allows for a constant bias:

$$\text{EVS} = 1 - \frac{\mathrm{Var}(y - \hat{y})}{\mathrm{Var}(y)}$$

$R^2$ vs. $D^2$-AE — both are "1 minus loss / loss-of-the-mean-predictor", but using MSE for $R^2$ and MAE for $D^2$-AE:

$$R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}, \qquad D^2_{\text{AE}} = 1 - \frac{\sum_i |y_i - \hat{y}_i|}{\sum_i |y_i - \tilde{y}|}$$

with $\tilde{y}$ the median.

Référence API

Signatures

sp.mean_squared_error(y_true, y_pred)                       -> float
sp.root_mean_squared_error(y_true, y_pred)                  -> float
sp.mean_absolute_error(y_true, y_pred)                      -> float
sp.median_absolute_error(y_true, y_pred)                    -> float
sp.r2_score(y_true, y_pred)                                 -> float
sp.explained_variance_score(y_true, y_pred)                 -> float
sp.max_error(y_true, y_pred)                                -> float
sp.mean_absolute_percentage_error(y_true, y_pred)           -> float
sp.mean_squared_log_error(y_true, y_pred)                   -> float
sp.root_mean_squared_log_error(y_true, y_pred)              -> float
sp.mean_pinball_loss(y_true, y_pred, alpha=0.5)             -> float
sp.d2_absolute_error_score(y_true, y_pred)                  -> float

Résumé

FonctionSortieDescription
mean_squared_errorfloatErreur quadratique moyenne
root_mean_squared_errorfloat$\sqrt{\text{MSE}}$, dans l'unité cible
mean_absolute_errorfloatErreur absolue moyenne
median_absolute_errorfloatMédiane de $
r2_scorefloatCoefficient de détermination
explained_variance_scorefloatRatio de variance (autorise un biais)
max_errorfloatPire résidu
mean_absolute_percentage_errorfloatMAPE, sans échelle
mean_squared_log_errorfloatMSE en espace log, requiert $y, \hat{y} \geq 0$
root_mean_squared_log_errorfloat$\sqrt{\text{MSLE}}$
mean_pinball_lossfloatPerte quantile (paramètre alpha dans $(0,1)$)
d2_absolute_error_scorefloatAnalogue de $R^2$ avec MAE
Exemple
import seraplot as sp

y_true = [3.0, -0.5, 2.0, 7.0, 5.0, 4.5]
y_pred = [2.5, 0.0, 2.1, 7.8, 4.7, 4.6]

print("MSE   :", sp.mean_squared_error(y_true, y_pred))
print("RMSE  :", sp.root_mean_squared_error(y_true, y_pred))
print("MAE   :", sp.mean_absolute_error(y_true, y_pred))
print("MedAE :", sp.median_absolute_error(y_true, y_pred))
print("R²    :", sp.r2_score(y_true, y_pred))
print("EVS   :", sp.explained_variance_score(y_true, y_pred))
print("MaxE  :", sp.max_error(y_true, y_pred))
print("MAPE  :", sp.mean_absolute_percentage_error(y_true, y_pred))
print("MSLE  :", sp.mean_squared_log_error([1,2,3], [1.1,2.1,3.1]))
print("Q90   :", sp.mean_pinball_loss(y_true, y_pred, alpha=0.9))
print("D²-AE :", sp.d2_absolute_error_score(y_true, y_pred))

Fonctionnement algorithmique

MSE / RMSE / MAE — agrégats d'erreur point par point :

$$\text{MSE} = \frac{1}{n}\sum_i (y_i - \hat{y}_i)^2 \qquad \text{MAE} = \frac{1}{n}\sum_i |y_i - \hat{y}_i|$$

Erreur absolue médiane — robuste aux outliers :

$$\text{MedAE} = \mathrm{median}_i \, |y_i - \hat{y}_i|$$

MAPE — sans échelle, indéfini quand $y_i = 0$ :

$$\text{MAPE} = \frac{1}{n}\sum_i \left|\frac{y_i - \hat{y}_i}{y_i}\right|$$

MSLE — pénalise davantage la sous-estimation que la sur-estimation ; requiert des valeurs positives :

$$\text{MSLE} = \frac{1}{n}\sum_i \big(\log(1+y_i) - \log(1+\hat{y}_i)\big)^2$$

Pinball loss — perte quantile asymétrique, minimisée par le prédicteur $\alpha$-quantile :

$$L_\alpha = \frac{1}{n}\sum_i \big(\alpha \max(y_i - \hat{y}_i, 0) + (1-\alpha)\max(\hat{y}_i - y_i, 0)\big)$$

Variance expliquée autorise un biais constant :

$$\text{EVS} = 1 - \frac{\mathrm{Var}(y - \hat{y})}{\mathrm{Var}(y)}$$

$R^2$ vs. $D^2$-AE — tous deux « 1 moins perte / perte du prédicteur moyen », mais utilisant MSE pour $R^2$ et MAE pour $D^2$-AE :

$$R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}, \qquad D^2_{\text{AE}} = 1 - \frac{\sum_i |y_i - \hat{y}_i|}{\sum_i |y_i - \tilde{y}|}$$

avec $\tilde{y}$ la médiane.