通过使用不同维度的 numpy 数组创建循环来计算相关系数

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

我正在使用两个不同的数组:x 和 y。

x 的形状 = (442,10)

y 的形状 = (442,)

我正在尝试编写一个循环来打印 y 和 x 中的 10 个特征之间的 10 个相关系数。

这是我的代码:

from numpy.lib.stride_tricks import sliding_window_view as swv

def np_corr(w, z):
    denom = (np.sqrt((len(z) * np.sum(w**2, axis=-1) - np.sum(w, axis=-1) ** 2)
                       * (len(z) * np.sum(z**2) - np.sum(z)**2)))
    return np.divide((len(z) * np.sum(w * z[None, :], axis=-1) - (np.sum(w, axis=-1) * np.sum(y))),
                     denom, where=denom!=0
                    )

corr = np_corr(swv(x, len(y)), y)

运行代码时出现此错误:由于 axis 为

None
,必须为
x
的所有维度提供 window_shape;有 1 个 window_shape 元素,
x.ndim
为 2。

不确定如何修复它以及是否有其他方法可以做到这一点。

python numpy loops correlation
1个回答
1
投票

您的错误:

In [41]: swv(np.ones((3,4)),4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[41], line 1
----> 1 swv(np.ones((3,4)),4)

File <__array_function__ internals>:180, in sliding_window_view(*args, **kwargs)

File ~\miniconda3\lib\site-packages\numpy\lib\stride_tricks.py:315, in sliding_window_view(x, window_shape, axis, subok, writeable)
    313     axis = tuple(range(x.ndim))
    314     if len(window_shape) != len(axis):
--> 315         raise ValueError(f'Since axis is `None`, must provide '
    316                          f'window_shape for all dimensions of `x`; '
    317                          f'got {len(window_shape)} window_shape elements '
    318                          f'and `x.ndim` is {x.ndim}.')
    319 else:
    320     axis = normalize_axis_tuple(axis, x.ndim, allow_duplicate=True)

ValueError: Since axis is `None`, must provide window_shape for all dimensions of `x`; got 1 window_shape elements and `x.ndim` is 2.

有效的通话:

In [45]: swv(np.ones((3,4)),(2,2)).shape
Out[45]: (2, 3, 2, 2)

在函数的最后一行进行更正:

In [48]: def np_corr(w, z):
    ...:     denom = (np.sqrt((len(z) * np.sum(w**2, axis=-1) - np.sum(w, axis=-1) ** 2)
    ...:                        * (len(z) * np.sum(z**2) - np.sum(z)**2)))
    ...:     return np.divide((len(z) * np.sum(w * z[None, :], axis=-1) - (np.sum(w, axis=-1) * np.sum(z))),
    ...:                      denom, where=denom!=0
    ...:                     )
    ...:                     

运行:

In [49]: np_corr(np.arange(100).reshape(10,10), np.arange(10))
Out[49]: array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
© www.soinside.com 2019 - 2024. All rights reserved.