Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_16/solution-code/04_decomposition_solutions.ipynb
1904 views
Kernel: Python 2

Time Series: Decomposition

Independent Practice

Instructor Note: These are optional and can be assigned as student practice questions outside of class.

1) Import the Airline Passengers data set, preprocess the data, and plot the raw time series.

import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np import datetime from dateutil.relativedelta import * %matplotlib inline
airline = pd.read_csv('./data/airline.csv') airline.head()
airline.tail()
airline.drop(airline.index[144], inplace=True)
airline.columns= ['index','passengers']
airline['passengers'] = airline.passengers.apply(lambda x: int(x))
airline.set_index(['index'], inplace=True) airline.index.name=None airline.head()
airline.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x1a11a9fe48>
Image in a Jupyter notebook

2) Decompose the time series and plot using the .seasonal_decompose() function.

from statsmodels.tsa.seasonal import seasonal_decompose
decomposition = seasonal_decompose(airline, freq = 12)
decomposition.plot() plt.show()
Image in a Jupyter notebook

3) Interpret these plots.

# There is a clear upward trend in airline passengers. There is a strong seasonal component.