R-squared score penalizes a prediction that is shifted away from the truth, because the shift shows up in the residual sum of squares. Sometimes that is not what you want. If a model is consistently 3 units too high but otherwise tracks every movement of the target perfectly, the explained variance score ignores the constant offset and still rewards the model for tracking the variation. It measures how much of the target's variance the errors fail to explain, with the mean error removed.
Implement explained_variance_score(y_true, y_pred), which returns 1 - Var(y_true - y_pred) / Var(y_true), using population variance (ddof = 0).
ValueError.y_true is constant, Var(y_true) is zero: raise ValueError.1.Open one at a time. Each gives away a little more than the last.
np.var already subtracts the mean, so np.var(y_true - y_pred) ignores a constant offset automatically.
Check np.var(y_true) == 0 before dividing, the same way the R-squared question checks for a constant target.
No results yet
Run your code to check it against the examples, or submit to run every test.