Skip to content

Instantly share code, notes, and snippets.

@rjurney
Created October 20, 2020 22:21
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 rjurney/1a887b475734c7239fdbfef6ceafb64d to your computer and use it in GitHub Desktop.
Save rjurney/1a887b475734c7239fdbfef6ceafb64d to your computer and use it in GitHub Desktop.
Implementations of R^2 Score
def r2_score(y_true, y_pred):
"""Implements the Coeffecient of Determination, R^2 or R-squared"""
SS_res = kb.sum(kb.square(y_true - y_pred))
SS_tot = kb.sum(kb.square(y_true - kb.mean(y_true)))
return (1 - SS_res / (SS_tot + kb.epsilon()))
def inverse_r2_score(y_true, y_pred):
"""Implements the inverse Coeffecient of Determination, R^2 or R-squared"""
SS_res = kb.sum(kb.square(y_true - y_pred))
SS_tot = kb.sum(kb.square(y_true - kb.mean(y_true)))
return 1 / (1 - SS_res / (SS_tot + kb.epsilon()))
def negative_r2_score(y_true, y_pred):
"""Implements the negative Coeffecient of Determination, R^2 or R-squared"""
SS_res = kb.sum(kb.square(y_true - y_pred))
SS_tot = kb.sum(kb.square(y_true - kb.mean(y_true)))
return -1 * (1 - SS_res / (SS_tot + kb.epsilon()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment