用numpy滚动最大

Bas*_*asj 3 python numpy

这将计算A一个长度为的滑动窗口上的“滚动最大值” (类似于滚动平均值)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)])
Run Code Online (Sandbox Code Playgroud)

但我认为,就性能而言,这远非最佳。

我知道该pandas库具有rolling_max,但是在我的项目中,我不想使用这种新的依赖关系。

问题:是否有一种简单的方法仅使用numpy计算滚动最大值?

Bas*_*asj 6

The solution is totally similar to Divakar's answer here (full credit to him) but the final cropping of the array has different indices in this context:

maximum_filter1d(A, size=K)[K//2:-((K+1)//2)]
Run Code Online (Sandbox Code Playgroud)

Example:

import numpy as np
from scipy.ndimage.filters import maximum_filter1d
A = np.random.randint(0, 10, (50))
K = 5
rollingmax = np.array([max(A[j-K:j]) for j in range(K,len(A))])
rollingmax2 = np.array([max(A[j:j+K]) for j in range(len(A)-K)])
rollingmax3 = maximum_filter1d(A,size=K)[K//2:-((K+1)//2)]
print A, rollingmax, rollingmax2, rollingmax3
Run Code Online (Sandbox Code Playgroud)

[6 7 7 9 4 5 4 7 2 0 3 3 5 9 4 6 6 1 5 2 7 5 7 7 5 6 0 9 0 5 9 3 7 1 9 5 3 7 5 1 6 9 6 0 5 1 5 5 4 9]
[9 9 9 9 7 7 7 7 5 9 9 9 9 9 6 6 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 7 7 9 9 9 9 9 6 5 5]
[9 9 9 9 7 7 7 7 5 9 9 9 9 9 6 6 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 7 7 9 9 9 9 9 6 5 5]
[9 9 9 9 7 7 7 7 5 9 9 9 9 9 6 6 7 7 7 7 7 7 7 9 9 9 9 9 9 9 9 9 9 9 9 7 7 9 9 9 9 9 6 5 5]


mj *_*eri 6

我想这个小动作使用stridesas_strided将做的工作:

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)
Run Code Online (Sandbox Code Playgroud)

出于比较目的,我根据您的算法定义了另一个函数:

def max_rolling2(A,K):
    rollingmax = np.array([max(A[j:j+K]) for j in range(len(A)-K)])
    return rollingmax
Run Code Online (Sandbox Code Playgroud)

timeit我的笔记本电脑上的比较是:

与:

A = np.random.rand(100000)
K = 10


%timeit X = max_rolling2(A,K)
Run Code Online (Sandbox Code Playgroud)
170 ms ± 19.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Run Code Online (Sandbox Code Playgroud)
%timeit X = max_rolling1(A,K)
Run Code Online (Sandbox Code Playgroud)
> 3.75 ms ± 479 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Run Code Online (Sandbox Code Playgroud)