Moving average or running mean

后端 未结 27 1325
庸人自扰
庸人自扰 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:54

    There is a comment by mab buried in one of the answers above which has this method. bottleneck has move_mean which is a simple moving average:

    import numpy as np
    import bottleneck as bn
    
    a = np.arange(10) + np.random.random(10)
    
    mva = bn.move_mean(a, window=2, min_count=1)
    

    min_count is a handy parameter that will basically take the moving average up to that point in your array. If you don't set min_count, it will equal window, and everything up to window points will be nan.

提交回复
热议问题