您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将Python序列(时间序列/数组)拆分为具有重叠的子序列

将Python序列(时间序列/数组)拆分为具有重叠的子序列

这比您的机器上的快速版本快34倍:

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

>>> rolling_window(ts.values, 3)
array([[0, 1, 2],
      [1, 2, 3],
      [2, 3, 4],
      [3, 4, 5],
      [4, 5, 6],
      [5, 6, 7],
      [6, 7, 8],
      [7, 8, 9]])

幸得埃里克Rigtorp

python 2022/1/1 18:44:26 有563人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶