Skip to content

Instantly share code, notes, and snippets.

@ericmjl
Last active August 24, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmjl/b51ca0dffc3ab72052356e91f1a3552e to your computer and use it in GitHub Desktop.
Save ericmjl/b51ca0dffc3ab72052356e91f1a3552e to your computer and use it in GitHub Desktop.
Difference between the my own implementation of explained variance and scikit-learn's
from sklearn.metrics import explained_variance_score
def var_explained(preds, actual):
"""
Implementation taken directly from the formula on this page:
http://scikit-learn.org/stable/modules/model_evaluation.html#explained-variance-score
"""
return 1 - ((preds - actual).var() / actual.var())
y_pred = np.array([3, -0.5, 2, 7])
y_actual = np.array([2.5, 0.0, 2, 8])
var_explained(y_pred, y_actual) # 0.9644760213143873
explained_variance_score(y_pred, y_actual) # 0.9571734475374732
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment