Selected topic
Seasonality Detection
Prefer practical output? Use related tools below while reading.
Seasonality detection is an essential step in exploratory data analysis to identify recurring patterns or cycles in time series data. It helps you understand whether your data exhibits regular fluctuations over a specific period, such as daily, weekly, monthly, quarterly, or yearly.
python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose# Generate sample time series data with seasonality
np.random.seed(0)
n = 100
t = np.arange(n)
y = np.sin(2 np.pi t / 12) + np.sin(2 np.pi t / 6) + 10 + np.random.randn(n)
# Create a Pandas DataFrame with the time series data
df = pd.DataFrame(y, index=t, columns=['values'])
# Plot the original time series
plt.figure(figsize=(10, 6))
plt.plot(df.index, df['values'])
plt.title('Original Time Series')
plt.show()
# Perform seasonal decomposition using Statsmodels
decomposition = seasonal_decompose(df['values'], model='additive')
trend = decomposition.trend
seasonal = decomposition.seasonal
residual = decomposition.resid
# Plot the decomposed components
fig, axes = plt.subplots(4, 1, figsize=(10, 12))
axes[0].plot(trend)
axes[0].set_title('Trend')
axes[1].plot(seasonal)
axes[1].set_title('Seasonality')
axes[2].plot(residual)
axes[2].set_title('Residuals')
plt.show()
In this example, we generate a sample time series with seasonality (daily and weekly cycles) and apply seasonal decomposition using Statsmodels. The resulting plots reveal the trend, seasonality, and residuals of the original data.