Seasonality, trend and noise in time series data

Visualizing Time Series Data in Python

Thomas Vincent

Head of Data Science, Getty Images

Properties of time series

Properties of time series

Visualizing Time Series Data in Python

The properties of time series

  • Seasonality: does the data display a clear periodic pattern?
  • Trend: does the data follow a consistent upwards or downwards slope?
  • Noise: are there any outlier points or missing values that are not consistent with the rest of the data?
Visualizing Time Series Data in Python

Time series decomposition

import statsmodels.api as sm
import matplotlib.pyplot as plt
from pylab import rcParams

rcParams['figure.figsize'] = 11, 9
decomposition = sm.tsa.seasonal_decompose(
                co2_levels['co2'])
fig = decomposition.plot()

plt.show()
Visualizing Time Series Data in Python

A plot of time series decomposition on the CO2 data

Time series decomposition

Visualizing Time Series Data in Python

Extracting components from time series decomposition

print(dir(decomposition))
['__class__', '__delattr__', '__dict__',
 ... 'plot', 'resid', 'seasonal', 'trend']
print(decomposition.seasonal)
datestamp
1958-03-29    1.028042
1958-04-05    1.235242
1958-04-12    1.412344
1958-04-19    1.701186
Visualizing Time Series Data in Python

Seasonality component in time series

decomp_seasonal = decomposition.seasonal

ax = decomp_seasonal.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Seasonality of time series')
ax.set_title('Seasonal values of the time series')

plt.show()
Visualizing Time Series Data in Python

Seasonality component in time series

Seasonality in time series

Visualizing Time Series Data in Python

Trend component in time series

decomp_trend = decomposition.trend

ax = decomp_trend.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')

plt.show()
Visualizing Time Series Data in Python

Trend component in time series

Trend in time series

Visualizing Time Series Data in Python

Noise component in time series

decomp_resid = decomp.resid

ax = decomp_resid.plot(figsize=(14, 2))
ax.set_xlabel('Date')
ax.set_ylabel('Residual of time series')
ax.set_title('Residual values of the time series')

plt.show()
Visualizing Time Series Data in Python

Noise component in time series

Noise in time series

Visualizing Time Series Data in Python

Let's practice!

Visualizing Time Series Data in Python

Preparing Video For Download...