用numpy滚动最大 [英] Rolling maximum with numpy
本文介绍了用numpy滚动最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这将在长度为K
的滑动窗口上计算A
的滚动最大值"(类似于滚动平均值):
This computes the "rolling max" of A
(similar to rolling average) over a sliding window of length K
:
import numpy as np
A = np.random.rand(100000)
K = 10
rollingmax = np.array([max(A[j:j+K]) for j in range(len(A)-K)])
但是我认为在性能方面远非最佳.
but I think it is far from optimal in terms of performance.
I know that the pandas
library has rolling_max
, but in my project, I don't want to use this new dependance.
问题:是否有一种简单的方法可以仅使用numpy计算滚动最大值?
推荐答案
我猜想使用 as_strided
会完成这项工作:
I guess this little trick using strides
and as_strided
will do the job:
def max_rolling1(a, window,axis =1):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
rolling = np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
return np.max(rolling,axis=axis)
出于比较目的,我根据您的算法定义了另一个函数:
and for comaprison purpose I defined another function based on your algorithm :
def max_rolling2(A,K):
rollingmax = np.array([max(A[j:j+K]) for j in range(len(A)-K)])
return rollingmax
,并且在我的笔记本电脑上通过timeit
进行的比较是:
and the comparison by timeit
on my laptop is :
与:
A = np.random.rand(100000)
K = 10
%timeit X = max_rolling2(A,K)
170 ms ± 19.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit X = max_rolling1(A,K)
> 3.75 ms ± 479 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
这篇关于用numpy滚动最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文