【发布时间】:2017-09-08 10:52:04
【问题描述】:
我有一个非常大的 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 数组扩展这种跨数组步幅删除?
【问题讨论】:
-
那么,您可以接受这些步幅的 3D 输出,还是必须有 2D 输出?
标签: python arrays python-3.x numpy stride