Taking subarrays from numpy array with given stride/stepsize

前端 未结 2 1580
不知归路
不知归路 2020-11-22 02:45

Lets say I have a Python Numpy array a.

a = numpy.array([1,2,3,4,5,6,7,8,9,10,11])

I want to create a matrix of sub sequences

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:34

    Approach #1 : Using broadcasting -

    def broadcasting_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
        nrows = ((a.size-L)//S)+1
        return a[S*np.arange(nrows)[:,None] + np.arange(L)]
    

    Approach #2 : Using more efficient NumPy strides -

    def strided_app(a, L, S ):  # Window len = L, Stride len/stepsize = S
        nrows = ((a.size-L)//S)+1
        n = a.strides[0]
        return np.lib.stride_tricks.as_strided(a, shape=(nrows,L), strides=(S*n,n))
    

    Sample run -

    In [143]: a
    Out[143]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    
    In [144]: broadcasting_app(a, L = 5, S = 3)
    Out[144]: 
    array([[ 1,  2,  3,  4,  5],
           [ 4,  5,  6,  7,  8],
           [ 7,  8,  9, 10, 11]])
    
    In [145]: strided_app(a, L = 5, S = 3)
    Out[145]: 
    array([[ 1,  2,  3,  4,  5],
           [ 4,  5,  6,  7,  8],
           [ 7,  8,  9, 10, 11]])
    

提交回复
热议问题