numpy步幅只能在子阵列中跨越吗?

时间:2017-09-08 10:52:04

标签: python arrays python-3.x numpy stride

我有一个非常大的numpy数组(145000行* 550 cols)。我想在子阵列中创建滚动切片。我尝试用函数实现它。函数lagged_vals的行为符合预期,但np.lib.stride_tricks的行为与我想要的不同 -

def lagged_vals(series,l):
# Garbage implementation but still right
    return np.concatenate([[x[i:i+l] for i in range(x.shape[0]) if i+l <= x.shape[0]] for x in series]
                          ,axis = 0)

# Sample 2D numpy array
something = np.array([[1,2,2,3],[2,2,3,3]])
lagged_vals(something,2) # Works as expected

# array([[1, 2],
#     [2, 2],
#     [2, 3],
#     [2, 2],
#     [2, 3],
#     [3, 3]])


np.lib.stride_tricks.as_strided(something,
                               (something.shape[0]*something.shape[1],2),
                               (8,8))

# array([[1, 2],
#        [2, 2],
#        [2, 3],
#        [3, 2], <--- across subarray stride, which I do not want
#        [2, 2],
#        [2, 3],
#        [3, 3])

如何删除np.lib.stride_tricks实施中的特定行?如何为一个大的numpy数组缩放这个交叉数组步幅去除?

1 个答案:

答案 0 :(得分:3)

当然,这可以通过np.lib.stride_tricks.as_strided实现。这是一种方式 -

from numpy.lib.stride_tricks import as_strided

L = 2 # window length
shp = a.shape
strd = a.strides

out_shp = shp[0],shp[1]-L+1,L
out_strd = strd + (strd[1],)

out = as_strided(a, out_shp, out_strd).reshape(-1,L)

示例输入,输出 -

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

In [178]: out
Out[178]: 
array([[0, 1],
       [1, 2],
       [2, 3],
       [4, 5],
       [5, 6],
       [6, 7]])

请注意,重塑的最后一步迫使其在那里制作副本。但如果我们需要最终输出为2D,则无法避免这种情况。如果我们对3D输出没问题,请跳过该重新整形,从而获得view,如示例案例所示 -

In [181]: np.shares_memory(a, out)
Out[181]: False

In [182]: as_strided(a, out_shp, out_strd)
Out[182]: 
array([[[0, 1],
        [1, 2],
        [2, 3]],

       [[4, 5],
        [5, 6],
        [6, 7]]])

In [183]: np.shares_memory(a, as_strided(a, out_shp, out_strd) )
Out[183]: True