Moving average or running mean

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

    Instead of numpy or scipy, I would recommend pandas to do this more swiftly:

    df['data'].rolling(3).mean()
    

    This takes the moving average (MA) of 3 periods of the column "data". You can also calculate the shifted versions, for example the one that excludes the current cell (shifted one back) can be calculated easily as:

    df['data'].shift(periods=1).rolling(3).mean()
    

提交回复
热议问题