How to smooth time series using simple running mean method?

  Data, matplotlib, Numpy, Python

We often use running averages to remove noise from time series. Below is a simple python code to do this easily.

import numpy as np
from numpy import random
import matplotlib.pyplot as plt

# your data
data = random.random(size=(1,100))

# set window 
kernel_size = 10 # window width is 10

# set weight, you can use different weight, but the sum of weight =1
kernel = np.ones(kernel_size) / kernel_size
# smooth the data
smoothed_data = np.convolve(data[0,:], kernel, mode='same')

#check your result
plt.plot(data[0])
plt.plot(smoothed_data)
plt.show()