繁体   English   中英

通过创建具有不同维度的循环 numpy arrays 来计算相关系数

[英]Calculate correlation coefficients by creating a loop with numpy arrays of different dimensions

我正在使用两个不同的 arrays: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)

运行代码时出现此错误:由于轴为None ,必须为x的所有维度提供 window_shape ; 得到 1 个 window_shape 元素, x.ndim为 2。

不确定如何修复它以及是否有其他方法可以解决。 谢谢您的帮助!

你的错误:

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)

在您的 function 的最后一行进行更正:

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.])

这是否意味着完整的答案,我不会说。 你没有提供 [mcve]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM