Time series decomposition involves thinking of a series as a combination of level, trend, seasonality, and noise components.
So this recipe is a short example on how to decompose a time series. Let's get started.
import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.seasonal import seasonal_decompose
Let's pause and look at these imports. Numpy and pandas are general ones. Here matplotlib.pyplot will help us in plotting. statsmodels.tsa.seasonal comes handy while analysing patterns.
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/a10.csv', parse_dates=['date']).set_index('date')
Here, we have used one time series data from github. Also, we have set our index to date.
Now our dataset is ready.
result = seasonal_decompose(df, model='additive')
We have simply created an object of seasonal_decompose to understand our model with additive feature.
print(result.trend) print(result.seasonal) print(result.resid) print(result.observed)
We are priting the decomposition of our dataset in here
Finally, we have tried to visualize trends in one go.
Once we run the above code snippet, we will see:
Scroll down the ipython file to visualize the output.