考虑以下数据:
data = np.array([[i for i in range(3)] for _ in range(9)])
print(data)
print(f'data has shape {data.shape}')
[[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]
[0 1 2]]
data has shape (9, 3)
还有一个参数,我们称之为history
。history的功能是,它在第一维上堆叠history
许多数组[0 1 2]
。作为一个示例,考虑使用history=2
对该过程进行一次迭代
history = 2
data = np.array([[[0, 1, 2], [0, 1, 2]]])
print(f'data has now shape {data.shape}')
data has now shape (1, 2, 3)
现在,让我们考虑两个迭代:
history = 2
data = np.array([[[0, 1, 2], [0, 1, 2]],[[0, 1, 2], [0, 1, 2]]])
print(f'data has now shape {data.shape}')
data has now shape (2, 2, 3)
应重复此过程,直到数据完全处理。这意味着,我们可能会在最后丢失一些数据,因为data.shape[0]/history % 2 != 0
。因此,history=2
的最终结果为
([[[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2]]])
如何才能做到这一点的性能?
1条答案
按热度按时间xoshrz7s1#
如果我没理解错的话,你可以先切片,然后再整形:
输出: