Mean squared error (MSE)#

  • Corresponde al valor esperado del error o pérdida cuadrática.

  • Se computa como:

    \text{MSE}(y, \hat{y}) = \frac{1}{n_{\text{samples}}} \sum_{i=0}^{n_{\text{samples}} - 1} (y_i - \hat{y}_i)^2 = \frac{1}{n_{\text{samples}}} \sum_{i=0}^{n_{\text{samples}} - 1} \hat{e}_i^2

[1]:
from sklearn.metrics import mean_squared_error

y_true = [3.0, -0.5, 2, 7]
y_pred = [2.5, +0.0, 2, 8]

#
# 1/4 * ((3.0-2.5)**2 + (-0.5-0)**2 + (2-2)**2 + (7-8)**2) = 0.375
#
mean_squared_error(
    # -------------------------------------------------------------------------
    # Ground truth (correct) target values.
    y_true=y_true,
    # -------------------------------------------------------------------------
    # Estimated target values.
    y_pred=y_pred,
    # -------------------------------------------------------------------------
    # Sample weights.
    sample_weight=None,
    # -------------------------------------------------------------------------
    # Defines aggregating of multiple output scores.
    # * 'raw_values': Returns a full set of scores in case of multioutput input.
    # * 'uniform_average': Scores of all outputs are averaged with uniform
    #      weight.
    multioutput="uniform_average",
    # -------------------------------------------------------------------------
    # If True returns MSE value, if False returns RMSE value.
    squared=True,
)
[1]:
0.375
[2]:
#
# 0.612372 ^2 = 0.375
#
mean_squared_error(
    y_true=y_true,
    y_pred=y_pred,
    squared=False,
)
[2]:
0.6123724356957945
[3]:
y_true = [[0.5, 1], [-1, 1], [7, -6]]
y_pred = [[0.0, 2], [-1, 2], [8, -5]]

# 1/3 * ((0.5-0)**2 + (-1-(-1))**2 + (7-8)**2) = 0.41666667
# 1/3 * ((1-2)**2 + (1-2)**2+(-5-(-6)**2)) = 1.0
mean_squared_error(
    y_true,
    y_pred,
    multioutput="raw_values",
)
[3]:
array([0.41666667, 1.        ])
[4]:
#
# 0.5 * (0.41666667 + 1.0) = 0.708333
#
mean_squared_error(
    y_true,
    y_pred,
)
[4]:
0.7083333333333334
[5]:
#
# 0.5 * (sqrt(0.41666667) + 1.0) = 0.822748
#
mean_squared_error(
    y_true,
    y_pred,
    squared=False,
)
[5]:
0.8227486121839513
[6]:
#
# 0.3 * 0.41666667 + 0.7 * 1.0 = 0.825
#
mean_squared_error(
    y_true,
    y_pred,
    multioutput=[0.3, 0.7],
)
[6]:
0.825