gpt4 book ai didi

python - numpy.cov() 函数是如何实现的?

转载 作者:太空狗 更新时间:2023-10-29 20:42:11 30 4
gpt4 key购买 nike

我有自己的基于等式的协方差函数的实现:

enter image description here

'''
Calculate the covariance coefficient between two variables.
'''

import numpy as np

X = np.array([171, 184, 210, 198, 166, 167])
Y = np.array([78, 77, 98, 110, 80, 69])

# Expected value function.
def E(X, P):
expectedValue = 0
for i in np.arange(0, np.size(X)):
expectedValue += X[i] * (P[i] / np.size(X))
return expectedValue

# Covariance coefficient function.
def covariance(X, Y):
'''
Calculate the product of the multiplication for each pair of variables
values.
'''
XY = X * Y

# Calculate the expected values for each variable and for the XY.
EX = E(X, np.ones(np.size(X)))
EY = E(Y, np.ones(np.size(Y)))
EXY = E(XY, np.ones(np.size(XY)))

# Calculate the covariance coefficient.
return EXY - (EX * EY)

# Display matrix of the covariance coefficient values.
covMatrix = np.array([[covariance(X, X), covariance(X, Y)],
[covariance(Y, X), covariance(Y, Y)]])
print("My function:", covMatrix)

# Display standard numpy.cov() covariance coefficient matrix.
print("Numpy.cov() function:", np.cov([X, Y]))

但问题是,我从我的函数和 numpy.cov() 中得到了不同的值,即:

My function: [[ 273.88888889  190.61111111]
[ 190.61111111 197.88888889]]
Numpy.cov() function: [[ 328.66666667 228.73333333]
[ 228.73333333 237.46666667]]

这是为什么呢? numpy.cov()函数是如何实现的?如果函数 numpy.cov() 实现良好,我做错了什么?我只是说,我的函数 covariance() 的结果与互联网上用于计算协方差系数的 paper 示例的结果一致,例如 http://www.naukowiec.org/wzory/statystyka/kowariancja_11.html .

最佳答案

numpy 函数的默认设置与您的不同。试试看

>>> np.cov([X, Y], ddof=0)
array([[ 273.88888889, 190.61111111],
[ 190.61111111, 197.88888889]])

引用资料:

关于python - numpy.cov() 函数是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27448352/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com