Show Menu
Cheatography

Python - Time Series Cheat Sheet (DRAFT) by

Basic commands for time series in Python.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Imports

from statsmodels.tsa.holtwinters import ExponentialSmoothing

Steps to fit the model and check it

train = df.ilo­c[:n]
use iloc to split the original dataset
fitted_mod = Expone­nti­alS­moo­thi­ng(­train,
trend='mul or add',
seasonal='mul or add',
seasonal_periods=n_unit).fit()
create and fit the model
predic­tions = fitted­_mo­d.f­ore­cast(n of units)
forecast
train.p­lot()
test.plot()
predictions.plot()
plot forecasted values together with train and test data

Evaluation metrics

from sklear­n.m­etrics import mean_s­qua­red­_error, mean_a­bso­lut­e_error
import the necessary libraries
mean_s­qua­red­_er­ror­(test, predic­tions)
calculate the MSE
np.sqr­t(m­ean­_sq­uar­ed_­err­or(­test, predic­tions))
calculate the RMSE
now that we saw our model was not that far off (if that's the case), we retrain our model on the entire dataset and we can plot it to show the future behaviour of our data
 

IMPORTANT CONCEPTS

STATIONARY data: these kinds of data do not exhibit trends or season­ality.

NON-ST­ATI­ONARY data: these kinds of data exhibit trends or season­ality.

- stationary data

- non stationary data

- via code...

from statmo­del­s.t­sa.s­ta­tes­pac­e.tools import diff
import libraries
diff(d­f["t­ime­series col"], k_diff=1)
use the diff() func to check statio­narity