padding numpy rolling window operations using strides

一世执手 提交于 2019-12-24 02:23:55

问题


I have a function f that I would like to efficiently compute in a sliding window.

def efficient_f(x):
   # do stuff
   wSize=50
   return another_f(rolling_window_using_strides(x, wSize), -1)

I have seen on SO that is particularly efficient to do that using strides: from numpy.lib.stride_tricks import as_strided

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

Then I try to apply it on a df:

df=pd.DataFrame(data=np.random.rand(180000,1),columns=['foo'])
df['bar']=df[['foo']].apply(efficient_f,raw=True)
# note the double [[, otherwise pd.Series.apply
# (not accepting raw, and axis kwargs) will be called instead of pd.DataFrame.

It is working very nicely, and it indeed led to significant performance gains. However, I still get the following error:

ValueError: Shape of passed values is (1, 179951), indices imply (1, 180000).

This is because I am using wSize=50, which yields

rolling_window_using_strides(df['foo'].values,50).shape
(1L, 179951L, 50L)

Is there a way by zero/np.nan padding at the borders to get

(1L, 180000, 50L)

hence same size as the original vector


回答1:


Here's one way to solve it with np.lib.stride_tricks.as_strided -

def strided_axis0(a, fillval, L): # a is 1D array
    a_ext = np.concatenate(( np.full(L-1,fillval) ,a))
    n = a_ext.strides[0]
    strided = np.lib.stride_tricks.as_strided     
    return strided(a_ext, shape=(a.shape[0],L), strides=(n,n))

Sample run -

In [95]: np.random.seed(0)

In [96]: a = np.random.rand(8,1)

In [97]: a
Out[97]: 
array([[ 0.55],
       [ 0.72],
       [ 0.6 ],
       [ 0.54],
       [ 0.42],
       [ 0.65],
       [ 0.44],
       [ 0.89]])

In [98]: strided_axis0(a[:,0], fillval=np.nan, L=3)
Out[98]: 
array([[  nan,   nan,  0.55],
       [  nan,  0.55,  0.72],
       [ 0.55,  0.72,  0.6 ],
       [ 0.72,  0.6 ,  0.54],
       [ 0.6 ,  0.54,  0.42],
       [ 0.54,  0.42,  0.65],
       [ 0.42,  0.65,  0.44],
       [ 0.65,  0.44,  0.89]])


来源:https://stackoverflow.com/questions/47417420/padding-numpy-rolling-window-operations-using-strides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!