我有两个 numpy 数组,我想计算它们的相关性。我正在使用 numpy.corrcoef,但我也想通过应用卷积来做到这一点,但我得到了完全不同的结果。我错过了什么?
array1 = np.array([1,3,10,4,2])
array2 = np.array([3,7,1,5,6])
# numpy corrcoef method
correlation = np.corrcoef(array1, array2)[0, 1]
print("Correlation:", correlation)
# convolution
rxx = np.convolve(array1,np.flip(array1))
rxx = rxx[rxx.size//2]
ryy = np.convolve(array2,np.flip(array2))
ryy = ryy[ryy.size//2]
rxy = np.convolve(array1,np.flip(array2))
rho = rxy / (np.sqrt(ryy*rxx))
rho = rho[rho.size//2]
print("Correlation:", rho)
我错过的是通过向量的方式对向量进行归一化
import numpy as np
array1 = np.array([1,3,10,4,2])
norm_array1 = array1-np.mean(array1)
array2 = np.array([3,7,1,5,6])
norm_array2 = array2-np.mean(array2)
# Calculate the correlation
correlation = np.corrcoef(array1, array2)[0, 1]
print("Correlation:", correlation)
rxx = np.convolve(norm_array1,np.flip(norm_array1))
rxx = rxx[rxx.size//2]
ryy = np.convolve(norm_array2,np.flip(norm_array2))
ryy = ryy[ryy.size//2]
rxy = np.convolve(norm_array1,np.flip(norm_array2))
rho = rxy / (np.sqrt(ryy*rxx))
rho = rho[rho.size//2]
print("Correlation:", rho)