Aditya Johorey
Aditya Johorey

Reputation: 53

Getting non zero value for r2_score() when I comapre true values with their mean

I was using r2_score() method, for evaluation of a Regression problem using scikit learn. As we know that we should get, 0.0 for the coefficient of determination R^2 of the prediction. but I'm getting 2.220446049250313e-16 when I do so.

here is the code

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import load_boston
import pandas as pd

boston=load_boston()
target=boston['target']
boston=pd.DataFrame(data=boston['data'],columns=boston['feature_names'])
boston['target']=target

x=boston.drop('target',axis=1)
y=boston['target']

np.random.seed(42)
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)
model=RandomForestRegressor().fit(x_train,y_train)
model.score(x_test,y_test,)

This code segment returns:

0.873969014117403

and this code segment:

from sklearn.metrics import r2_score
y_preds=model.predict(x_test)
r2_score(y_test,y_preds)

also returns:

0.873969014117403

But when I run this code segment where I'm comparing the mean of the test values with their mean:

y_test_mean=np.full(len(y_test),y_test.mean())
r2_score(y_test,y_test_mean)

I get the value(which is an error):

2.220446049250313e-16

Because, it should be 0.0 according to the scikit learn documentation.Here is the code snippet from there:

>>> y_true = [1, 2, 3]
>>> y_pred = [2, 2, 2]
>>> r2_score(y_true, y_pred)
0.0

I'm using google colaboratory for my work. Can anyone pls help me figure out, why the error is coming?? Thank you.

Upvotes: 0

Views: 267

Answers (0)

Related Questions