sklearn.metrics.explained_variance_score(y_true, y_pred, *, sample_weight=None, multioutput='uniform_average', force_finite=True)[source]

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.


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.

y_truearray-like of shape (n_samples,) or (n_samples, n_outputs)

Ground truth (correct) target values.

y_predarray-like of shape (n_samples,) or (n_samples, n_outputs)

Estimated target values.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

multioutput{‘raw_values’, ‘uniform_average’, ‘variance_weighted’} or array-like of shape (n_outputs,), default=’uniform_average’

Defines aggregating of multiple output scores. Array-like value defines weights used to average scores.

‘raw_values’ :

Returns a full set of scores in case of multioutput input.

‘uniform_average’ :

Scores of all outputs are averaged with uniform weight.

‘variance_weighted’ :

Scores of all outputs are averaged, weighted by the variances of each individual output.

force_finitebool, default=True

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

New in version 1.1.

scorefloat or ndarray of floats

The explained variance or ndarray if ‘multioutput’ is ‘raw_values’.

See also


Similar 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)
>>> 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')
>>> y_true = [-2, -2, -2]
>>> y_pred = [-2, -2, -2]
>>> explained_variance_score(y_true, y_pred)
>>> explained_variance_score(y_true, y_pred, force_finite=False)
>>> y_true = [-2, -2, -2]
>>> y_pred = [-2, -2, -2 + 1e-8]
>>> explained_variance_score(y_true, y_pred)
>>> explained_variance_score(y_true, y_pred, force_finite=False)