2021-Nov-18, Thursday
I found a tutorial on time series analsyis (TSA), which uses the Python library statsmodels
to decompose a time series (TS). I thought it'll be fun to play around with it a bit.
Here's the link to the tutorial: https://towardsdatascience.com/time-series-decomposition-in-python-8acac385a5b2
In TSA it is useful to split a TS into its different components representing trend, seasonality, and noise.
Trend: movement of time series over time; can be increasing, decreasing, or constant.
Seasonality: periodic part of the time series.
Noise: what remains behind after removing trend and seasonality. This is the variability in the time series that cannot be explained by the model.
import numpy as np
import pandas as pd
from statsmodels.tsa.seasonal import seasonal_decompose
df = pd.read_csv("AirPassengers.csv")
df.head()
Month | #Passengers | |
---|---|---|
0 | 1949-01 | 112 |
1 | 1949-02 | 118 |
2 | 1949-03 | 132 |
3 | 1949-04 | 129 |
4 | 1949-05 | 121 |
# Set the "Month" column as index, and convert format to Datetime:
df.set_index("Month", inplace=True)
df.index = pd.to_datetime(df.index)
df.head()
#Passengers | |
---|---|
Month | |
1949-01-01 | 112 |
1949-02-01 | 118 |
1949-03-01 | 132 |
1949-04-01 | 129 |
1949-05-01 | 121 |
# Check for null values:
df.isnull().sum()
#Passengers 0 dtype: int64
df.plot()
<AxesSubplot:xlabel='Month'>
ts_decomposed = seasonal_decompose(df["#Passengers"], model="multiplicative", period=12)
ts_decomposed.seasonal.plot()
<AxesSubplot:xlabel='Month'>
ts_decomposed.trend.plot()
<AxesSubplot:xlabel='Month'>
ts_decomposed.resid.plot()
<AxesSubplot:xlabel='Month'>