Moving average or running mean

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

    With @Aikude's variables, I wrote one-liner.

    import numpy as np
    
    mylist = [1, 2, 3, 4, 5, 6, 7]
    N = 3
    
    mean = [np.mean(mylist[x:x+N]) for x in range(len(mylist)-N+1)]
    print(mean)
    
    >>> [2.0, 3.0, 4.0, 5.0, 6.0]
    

提交回复
热议问题