两个向量的相关性 - numpy.corrcoef 与卷积方法

问题描述 投票:0回答:1

我有两个 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)
statistics signal-processing correlation convolution pearson-correlation
1个回答
0
投票

我错过的是通过向量的方式对向量进行归一化

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)
© www.soinside.com 2019 - 2024. All rights reserved.