We can note that both Simple average and Weighted moving average lie on completely opposite ends. We would need something between these two extremes approaches which takes into account all the data while weighing the data points differently. It can be best done by exponential smoothing.
So this recipe is a short example on forecast a time series using exponential smoothing. Let's get started.
import numpy as np import pandas as pd from statsmodels.tsa.arima_model import ARIMA from statsmodels.tsa.holtwinters import SimpleExpSmoothing
Let's pause and look at these imports. Numpy and pandas are general ones. Here statsmodels.tsa.holtwinters is used to import SimpleExpSmoothing library for building of model.
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']) df.head()
Here, we have used one time series data from github.
Now our dataset is ready.
train_data = df[1:len(df)-12] test_data = df[len(df)-12:]
Here we have simply split data into size of 12 and rest elements
model = SimpleExpSmoothing(train_data.value) model_fitted = model.fit()
A simple model of exponential smoothing has been created in here for building our model on train values.
print('coefficients',model_fitted.params) predictions = model_fitted.predict(start=len(train_data), end=len(train_data) + len(test_data)-1) print(predictions)
Here, we have printed the coeffiecient of model and the predicted values.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the output.