Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

python - Encountered invalid value when I use pearsonr

Maybe I made a mistake. If so, I am sorry to ask this.

I want to calculate Pearson's correlation coefficent by using scipy's pearsonr function.

from scipy.stats.stats import pearsonr

X = [4, 4, 4, 4, 4, 4]
Y = [4, 5, 5, 4, 4, 4]

pearsonr(X, Y)

I get an error below

RuntimeWarning: invalid value encountered in double_scalars ###

The reason why I get an error is E[X] = 4 (Excepted Value of X is 4)

I look at the code of pearsonr function in scpy.stats.stats.py. Some part of the pearsonr function is as follows.

mx = x.mean() # which is 4
my = y.mean() # not necessary
xm, ym = x-mx, y-my # xm = [0 0 0 0 0 0]
r_num = n*(np.add.reduce(xm*ym)) #r_num = 0, because xm*ym 1x6 Zero Vector.
r_den = n*np.sqrt(ss(xm)*ss(ym)) #r_den = 0
r = (r_num / r_den) # Invalid value encountered in double_scalars

At the end, pearsonr returns (nan, 1.0)

Should pearsonr return (0, 1.0)?

I think if a vector has same value for every row/column, covariance should be zero. Thus Pearson's Correleation Coefficient should also be zero by the definition of PCC.

Pearson's correlation coefficient between two variables is defined as the covariance of the two variables divided by the product of their standard deviations.

Is it bug or where do I make a mistake?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pearson's correlation coefficient between two variables is defined as the covariance of the two variables divided by the product of their standard deviations.

So it's the covariance over

  • the standard deviation of [4, 5, 5, 4, 4, 4] times
  • the standard deviation of [4, 4, 4, 4, 4, 4].

The standard deviation of [4, 4, 4, 4, 4, 4] is zero.

So it's the covariance over

  • the standard deviation of [4, 5, 5, 4, 4, 4] times
  • zero.

So it's the covariance over

  • zero.

Anything divided by zero is nan. The value of the covariance is irrelevant.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...