沿2D数组的最后一个轴滑动窗口以使用NumPy步幅生成3D数组 [英] Sliding windows along last axis of a 2D array to give a 3D array using NumPy strides

查看:64
本文介绍了沿2D数组的最后一个轴滑动窗口以使用NumPy步幅生成3D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 numpy.lib.stride_tricks 中的函数 as_strided 从更大的2D数组中提取子序列,但是我一直在努力寻找合适的方法为 strides 参数编写.

I am trying to use the function as_strided from numpy.lib.stride_tricks to extract sub series from a larger 2D array, but I struggled to find the right thing to write for the strides argument.

比方说,我有一个矩阵 m ,其中包含5个1D数组,长度为( a = )10.我想为 m 中的每个1D数组提取长度为( b = )4的1D子数组.

Let's say I have a matrix m which contains 5 1D array of length (a=)10. I want to extract sub 1D arrays of length (b=)4 for each 1D array in m.

import numpy
from numpy.lib.stride_tricks import as_strided

a, b = 10, 4
m = numpy.array([range(i,i+a) for i in range(5)])

# first try
sub_m = as_strided(m, shape=(m.shape[0], m.shape[1]-b+1, b))
print sub_m.shape # (5,7,4) which is what i expected
print sub_m[-1,-1,-1] # Some unexpected strange number: 8227625857902995061

# second try with strides argument
sub_m = as_strided(m, shape=(m.shape[0], m.shape[1]-b+1, b), strides=(m.itemize,m.itemize,m.itemize))
# gives error, see below

AttributeError:"numpy.ndarray"对象没有属性"itemize"

AttributeError: 'numpy.ndarray' object has no attribute 'itemize'

如您所见,在我的第一次尝试中,我成功地为 sub_m 设置了正确的形状.但是我找不到在 strides =()

As you can see I succeed to get the right shape for sub_m in my first try. However I can't find what to write in strides=()

有关信息:

m = [[ 0  1  2  3  4  5  6  7  8  9]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 2  3  4  5  6  7  8  9 10 11]
 [ 3  4  5  6  7  8  9 10 11 12]
 [ 4  5  6  7  8  9 10 11 12 13]]

预期输出:

sub_n = [
         [[0 1 2 3] [1 2 3 4] ... [5 6 7 8] [6 7 8 9]]
         [[1 2 3 4] [2 3 4 5] ... [6 7 8 9] [7 8 9 10]]
         [[2 3 4 5] [3 4 5 6] ... [7 8 9 10] [8 9 10 11]]
         [[3 4 5 6] [4 5 6 7] ... [8 9 10 11] [9 10 11 12]]
         [[4 5 6 7] [5 6 7 8] ... [9 10 11 12] [10 11 12 13]]
        ]

edit :我有更多数据,这就是为什么我要使用 as_strided (效率)

edit: I have much more data, that's the reason why I want to use as_strided (efficiency)

推荐答案

这是使用 np.lib.stride_tricks.as_strided -

def strided_lastaxis(a, L):
    s0,s1 = a.strides
    m,n = a.shape
    return np.lib.stride_tricks.as_strided(a, shape=(m,n-L+1,L), strides=(s0,s1,s1))

有关 as_strided 的步幅的一些解释:

Bit of explanation on strides for as_strided :

我们有3D步幅,它沿最后/第三轴增加一个元素,所以在那里 s1 代表最后一个步幅.第二根轴跨过相同的一个元素距离",因此也使用 s1 .对于第一个轴,步幅与数组的第一个轴的步幅长度相同,因为我们在下一行上移动,所以在这里 s0 .

We have 3D strides, that increments by one element along the last/third axis, so s1 there for the last axis striding. The second axis strides by the same one element "distance", so s1 for that too. For the first axis, the striding is same as the first axis stride length of the array, as we move on the next row, so s0 there.

样品运行-

In [46]: a
Out[46]: 
array([[0, 5, 6, 2, 3, 6, 7, 1, 4, 8],
       [2, 1, 3, 7, 0, 3, 5, 4, 0, 1]])

In [47]: strided_lastaxis(a, L=4)
Out[47]: 
array([[[0, 5, 6, 2],
        [5, 6, 2, 3],
        [6, 2, 3, 6],
        [2, 3, 6, 7],
        [3, 6, 7, 1],
        [6, 7, 1, 4],
        [7, 1, 4, 8]],

       [[2, 1, 3, 7],
        [1, 3, 7, 0],
        [3, 7, 0, 3],
        [7, 0, 3, 5],
        [0, 3, 5, 4],
        [3, 5, 4, 0],
        [5, 4, 0, 1]]])

这篇关于沿2D数组的最后一个轴滑动窗口以使用NumPy步幅生成3D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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