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

Scikit-Learn · Function

mean_absolute_error

Computes mean absolute error, a non-negative regression loss based on absolute differences between true and predicted targets. It can weight samples and either preserve or aggregate errors across multiple outputs.

How it works

The function compares each predicted target with its corresponding true target, takes the absolute error, and averages it. For multiple outputs, it can return one error per output or aggregate outputs uniformly or with supplied weights; sample weights can also affect the averaging.

Syntax

sklearn.metrics.mean_absolute_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 or NoneSample weights; array-like with shape (n_samples,), default None.
multioutput{'raw_values', 'uniform_average'} or array-likeOutput aggregation mode: 'raw_values', 'uniform_average', or an array-like of output weights; default 'uniform_average'.

Returns

float or array of floats. raw_values returns one error per output; uniform aggregation or an output-weight array returns a weighted average. The result is non-negative, with 0.0 as the best value.

Key points

  • This function is a regression loss based on absolute prediction errors.
  • Scores cannot be negative, and lower values indicate better predictions; zero is optimal.
  • Pass the reference targets as y_true.
  • Pass the model's predictions as y_pred.
  • The multioutput argument controls how errors from several target columns are combined.

Common mistakes

  • Use raw_values when you need an error for every output instead of one aggregate score.
  • An array supplied to multioutput must represent output-combination weights, not per-sample weights.

Example: Compute a regression error

from sklearn.metrics import mean_absolute_error

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

mae = mean_absolute_error(y_true, y_pred)
print(mae)  # 0.5

This computes the scalar MAE for four regression predictions.

When to use it

Regression evaluation

Compare regression predictions with observed continuous targets.

Absolute-error reporting

Measure error when absolute deviations are easier to interpret than squared deviations.

Multioutput scoring

Evaluate models that predict several target columns.

Weighted evaluation

Give selected observations more influence in the metric.

Interview questions

QWhat does mean_absolute_error measure?

It is a regression loss that returns a non-negative floating-point value. A perfect prediction has a score of 0.0.

Follow-up: How can multiple outputs be handled? Use raw_values for separate output errors, or use uniform aggregation or an output-weight array for one combined result.

QWhat are the main input arguments?

y_true contains the correct target values and y_pred contains estimated target values. Both may be one-dimensional or shaped for multiple outputs.

Follow-up: How would sample-level importance be represented? Pass an array of sample weights through sample_weight.

QHow does the multioutput argument change the return value?

With multioutput='raw_values', the function returns an error for each output. With the default uniform aggregation or an output-weight array, it returns one weighted average.

Follow-up: What does sample_weight control? It supplies weights used when averaging the sample errors.

  • mean_squared_error
  • mean_absolute_percentage_error
  • median_absolute_error
  • root_mean_squared_error

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