Massoud
Massoud

Reputation: 523

Coefficient of determination is different between scikit-learn and scipy libraries. why?

I have a dataset from a paper and I am having a hard time verifying their reported coefficient of determination, R-squared. I used sklearn and scipy libraries and im getting different answers. why? which one is more reliable? p.s. when I used Excel as another alternative I got the same answer as from scipy.

Below is the code I used to compare the results from SKlearn and Scipy:

import pandas as pd
from scipy import stats
from sklearn.metrics import r2_score

data = pd.read_csv("output.csv", header=None)

y_measured = data.iloc[0, :].values
y_predicted = data.iloc[1, :].values

print(r2_score(y_measured, y_predicted)) # prints 0.708717556205

slope, intercept, r_value, p_value, std_err = stats.linregress(y_measured, y_predicted)

print(r_value**2)  # prints 0.731889173485

Upvotes: 4

Views: 3869

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114841

r2_score computes its value from the given y values and the y values predicted by the linear regression line, not from the given x and y values.

Here's an example. First, the imports:

In [59]: import numpy as np

In [60]: from scipy import stats

In [61]: from sklearn.metrics import r2_score

Some data to work with:

In [62]: x = np.array([0, 1, 2, 3, 5, 8, 13])

In [63]: y = np.array([1.2, 1.4, 1.6, 1.7, 2.0, 4.1, 6.6])

Do the linear regression using scipy.stats.linregress, and check r2:

In [64]: slope, intercept, rvalue, pvalue, stderr = stats.linregress(x, y)

In [65]: rvalue**2
Out[65]: 0.9485915175891462

For the given x values, compute the y values predicted by the regression line:

In [66]: ypred = slope*x + intercept

Compute r2 again, using r2_score:

In [67]: r2_score(y, ypred)
Out[67]: 0.9485915175891464

As expected, we get the same value.

We can also compute this value with scipy.stats.pearsonr:

In [68]: pearson_r, pearson_p = stats.pearsonr(x, y)

In [69]: pearson_r**2
Out[69]: 0.9485915175891464

Upvotes: 6

Related Questions