Regression model evaluation
Evaluate a regressor against held-out targets.
Scikit-Learn · Function
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.
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.
r2_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True)| Name | Type | Description |
|---|---|---|
y_true | array-like | Ground-truth target values, shaped as n_samples or n_samples by n_outputs. |
y_pred | array-like | Estimated target values, shaped as n_samples or n_samples by n_outputs. |
sample_weight | array-like, default=None | Optional 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_finite | bool, default=True | Whether to replace non-finite constant-target scores with finite values. |
float or ndarray of floats: the R² score, or one score per output when multioutput='raw_values'.
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.
Evaluate a regressor against held-out targets.
Compare predictions with an average-target baseline.
Inspect or aggregate several target dimensions.
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.
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.
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_scoresklearn.metrics.mean_squared_errorsklearn.metrics.get_scorer