Use convolve.
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,10,100)
yorg=np.sin(x)
y=np.sin(x)+np.random.randn(100)*0.2
num=5.0#Number of moving averages
b=np.ones(num)/num
y2=np.convolve(y, b, mode='same')#moving average
plt.plot(x,yorg,'r',label='Original sin')
plt.plot(x,y,'k-',label='Original series')
plt.plot(x,y2,'b--', label='moving average')
plt.legend()
result
Recommended Posts