0

This is my code, I calculated R square using Scikit learn :

y =[0, 10, 20, 30, 40]
f =[0, 1, 2, 3, 4]
r2 = r2_score(y, f)
print('r2 score for perfect model is', r2)

Output :

r2 score for perfect model is -1.4300000000000002

Why is this coming out negative?

sage76
  • 13
  • 1
    FAQ: https://stats.stackexchange.com/search?q=negative+r+square. – whuber Sep 13 '22 at 16:11
  • In order to point you to the specific parts of the duplicate that are pertinent to your particular confusion, why do you think the $R^2$ returned by your code should be $1?$ (You refer to a perfect model, so I assume you expect a perfect $R^2$.) – Dave Sep 13 '22 at 16:24
  • Yes, it's a perfect fit, so why am i getting a negative value? – sage76 Sep 13 '22 at 16:30
  • How is it a perfect fit? Your observed value is 40, and you predict 4 (or maybe you observed 4 and predicted 40). // You might be interested in my simulations here and the paragraph right below the simulation code. – Dave Sep 13 '22 at 16:36

0 Answers0