2017-08-12 5 views
1

私は配列がありません。ローリングウィンドウが必要です [1,2,3,4,5,6] サブアレイ長3の予想結果: [1,2,3] [2,3,4] [3,4,5] [4,5,6] 助けてください。私はPython開発者ではありません。Pythonのローリングウィンドウ

のPython 3.5

答えて

2

numpyが必要でない場合は、あなただけのリストの内包表記を使用することができます。 xがあなたの配列である場合には、:

In [102]: [x[i: i + 3] for i in range(len(x) - 2)] 
Out[102]: [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]] 

また

np.lib.stride_tricksを使用。機能rolling_windowを定義します(this blogからソース):

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) 

コールwindow=3と機能:

In [122]: rolling_window(x, 3) 
Out[122]: 
array([[1, 2, 3], 
     [2, 3, 4], 
     [3, 4, 5], 
     [4, 5, 6]]) 
関連する問題