Skip to content

Instantly share code, notes, and snippets.

@WittmannF
Created January 23, 2019 18:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WittmannF/02060b45ce3ec9239898a5b91df2564e to your computer and use it in GitHub Desktop.
Save WittmannF/02060b45ce3ec9239898a5b91df2564e to your computer and use it in GitHub Desktop.
Very simple code for comparing different R2 scores
from sklearn.datasets import make_regression
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
#%matplotlib inline
# Generate toy data
X, y, w_true = make_regression(n_samples=20, n_features=1, random_state=0, noise=1.0, coef=True)
w_bad = 0.5*w_true
w_verybad = -0.3*w_true
# Plot true data
plt.figure(figsize=(8,5))
plt.plot(X, y, '.')
# Plot a good, a bad and a very model with R2 scores
for w in [w_true, w_bad, w_verybad]:
plt.plot(X, w*X, label="R2 Score = {:.3f}".format(r2_score(y, w*X)))
plt.legend(loc=0)
plt.show()
@WittmannF
Copy link
Author

Output:
download 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment