在2D数组上的Numpy滚动窗口,作为1D数组,嵌套数组作为数据值 [英] Numpy rolling window over 2D array, as a 1D array with nested array as data values

查看:82
本文介绍了在2D数组上的Numpy滚动窗口,作为1D数组,嵌套数组作为数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 np.lib.stride_tricks.as_strided 时,如何使用嵌套数组作为数据值来管理2D数组?有没有更可取的有效方法?

When using np.lib.stride_tricks.as_strided, how can I manage 2D a array with the nested arrays as data values? Is there a preferable efficient approach?

特别是,如果我有2D np.array 看起来如下,其中一维数组中的每个数据项都是长度为2的数组:

Specifically, if I have a 2D np.array looking as follows, where each data item in a 1D array is an array of length 2:

[[1., 2.],[3., 4.],[5.,6.],[7.,8.],[9.,10.]...]

我要重塑以进行如下滚动:

I want to reshape for rolling over as follows:

[[[1., 2.],[3., 4.],[5.,6.]],
 [[3., 4.],[5.,6.],[7.,8.]],
 [[5.,6.],[7.,8.],[9.,10.]],
  ...
]

我也看过类似的答案(例如此滚动窗口功能),但是在使用中,我无法使内部数组/元组保持不变。

I have had a look at similar answers (e.g. this rolling window function), however in use I cannot leave the inner array/tuples untouched.

窗口长度为 3 的窗口:我试过 shape (len(seq) + 3-1,3,2)步幅(2 * 8,2 * 8,8) ,但没有运气。

For example with a window length of 3: I have tried a shape of (len(seq)+3-1, 3, 2) and a stride of (2 * 8, 2 * 8, 8), but no luck. Maybe I am missing something obvious?

干杯。

编辑:使用Python内置的代码可以轻松生成功能相同的解决方案(可以使用例如Divakar解决方案的 np.arange 进行优化。 ),但是使用 as_strided 怎么办?据我了解,这可以用于高效解决方案吗?

It is easy to produce a functionally identical solution using Python built-ins (which can be optimised using e.g. np.arange similar to Divakar's solution), however, what about using as_strided? From my understanding, this could be used for a highly efficient solution?

推荐答案

您的怎么了? as_strided 试用?

In [28]: x=np.arange(1,11.).reshape(5,2)
In [29]: x.shape
Out[29]: (5, 2)
In [30]: x.strides
Out[30]: (16, 8)
In [31]: np.lib.stride_tricks.as_strided(x,shape=(3,3,2),strides=(16,16,8))
Out[31]: 
array([[[  1.,   2.],
        [  3.,   4.],
        [  5.,   6.]],

       [[  3.,   4.],
        [  5.,   6.],
        [  7.,   8.]],

       [[  5.,   6.],
        [  7.,   8.],
        [  9.,  10.]]])

在我的第一次编辑中,我使用了 int 数组,因此不得不使用(8,8,4)

On my first edit I used an int array, so had to use (8,8,4) for the strides.

您的形状可能是错误的。如果太大,则会开始从数据缓冲区的末尾看到值。

Your shape could be wrong. If too large it starts seeing values off the end of the data buffer.

   [[  7.00000000e+000,   8.00000000e+000],
    [  9.00000000e+000,   1.00000000e+001],
    [  8.19968827e-257,   5.30498948e-313]]])

这里只是改变了显示方法, 7、8、9、10 仍然存在。编写这些插槽可能很危险,会弄乱代码的其他部分。 as_strided 最好用于只读目的。写/集比较棘手。

Here it just alters the display method, the 7, 8, 9, 10 are still there. Writing those those slots could be dangerous, messing up other parts of your code. as_strided is best if used for read-only purposes. Writes/sets are trickier.

这篇关于在2D数组上的Numpy滚动窗口,作为1D数组,嵌套数组作为数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆