Skip to content

Instantly share code, notes, and snippets.

@nphilou
Last active February 25, 2020 09:01
Show Gist options
  • Select an option

  • Save nphilou/14a46de1d020d580f6f2f79e93df9688 to your computer and use it in GitHub Desktop.

Select an option

Save nphilou/14a46de1d020d580f6f2f79e93df9688 to your computer and use it in GitHub Desktop.
from gtime import *
from gtime.feature_extraction import *
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
# Create random DataFrame with DatetimeIndex
X_dt = pd.DataFrame(np.random.randint(4, size=(20)),
index=pd.date_range("2019-12-20", "2020-01-08"),
columns=['time_series'])
# Convert the DatetimeIndex to PeriodIndex and create y matrix
X = preprocessing.TimeSeriesPreparation().transform(X_dt)
y = model_selection.horizon_shift(X, horizon=2)
# Create some features
cal = feature_generation.Calendar(region="europe", country="Switzerland", kernel=np.array([1, 2]))
X_f = compose.FeatureCreation(
[('s_2', Shift(2), ['time_series']),
('ma_3', MovingAverage(window_size=3), ['time_series']),
('cal', cal, ['time_series'])]).fit_transform(X)
# Train/test split
X_train, y_train, X_test, y_test = model_selection.FeatureSplitter().transform(X_f, y)
# Try some different forecasting models (TrendForecaster doesn't need computed features)
gar = forecasting.GAR(LinearRegression())
gar.fit(X_train, y_train).predict(X_test)
X_train, _, X_test, _ = model_selection.FeatureSplitter().transform(X, y)
tf = forecasting.TrendForecaster(trend='polynomial', trend_x0=np.zeros(3))
tf.fit(X_train).predict(X_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment