Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

mean_squared_error

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.

How it works

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.

Syntax

sklearn.metrics.mean_squared_error(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average')

Parameters

NameTypeDescription
y_truearray-likeGround-truth target values; array-like with shape (n_samples,) or (n_samples, n_outputs).
y_predarray-likeEstimated target values; array-like with shape (n_samples,) or (n_samples, n_outputs).
sample_weightarray-like, default=NoneOptional 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.

Returns

A non-negative float, or an array of floats with one value for each individual target.

Key points

  • This function is a regression loss based on squared prediction errors.
  • y_true supplies the correct target values used for comparison.
  • y_pred supplies the model's estimated target values.
  • multioutput determines how errors from several target columns are combined.
  • The default aggregation gives every output equal influence.

Common mistakes

  • The metric cannot be negative; a value of zero indicates the best possible result.
  • When providing multiple output weights, the array must define how output errors are combined.

Example: Compute mean squared error

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.375

Computes the regression loss for the true and predicted targets.

When to use it

Regression evaluation

Evaluating a single-output regression model with one scalar loss.

Multi-output diagnostics

Inspecting errors independently for several target columns.

Weighted assessment

Giving selected observations greater influence during evaluation.

Interview questions

QWhat does mean_squared_error measure?

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.

QWhat shapes do the target arguments support?

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.

QHow does multioutput affect the return value?

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_error
  • mean_absolute_error
  • r2_score

Source: mean_squared_error — scikit-learn 1.9.1 documentation. Checked 2026-10-15.