numpy stride_tricks.as_strided vs滚动窗口的列表理解 [英] numpy stride_tricks.as_strided vs list comprehension for rolling window

查看:341
本文介绍了numpy stride_tricks.as_strided vs滚动窗口的列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When dealing with rolling windows, I wrote my functions in the way like list comprehension

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

最近我发现了 numpy.lib.stride_tricks.as_strided ,发现它广泛用于滚动窗口(例如,

Recently I discovered numpy.lib.stride_tricks.as_strided and found it is used widely for rolling windows (for example, this post), even though it is a "hidden" function.

此问题中,有关为何未记录stride_tricks.as_strided的内容,提到

In this issue concerning why stride_tricks.as_strided is undocumented, it's mentioned that

故意!这很危险!只是低级管道即可帮助实现broadcast_arrays().

Intentionally! It's dangerous! It was just low-level plumbing to help implement broadcast_arrays().

stride_tricks.as_strided 是否比列表理解或for循环有任何优势?我看了 stride_tricks的源代码 ,但收益不大.

Is there any advantage for stride_tricks.as_strided over the list comprehension or a for loop? I had a look at the source code of stride_tricks but gained little.

推荐答案

来自 此帖子 ,我们可以使用 strided_app 基本上将滑动视图获取到数组中,并且还可以指定hopsize/stepsize.然后,我们只需沿第二个轴使用 np.std 即可获得最终输出,就像这样-

From this post, we can use strided_app to basically get sliding views into the array and it also allows us to specify the hopsize/stepsize. Then, we simply use np.std along the second axis for the final output, like so -

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

运行示例以进行验证-

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]) \
            for i 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 a view into the input array, these strided operations must be really efficient. Let's find it out!

运行时测试

循环方法-

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

时间-

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

因此,要通过 stride 来回答效率问题,时间安排应该有助于证明这一点!

So, to answer the question on efficiency with strides, the timings should help prove a point there!

这篇关于numpy stride_tricks.as_strided vs滚动窗口的列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆