为什么相关性是由核仁()和np.std标准()与np.CORRCOF公司()

2024-04-26 05:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望有人能帮助我回答为什么由numpy.cov()np.std()计算的相关结果不同于由np.corrcoef()直接计算的结果。你知道吗

下面的代码显示了差异

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

X = np.random.rand(50)
Y = np.random.rand(50)

plt.scatter(X,Y)
plt.xlabel('X Value')
plt.ylabel('Y Value')

# taking the relevant value from the matrix returned by np.cov
print 'Correlation: ' + str(np.cov(X,Y)[0,1]/(np.std(X)*np.std(Y)))
# Let's also use the builtin correlation function
print 'Built-in Correlation: ' + str(np.corrcoef(X, Y)[0, 1])

输出示例:

Correlation: -0.0972430699897

Built-in Correlation: -0.0952982085899


Tags: theimportnumpyvalueasnppltrandom
1条回答
网友
1楼 · 发布于 2024-04-26 05:08:39

感谢Brian Borchers的建议

检查文档后,差异是由于参数ddof(delta degrees of freedom)的np.std()默认为0,而1用于np.corrcoef()

修改如下,代码可以生成相同的结果:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

X = np.random.rand(50)
Y = np.random.rand(50)

plt.scatter(X,Y)
plt.xlabel('X Value')
plt.ylabel('Y Value')

# taking the relevant value from the matrix returned by np.cov
print 'Correlation: ' + str(np.cov(X,Y)[0,1]/(np.std(X,ddof=1)*np.std(Y,ddof=1)))
# Let's also use the builtin correlation function
print 'Built-in Correlation: ' + str(np.corrcoef(X, Y)[0, 1])

输出示例:

Correlation: -0.174042621953

Built-in Correlation: -0.174042621953

相关问题 更多 >