Step-by-step implementation
Importing required modules
At first, we will import all required Python modules like Pandas, NumPy, Matplotlib and Seaborn etc.
Python3
import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns from statsmodels.tsa.seasonal import seasonal_decompose |
Dataset loading and visualization
Now we will load a time-series dataset from Kaggle. Then we will visualize the raw data.
Python3
# Load the dataset data = pd.read_csv( 'AirPassengers.csv' ) data[ 'Month' ] = pd.to_datetime(data[ 'Month' ], format = '%Y-%m' ) data.set_index( 'Month' , inplace = True ) # Plot the original time series data plt.figure(figsize = ( 7 , 5 )) plt.plot(data, label = 'Original Time Series' ) plt.title( 'Air Passengers Time Series' ) plt.xlabel( 'Year' ) plt.ylabel( 'Number of Passengers' ) plt.legend() plt.show() |
Output:
Data decomposition
As we have already got the time-series plot, now we will decompose it to the trend, seasonal and residual components. To do this we need to specify some of the parameters of seasonal decompose function which are listed below–>
- data: This parameter represents the time series data that we want to decompose which is should be in a pandas Data Frame or Series with a datetime index.
- model: This parameter specifies the type of decomposition to be performed which can take two values ‘additive’ or ‘multiplicative’. Here we will use ‘multiplicative’ model as we can see the amplitude of seasonal component is relatively constant(means the pattern is constant) across different levels of the time series. In a multiplicative model, the seasonal and trend components are multiplied rather than added(Additive model).
- extrapolate_trend: This parameter controls whether to extrapolate the trend component to cover missing values at the end of the time series. Here we will set it to
'freq'
means that the trend component is extrapolated using the frequency of the time series. Extrapolating the trend can be useful when there are missing values at the end of the time series.
Python3
# Decompose the time series into trend, seasonal and residual components result = seasonal_decompose(data, model = 'multiplicative' , extrapolate_trend = 'freq' ) result.plot() plt.suptitle( 'Seasonal Decomposition of Air Passengers Time Series' ) plt.tight_layout() plt.show() |
Output:
Visualizing the seasonality
Now we will visualize the only seasonal component by extracting it from the decomposition results.
Python3
# Plot the seasonal component plt.figure(figsize = ( 6 , 4 )) plt.plot(result.seasonal, label = 'Seasonal Component' ) plt.title( 'Seasonal Component of Air Passengers Time Series' ) plt.xlabel( 'Year' ) plt.ylabel( 'Seasonal Component' ) plt.legend() plt.show() |
Output:
Removing seasonality from the data
To use a time-series data for various purposes including model training it is required to have a seasonality free time-series data. Here we will visualize how organized it will look after removing the seasonality.
Python3
# Plotting the original data and original data without the seasonal component plt.figure(figsize = ( 7 , 4 )) # Plot the original time series data plt.plot(data, label = 'Original Time Series' , color = 'blue' ) data_without_seasonal = data[ '#Passengers' ] / result.seasonal # Plot the original data without the seasonal component plt.plot(data_without_seasonal, label = 'Original Data without Seasonal Component' , color = 'green' ) plt.title( 'Air Passengers Time Series with and without Seasonal Component' ) plt.xlabel( 'Year' ) plt.ylabel( 'Number of Passengers' ) plt.legend() plt.show() |
Output:
From the plot we can see that after removing seasonality the time-series data became very organized which required for model training for any further purposes.
Seasonality Detection in Time Series Data
Time series analysis is a fundamental area of study in statistics and data science that provides a powerful framework for understanding and predicting patterns in sequential data. Time series data, in particular, captures information over successive intervals of time, which allows analysts to uncover trends, seasonal patterns, and other temporal dependencies. Among the various aspects of time series analysis, the detection of seasonality plays a crucial role in revealing recurring patterns within the data. In this article, we will detect seasonality in time-series data and remove it from the data, which will make the time-series data more suitable for model training.