A seasonality effect is a repeating pattern tied to a regular period, often a calendar interval such as a weekday, month position, or time of year. Unlike cycles, seasonal patterns are more regular and need not be annual. section 17.2.3
Seasonality changes what counts as unusual. A spike occurring every 10 seconds is expected, while its absence may be anomalous. For calendar data, compare observations with matching recent weekdays, month positions, or months from prior years. section 17.2.3
Two practical ways to account for seasonality are:
- Decompose the series into trend, seasonal pattern, and residuals, then assess anomalies in the residuals after larger regular effects are removed. section 17.2.3
- Reshape strongly seasonal data so each row represents one period and its features represent positions within that period; then apply multivariate tabular outlier detection. section 17.2.8
Listing 17.4 Using statsmodels for time-series decomposition
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.seasonal import seasonal_decompose
np.random.seed(0)
n_points = 2000
dates = np.array('2005-01-01', dtype=np.datetime64) + np.arange(n_points)
data = 12*np.sin(4 * np.pi * np.arange(n_points)/365) + \
50.0*np.array(range(n_points))/n_points + \
np.random.normal(12, 2, n_points)
df = pd.DataFrame({'Values': data}, index=dates)
result = seasonal_decompose(df, model='additive', period=365)
result.plot()
plt.show()
Inspect a Python decomposition using statsmodels, with an additive model and a 365-point period. section 17.2.3
Listing 17.12 Creating a dataframe for each period
weekly_arr = []
for i in range(0, len(df), 7): #1
week_df = df.iloc[i: i+7]
weekly_arr.append([i] + week_df['Values'].values.tolist())
weekly_df = pd.DataFrame(
weekly_arr,
columns=['Week', 'Mon', 'Tues', 'Wed', 'Thur',
'Fri', 'Sat', 'Sun'])
weekly_df = weekly_df.dropna()
weekly_df
Inspect a weekly dataframe that converts seven positions in each week into separate features for tabular detection. section 17.2.8







