gpt4 book ai didi

python - numpy strides 只能在子数组内跨步吗?

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:52 24 4
gpt4 key购买 nike

我有一个非常大的 numpy 数组(145000 行 * 550 列)。我想在子数组中创建滚动切片。我试图用一个函数来实现它。函数 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 数组扩展这个跨数组步幅删除?

最佳答案

当然,这可以用 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]])

请注意, reshape 的最后一步强制它在那里制作副本。但如果我们需要最终输出为 2D,那是无法避免的。如果我们对 3D 输出没问题,请跳过 reshape ,从而实现 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

关于python - numpy strides 只能在子数组内跨步吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46114811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com