python - numpy stride_tricks.as_strided vs list comprehension for rolling window -


when dealing rolling windows, wrote functions in way list comprehension

[np.std(x[i:i+framesize]) in range(0, len(x)-framesize, hopsize)])] 

recently discovered numpy.lib.stride_tricks.as_strided , found used rolling windows (for example, this post), though "hidden" function.

in this issue concerning why stride_tricks.as_strided undocumented, it's mentioned

intentionally! it's dangerous! low-level plumbing implement broadcast_arrays().

is there advantage stride_tricks.as_strided on list comprehension or loop? had @ the source code of stride_tricks gained little.

from this post, can use strided_app sliding views array , allows specify hopsize/stepsize. then, use np.std along second axis final output, -

np.std(strided_app(x, framesize, hopsize), axis=1) 

sample run verification -

in [162]: x = np.random.randint(0,9,(11))  in [163]: framesize = 5  in [164]: hopsize = 3  in [165]: np.array([np.std(x[i:i+framesize]) \             in range(0, len(x)-framesize+1, hopsize)]) out[165]: array([ 1.62480768,  2.05912603,  1.78885438])  in [166]: np.std(strided_app(x, framesize, hopsize), axis=1) out[166]: array([ 1.62480768,  2.05912603,  1.78885438]) 

being view input array, these strided operations must efficient. let's find out!

runtime test

loopy approach -

def loopy_app(x, framesize, hopsize):     return [np.std(x[i:i+framesize]) \         in range(0, len(x)-framesize+1, hopsize)] 

timings -

in [185]: x = np.random.randint(0,9,(1001))  in [186]: framesize = 5  in [187]: hopsize = 3  in [188]: %timeit loopy_app(x, framesize, hopsize) 10 loops, best of 3: 17.8 ms per loop  in [189]: %timeit np.std(strided_app(x, framesize, hopsize), axis=1) 10000 loops, best of 3: 111 µs per loop 

so, answer question on efficiency strides, timings should prove point there!


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -