Moving average or running mean

后端 未结 27 1373
庸人自扰
庸人自扰 2020-11-22 08:37

Is there a SciPy function or NumPy function or module for Python that calculates the running mean of a 1D array given a specific window?

27条回答
  •  深忆病人
    2020-11-22 08:56

    Update: The example below shows the old pandas.rolling_mean function which has been removed in recent versions of pandas. A modern equivalent of the function call below would be

    In [8]: pd.Series(x).rolling(window=N).mean().iloc[N-1:].values
    Out[8]: 
    array([ 0.49815397,  0.49844183,  0.49840518, ...,  0.49488191,
            0.49456679,  0.49427121])
    

    pandas is more suitable for this than NumPy or SciPy. Its function rolling_mean does the job conveniently. It also returns a NumPy array when the input is an array.

    It is difficult to beat rolling_mean in performance with any custom pure Python implementation. Here is an example performance against two of the proposed solutions:

    In [1]: import numpy as np
    
    In [2]: import pandas as pd
    
    In [3]: def running_mean(x, N):
       ...:     cumsum = np.cumsum(np.insert(x, 0, 0)) 
       ...:     return (cumsum[N:] - cumsum[:-N]) / N
       ...:
    
    In [4]: x = np.random.random(100000)
    
    In [5]: N = 1000
    
    In [6]: %timeit np.convolve(x, np.ones((N,))/N, mode='valid')
    10 loops, best of 3: 172 ms per loop
    
    In [7]: %timeit running_mean(x, N)
    100 loops, best of 3: 6.72 ms per loop
    
    In [8]: %timeit pd.rolling_mean(x, N)[N-1:]
    100 loops, best of 3: 4.74 ms per loop
    
    In [9]: np.allclose(pd.rolling_mean(x, N)[N-1:], running_mean(x, N))
    Out[9]: True
    

    There are also nice options as to how to deal with the edge values.

提交回复
热议问题