Regression evaluation
Evaluating a single-output regression model with one scalar loss.
Scikit-Learn · Function
Computes mean squared error, a regression loss that summarizes squared differences between true and predicted targets. It returns one aggregate value by default or per-output values when requested.
The function compares each predicted target with its corresponding true target, squares the differences, and averages them. For multiple outputs, it can either return one error per output or combine output errors using uniform or user-supplied weights; sample weights can also influence the calculation.
sklearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average')| Name | Type | Description |
|---|---|---|
y_true | array-like | Ground-truth target values; array-like with shape (n_samples,) or (n_samples, n_outputs). |
y_pred | array-like | Estimated target values; array-like with shape (n_samples,) or (n_samples, n_outputs). |
sample_weight | array-like, default=None | Optional sample weights; array-like with shape (n_samples,). |
multioutput | {'raw_values', 'uniform_average'} or array-like, default='uniform_average' | Output aggregation mode: 'raw_values', 'uniform_average', or an array-like of shape (n_outputs,) containing output weights. |
A non-negative float, or an array of floats with one value for each individual target.
from sklearn.metrics import mean_squared_error
y_true = [3, -0.5, 2, 7]
y_pred = [2.5, 0.0, 2, 8]
print(mean_squared_error(y_true, y_pred)) # 0.375Computes the regression loss for the true and predicted targets.
Evaluating a single-output regression model with one scalar loss.
Inspecting errors independently for several target columns.
Giving selected observations greater influence during evaluation.
It computes a regression loss by averaging squared differences between corresponding true and predicted values. Lower values are better, and the best value is 0.0.
Follow-up: How can multiple outputs be handled? Use multioutput='raw_values' for one error per target, or aggregate them with uniform or custom output weights.
y_true and y_pred contain the ground-truth and estimated target values. Each can have shape (n_samples,) for one output or (n_samples, n_outputs) for multiple outputs.
Follow-up: What additional weighting is available? sample_weight assigns weights to individual samples, while multioutput controls aggregation across targets.
The default multioutput='uniform_average' averages output errors equally. 'raw_values' returns an array containing the error for each output, and an array-like value supplies custom output weights.
Follow-up: When would raw_values be preferable? It is useful when each target's error must be inspected independently rather than reduced to one scalar.
root_mean_squared_errormean_absolute_errorr2_score