Skip to content

Instantly share code, notes, and snippets.

@mvervuurt
mvervuurt / ts_seasons_plot.py
Created August 29, 2019 13:30
Plotting horizontal lines to identify seasonality
ax = df1['total'].plot(figsize=(16,5),title=title)
ax.autoscale(axis='x',tight=True)
ax.set(xlabel=xlabel, ylabel=ylabel)
for x in df1.query('holiday==1').index:
ax.axvline(x=x, color='k', alpha = 0.3);
@mvervuurt
mvervuurt / pd_dt.py
Created August 29, 2019 10:33
Pandas datetime
import pandas as pd
#really neat trick to create a Pandas datetime from several columns
df['date']=pd.to_datetime(dict(year=df['year'], month=df['month'], day=1))
@mvervuurt
mvervuurt / print_adfuller.py
Created August 29, 2019 05:19
Nice print of augmented dicky fuller test
dfout = pd.Series(dftest[0:4],index=['ADF test statistic','p-value','# lags used','# observations'])
for key,val in dftest[4].items():
dfout[f'critical value ({key})']=val
print(dfout)
@mvervuurt
mvervuurt / ts_forecasting.py
Created August 28, 2019 13:41
Python Time Series Forecasting
# Load specific forecasting tools
from statsmodels.tsa.ar_model import AR,ARResults
model = AR(train['PopEst'])
AR1fit = model.fit(maxlag=1,method='mle')
start=len(train)
end=len(train)+len(test)-1
predictions1 = AR1fit.predict(start=start, end=end, dynamic=False).rename('AR(1) Predictions')
@mvervuurt
mvervuurt / ts_cov_corr.py
Created August 28, 2019 11:44
Python statsmodels autocovariance, autocorrelation and partial autocorrelation
# Remember that there are slightly different formulas for weakly stationary and strictly stationary time series
from statsmodels.tsa.stattools import acovf,acf,pacf,pacf_yw,pacf_ols
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
# Lag plots
from pandas.plotting import lag_plot
lag_plot(df1['Thousands of Passengers']);
@mvervuurt
mvervuurt / filter_warnings.py
Created August 28, 2019 11:01
Python filter out your warnings in Jupyter notebook ;)
import warnings
warnings.filterwarnings("ignore")
@mvervuurt
mvervuurt / ts_tests.py
Last active August 29, 2019 05:47
Python statsmodels Time Series Tests
# Ljung-Box test for no autocorrelation
statsmodels.stats.diagnostic.acorr_ljungbox()
# Augmented Dickey-Fuller unit root test
statsmodels.tsa.stattools.adfuller()
# Kwiatkowski-Phillips-Schmidt-Shin test for stationarity.
statsmodels.tsa.stattools.kpss()
from statsmodels.tsa.stattools import ccovf,ccf,periodogram
@mvervuurt
mvervuurt / sklearn_eval_metrics.py
Last active August 28, 2019 08:48
SKLearn Handy Evaluation Metrics
import numpy as np
from sklearn.metrics import mean_squared_error,mean_absolute_error
mean_absolute_error(test_data,test_predictions)
mean_squared_error(test_data,test_predictions)
np.sqrt(mean_squared_error(test_data,test_predictions))
@mvervuurt
mvervuurt / ts_ets_decomposition.py
Last active December 4, 2020 21:36
Python Time Series ETS Decomposition
# Assume Pandas DataFrame or Series
#moving average
df.ts_col.rolling(window=6).mean() #example 6 months rolling window
df.ts_col.rolling(window=12).mean() #example 12 months rolling window
#exponentially weighted moving average
df.ts_col.ewm(spane=12).mean()
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
@mvervuurt
mvervuurt / sample_statsmodels.py
Created August 27, 2019 10:13
Statsmodels datasets and using sm datetools
import pandas as pd
import statsmodels.api as sm
df = sm.datasets.macrodata.load_pandas().data
df.index = pd.Index(sm.tsa.datetools.dates_from_range('1959Q1', '2009Q3'))
print(sm.datasets.macrodata.NOTE)