explained_variance_score#
- 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 eitherNaN
(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. Ifforce_finite
is set toFalse
, 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 theR^2 score
should be preferred.Read more in the User guide.
- Parameters:
- 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 isTrue
, a convenient setting for hyperparameters’ search procedures (e.g. grid search cross-validation).Added in version 1.1.
- Returns:
- scorefloat or ndarray of floats
The explained variance or ndarray if ‘multioutput’ is ‘raw_values’.
See also
r2_score
Similar metric, but accounting for systematic offsets in prediction.
Notes
This is not a symmetric function.
Examples
>>> 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