Moving average or running mean

后端 未结 27 1085
庸人自扰
庸人自扰 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条回答
  •  旧时难觅i
    2020-11-22 09:09

    There are many answers above about calculating a running mean. My answer adds two extra features:

    1. ignores nan values
    2. calculates the mean for the N neighboring values NOT including the value of interest itself

    This second feature is particularly useful for determining which values differ from the general trend by a certain amount.

    I use numpy.cumsum since it is the most time-efficient method (see Alleo's answer above).

    N=10 # number of points to test on each side of point of interest, best if even
    padded_x = np.insert(np.insert( np.insert(x, len(x), np.empty(int(N/2))*np.nan), 0, np.empty(int(N/2))*np.nan ),0,0)
    n_nan = np.cumsum(np.isnan(padded_x))
    cumsum = np.nancumsum(padded_x) 
    window_sum = cumsum[N+1:] - cumsum[:-(N+1)] - x # subtract value of interest from sum of all values within window
    window_n_nan = n_nan[N+1:] - n_nan[:-(N+1)] - np.isnan(x)
    window_n_values = (N - window_n_nan)
    movavg = (window_sum) / (window_n_values)
    

    This code works for even Ns only. It can be adjusted for odd numbers by changing the np.insert of padded_x and n_nan.

    Example output (raw in black, movavg in blue):

    This code can be easily adapted to remove all moving average values calculated from fewer than cutoff = 3 non-nan values.

    window_n_values = (N - window_n_nan).astype(float) # dtype must be float to set some values to nan
    cutoff = 3
    window_n_values[window_n_values

提交回复
热议问题