Explained variance regression score function.
Best possible score is 1.0, lower values are worse.
In the particular case when y_true is constant, the explained variance score is not finite: it is either NaN (perfect predictions) or -Inf (imperfect predictions). To prevent such non-finite numbers to pollute higher-level experiments such as a grid search cross-validation, by default these cases are replaced with 1.0 (perfect predictions) or 0.0 (imperfect predictions) respectively. If force_finite is set to False, this score falls back on the original \(R^2\) definition.
Note
The Explained Variance score is similar to the R^2 score, with the notable difference that it does not account for systematic offsets in the prediction. Most often the R^2 score should be preferred.
Read more in the User Guide.
Ground truth (correct) target values.
Estimated target values.
Sample weights.
Defines aggregating of multiple output scores. Array-like value defines weights used to average scores.
Returns a full set of scores in case of multioutput input.
Scores of all outputs are averaged with uniform weight.
Scores of all outputs are averaged, weighted by the variances of each individual output.
Flag indicating if NaN and -Inf scores resulting from constant data should be replaced with real numbers (1.0 if prediction is perfect, 0.0 otherwise). Default is True, a convenient setting for hyperparameters’ search procedures (e.g. grid search cross-validation).
Added in version 1.1.
The explained variance or ndarray if ‘multioutput’ is ‘raw_values’.
See also
r2_scoreSimilar metric, but accounting for systematic offsets in prediction.
This is not a symmetric function.
>>> from sklearn.metrics import explained_variance_score >>> y_true = [3, -0.5, 2, 7] >>> y_pred = [2.5, 0.0, 2, 8] >>> explained_variance_score(y_true, y_pred) 0.957... >>> y_true = [[0.5, 1], [-1, 1], [7, -6]] >>> y_pred = [[0, 2], [-1, 2], [8, -5]] >>> explained_variance_score(y_true, y_pred, multioutput='uniform_average') 0.983... >>> y_true = [-2, -2, -2] >>> y_pred = [-2, -2, -2] >>> explained_variance_score(y_true, y_pred) 1.0 >>> explained_variance_score(y_true, y_pred, force_finite=False) nan >>> y_true = [-2, -2, -2] >>> y_pred = [-2, -2, -2 + 1e-8] >>> explained_variance_score(y_true, y_pred) 0.0 >>> explained_variance_score(y_true, y_pred, force_finite=False) -inf
© 2007–2025 The scikit-learn developers
Licensed under the 3-clause BSD License.
https://scikit-learn.org/1.6/modules/generated/sklearn.metrics.explained_variance_score.html