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

Scikit-Learn · Function

r2_score

Computes the coefficient of determination for regression predictions. A score of 1.0 is best; scores can be negative, and multi-output results can be aggregated or returned separately.

Since
1.1

How it works

The function compares prediction residuals with the variation of the true targets around their average. It returns a single score by default, averages multiple-output scores uniformly by default, and can preserve separate output scores or apply output weights. Constant targets require special handling because their ordinary score is not finite.

Syntax

r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True)

Parameters

NameTypeDescription
y_truearray-likeGround-truth target values, shaped as n_samples or n_samples by n_outputs.
y_predarray-likeEstimated target values, shaped as n_samples or n_samples by n_outputs.
sample_weightarray-like, default=NoneOptional weight for each sample.
multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’} or array-like of shape (n_outputs,), default=‘uniform_average’Output-score aggregation mode: uniform_average, raw_values, or variance_weighted; an array of weights is also supported.
force_finitebool, default=TrueWhether to replace non-finite constant-target scores with finite values.

Returns

float or ndarray of floats: the R² score, or one score per output when multioutput='raw_values'.

Key points

  • A perfect model scores 1.0, but poor predictions can produce negative values.
  • The score uses the constant predictor of the target average as the zero reference.
  • R² equals explained variance when prediction residuals have zero mean.
  • The result is a float for aggregated outputs or an array when raw_values is selected.

Common mistakes

  • Constant target values produce NaN for perfect predictions or negative infinity for imperfect predictions; the default force_finite behavior replaces these non-finite results.
  • Do not use the metric with fewer than two samples.
  • Swapping observed and predicted values can change the result.

Example: Score regression predictions

from sklearn.metrics import r2_score

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

print(r2_score(y_true, y_pred))

Computes the coefficient of determination for four observed and predicted target values.

When to use it

Regression model evaluation

Evaluate a regressor against held-out targets.

Baseline comparison

Compare predictions with an average-target baseline.

Multi-output regression scoring

Inspect or aggregate several target dimensions.

Interview questions

QWhat does r2_score measure, and what range can it have?

r2_score measures the coefficient of determination. Its best possible value is 1.0, while it may be negative when predictions are worse than the constant-average baseline.

Follow-up: What happens when all true targets are identical? The unadjusted score is NaN for perfect predictions and -Inf for imperfect predictions; force_finite=False exposes those values.

QHow does multioutput control the returned result?

For multiple outputs, uniform_average is the default aggregation, producing the unweighted mean of the output scores. raw_values returns one score per output, while variance_weighted weights outputs by their target variances.

Follow-up: Which option keeps output-specific diagnostics? Use raw_values, which returns an array instead of aggregating the outputs.

QWhat are the important input and control parameters?

y_true and y_pred contain the ground-truth and estimated targets, with shape n_samples or n_samples by n_outputs. sample_weight can assign a weight to each sample, and force_finite controls handling of non-finite scores from constant targets.

Follow-up: When should force_finite be disabled? Disable it when downstream analysis must distinguish NaN and -Inf rather than receive finite replacements.

  • sklearn.metrics.explained_variance_score
  • sklearn.metrics.mean_squared_error
  • sklearn.metrics.get_scorer

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