avatarBilly Bonaros

Summary

This context provides a tutorial on time series decomposition using Python, focusing on splitting a time series into trend, seasonality, and noise components using the seasonal_decompose function from statsmodels.

Abstract

In this tutorial, the concept of time series decomposition is explained, which involves breaking down a time series into trend, seasonality, and noise components. The tutorial uses the Air Passengers Data from Kaggle as an example and demonstrates how to decompose the time series using Python's statsmodels library. The seasonal_decompose function is used with either an additive or multiplicative model, depending on whether the trend and seasonal variation are relatively constant over time. The tutorial also explains how to plot each component and all components together.

Opinions

  • Time series decomposition is a useful technique for analyzing time series data and understanding its underlying patterns.
  • The statsmodels library in Python provides a convenient function for performing time series decomposition.
  • The choice between an additive and multiplicative model for time series decomposition depends on the nature of the trend and seasonal variation in the data.
  • Visualizing each component of a time series can provide valuable insights into its behavior over time.
  • Plotting all components together can help in understanding the overall trend and seasonality in the data.
  • The tutorial uses the Air Passengers Data from Kaggle as an example, which is a popular dataset for time series analysis.
  • The author encourages readers to follow them on Medium and LinkedIn and to support them by signing up for Medium using their referral link.

Time Series Decomposition In Python

Time Series Analysis Made Easy

Image by Author

Time series decomposition is a technique that splits a time series into several components, each representing an underlying pattern category, trend, seasonality, and noise. In this tutorial, we will show you how to automatically decompose a time series with Python.

To begin with, let's talk a bit about the components of a time series:

Seasonality: describes the periodic signal in your time series. Trend: describes whether the time series is decreasing, constant, or increasing over time. Noise: describes what remains behind the separation of seasonality and trend from the time series. In other words, it’s the variability in the data that cannot be explained by the model.

For this example, we will use the Air Passengers Data from Kaggle.

import pandas as pd
import numpy as np
from statsmodels.tsa.seasonal import seasonal_decompose
 
#https://www.kaggle.com/rakannimer/air-passengers
df=pd.read_csv(‘AirPassengers.csv’)
 
df.head()
Image by Author

Firstly, we need to set as index the Month column and convert it into Datetime Object.

df.set_index('Month',inplace=True)
df.index=pd.to_datetime(df.index)
#drop null values
df.dropna(inplace=True)
df.plot()
Image by Author

The Decomposition

We will use Pythons statsmodels function seasonal_decompose.

result=seasonal_decompose(df['#Passengers'], model='multiplicable', period=12)

In seasonal_decompose we have to set the model. We can either set the model to be Additive or Multiplicative. A rule of thumb for selecting the right model is to see in our plot if the trend and seasonal variation are relatively constant over time, in other words, linear. If yes, then we will select the Additive model. Otherwise, if the trend and seasonal variation increase or decrease over time then we use the Multiplicative model.

Our data here are aggregated by month. The period we want to analyze is by year so that's why we set the period to 12.

We can get each component as follows:

result.seasonal.plot()

Image by Author

result.trend.plot()

Image by Author

Also, we can plot every component at once

result.plot()

Image by Author

Wrapping up

Frequently, when looking at time series data it’s difficult to manually extract the trend or identify the seasonality. Fortunately, we can automatically decompose a time series and helps us have a clearer view of the components as It’s easier to analyze the trend if we remove the seasonality from our data and vise versa.

Want More From Me?: Follow me on Medium • Add me on Linked In • Support me by signing up for Medium using my referral link .

Originally published at https://predictivehacks.com.

Time Series Analysis
Time Series Decompose
Python
Data Analysis
Data Science
Recommended from ReadMedium