numpy stride_tricks.as_strided vs列表理解滚动窗口

Lee*_*Lee 5 python arrays numpy stride

在处理滚动窗口时,我以列表理解的方式编写了函数

[np.std(x[i:i+framesize]) for i in range(0, len(x)-framesize, hopsize)])]
Run Code Online (Sandbox Code Playgroud)

最近,我发现numpy.lib.stride_tricks.as_strided并发现它被广泛用于滚动窗口(例如,这篇文章),即使它是“隐藏”功能。

在有关为何未记录stride_tricks.as_strided的问题中,提到了

故意地!这很危险!帮助实现broadcast_arrays()只是底层管道。

stride_tricks.as_strided列表理解或for循环有什么优势吗?我看了的源代码,stride_tricks但收获甚微。

Div*_*kar 3

this post,我们 strided_app基本上可以使用将滑动视图放入数组中,它还允许我们指定跳跃大小/步长。然后,我们只需np.std沿第二个轴使用即可获得最终输出,如下所示 -

\n\n
np.std(strided_app(x, framesize, hopsize), axis=1)\n
Run Code Online (Sandbox Code Playgroud)\n\n

运行样品进行验证 -

\n\n
In [162]: x = np.random.randint(0,9,(11))\n\nIn [163]: framesize = 5\n\nIn [164]: hopsize = 3\n\nIn [165]: np.array([np.std(x[i:i+framesize]) \\\n            for i in range(0, len(x)-framesize+1, hopsize)])\nOut[165]: array([ 1.62480768,  2.05912603,  1.78885438])\n\nIn [166]: np.std(strided_app(x, framesize, hopsize), axis=1)\nOut[166]: array([ 1.62480768,  2.05912603,  1.78885438])\n
Run Code Online (Sandbox Code Playgroud)\n\n

作为输入数组的视图,这些跨步操作必须非常高效。让我们来找出答案吧!

\n\n

运行时测试

\n\n

循环的方法 -

\n\n
def loopy_app(x, framesize, hopsize):\n    return [np.std(x[i:i+framesize]) \\\n        for i in range(0, len(x)-framesize+1, hopsize)]\n
Run Code Online (Sandbox Code Playgroud)\n\n

时间安排 -

\n\n
In [185]: x = np.random.randint(0,9,(1001))\n\nIn [186]: framesize = 5\n\nIn [187]: hopsize = 3\n\nIn [188]: %timeit loopy_app(x, framesize, hopsize)\n10 loops, best of 3: 17.8 ms per loop\n\nIn [189]: %timeit np.std(strided_app(x, framesize, hopsize), axis=1)\n10000 loops, best of 3: 111 \xc2\xb5s per loop\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,要回答有关效率的问题strides,时间安排应该有助于证明这一点!

\n