Numpy Python 相当于 MATLAB corr

Numpy Python equivalent of MATLAB corr

objective是计算两个数组的相关性。特别是 polyval 输出与另一个时间序列之间的相关性。

给出信号

y_ax = [7.8625913,7.7713094,7.6833806,7.5997391,7.5211883,7.4483986,7.3819046, 7.3221073,7.2692747,7.2235470,7.1849418,7.1533613,7.1286001,7.1103559,7.0982385, 7.0917811,7.0904517,7.0936642,7.1007910,7.1111741,7.1241360,7.1389918,7.1550579, 7.1716633, 7.1881566, 7.2039142, 7.2183490, 7.2309117, 7.2410989, 7.2484550, 7.2525721, 7.2530937, 7.2497110, 7.2421637, 7.2302341, 7.2137470, 7.1925621, 7.1665707, 7.1356878, 7.0998487, 7.0590014, 7.0131001, 6.9621005, 6.9059525, 6.8445964, 6.7779589, 6.7059474, 6.6284504, 6.5453324, 6.4564347, 6.3615761, 6.2605534, 6.1531439, 6.0391097, 5.9182019, 5.7901659, 5.6547484, 5.5117044, 5.3608050, 5.2018456, 5.0346560, 4.8591075, 4.6751242, 4.4826899, 4.2818580, 4.0727611, 3.8556159, 3.6307325, 3.3985188, 3.1594861, 2.9142516, 2.6635408, 2.4081881, 2.1491354, 1.8874279, 1.6242117, 1.3607255, 1.0982931, 0.83831298] 


x_ax = [6210, 6211, 6212, 6213, 6214, 6215, 6216, 6217, 6218, 6219, 6220, 6221, 6222, 6223, 6224, 6225, 6226, 6227, 6228, 6229, 6230, 6231, 6232, 6233, 6234, 6235, 6236, 6237, 6238, 6239, 6240, 6241, 6242, 6243, 6244, 6245, 6246, 6247, 6248, 6249, 6250, 6251, 6252, 6253, 6254, 6255, 6256, 6257, 6258, 6259, 6260, 6261, 6262, 6263, 6264, 6265, 6266, 6267, 6268, 6269, 6270, 6271, 6272, 6273, 6274, 6275, 6276, 6277, 6278, 6279, 6280, 6281, 6282, 6283, 6284, 6285, 6286, 6287, 6288] 

在 MATLAB 中,这可以实现为

[p, S, mu] = polyfit(x_ax, y_ax, 1);
     ffit  = polyval(p, x_ax, S, mu);
   cor_val = corr(y_ax, ffit )

cor_val相当于0.85406816

同样,前两步可以使用Numpy

重现
y_ax=np.array(y_ax)
x_ax=np.array(x_ax)
coefs = np.polyfit ( x_ax, y_ax, 1 )
ffit = np.polyval(coefs,x_ax)

然而,使用 Numpy correlate 如下

cor_val = np.correlate(y_ax,ffit)

不通过 Matlab corr 产生 return 的值。

我尝试了所有 3 个 Numpy correlate 模式 {'valid', 'same', 'full'},但都没有产生与 Matlab 相同的结果。

请问,什么参数或 Numpy Python 相当于 MATLAB corr

您可以改用np.corrcoef()

In [63]: y_ax = [7.8625913,7.7713094,7.6833806,7.5997391,7.5211883,7.4483986,7.3819046, 7.3221073,7.2692747,7.2235470,7.1849418,7.1533613
    ...: ,7.1286001,7.1103559,7.0982385, 7.0917811,7.0904517,7.0936642,7.1007910,7.1111741,7.1241360,7.1389918,7.1550579, 7.1716633, 7.18
    ...: 81566, 7.2039142, 7.2183490, 7.2309117, 7.2410989, 7.2484550, 7.2525721, 7.2530937, 7.2497110, 7.2421637, 7.2302341, 7.2137470,
    ...: 7.1925621, 7.1665707, 7.1356878, 7.0998487, 7.0590014, 7.0131001, 6.9621005, 6.9059525, 6.8445964, 6.7779589, 6.7059474, 6.62845
    ...: 04, 6.5453324, 6.4564347, 6.3615761, 6.2605534, 6.1531439, 6.0391097, 5.9182019, 5.7901659, 5.6547484, 5.5117044, 5.3608050, 5.2
    ...: 018456, 5.0346560, 4.8591075, 4.6751242, 4.4826899, 4.2818580, 4.0727611, 3.8556159, 3.6307325, 3.3985188, 3.1594861, 2.9142516,
    ...:  2.6635408, 2.4081881, 2.1491354, 1.8874279, 1.6242117, 1.3607255, 1.0982931, 0.83831298]

In [64]: x_ax = [6210, 6211, 6212, 6213, 6214, 6215, 6216, 6217, 6218, 6219, 6220, 6221, 6222, 6223, 6224, 6225, 6226, 6227, 6228, 6229,
    ...: 6230, 6231, 6232, 6233, 6234, 6235, 6236, 6237, 6238, 6239, 6240, 6241, 6242, 6243, 6244, 6245, 6246, 6247, 6248, 6249, 6250, 62
    ...: 51, 6252, 6253, 6254, 6255, 6256, 6257, 6258, 6259, 6260, 6261, 6262, 6263, 6264, 6265, 6266, 6267, 6268, 6269, 6270, 6271, 6272
    ...: , 6273, 6274, 6275, 6276, 6277, 6278, 6279, 6280, 6281, 6282, 6283, 6284, 6285, 6286, 6287, 6288]
In [65]: np.corrcoef(y_ax, x_ax)
Out[65]: 
array([[ 1.        , -0.85406816],
       [-0.85406816,  1.        ]])