Moving average or running mean

后端 未结 27 1049
庸人自扰
庸人自扰 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 09:16

    All the aforementioned solutions are poor because they lack

    • speed due to a native python instead of a numpy vectorized implementation,
    • numerical stability due to poor use of numpy.cumsum, or
    • speed due to O(len(x) * w) implementations as convolutions.

    Given

    import numpy
    m = 10000
    x = numpy.random.rand(m)
    w = 1000
    

    Note that x_[:w].sum() equals x[:w-1].sum(). So for the first average the numpy.cumsum(...) adds x[w] / w (via x_[w+1] / w), and subtracts 0 (from x_[0] / w). This results in x[0:w].mean()

    Via cumsum, you'll update the second average by additionally add x[w+1] / w and subtracting x[0] / w, resulting in x[1:w+1].mean().

    This goes on until x[-w:].mean() is reached.

    x_ = numpy.insert(x, 0, 0)
    sliding_average = x_[:w].sum() / w + numpy.cumsum(x_[w:] - x_[:-w]) / w
    

    This solution is vectorized, O(m), readable and numerically stable.

提交回复
热议问题