Skip to content

Instantly share code, notes, and snippets.

@rtkilian
Last active March 19, 2024 15:41
Show Gist options
  • Save rtkilian/b4c3a50d112f275f4cb6a64bbc70a191 to your computer and use it in GitHub Desktop.
Save rtkilian/b4c3a50d112f275f4cb6a64bbc70a191 to your computer and use it in GitHub Desktop.
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction
from sktime.utils.plotting import plot_series
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from xgboost import XGBRegressor
# Create an exogenous dataframe indicating the month
X = pd.DataFrame({'month': y.index.month}, index=y.index)
X = pd.get_dummies(X.astype(str), drop_first=True)
# Split data
y_train, y_test = temporal_train_test_split(y, test_size=26) # Predict from 1st July 2019
X_train, X_test = temporal_train_test_split(X, test_size=26)
# Forecasting horizon, same as test data
fh = ForecastingHorizon(y_test.index, is_relative=False)
# XGBoost forecaster
regressor = XGBRegressor(objective='reg:squarederror', random_state=42)
forecaster = make_reduction(regressor, window_length=52, strategy="recursive")
# Fit and predict
forecaster.fit(y=y_train, X=X_train)
y_pred = forecaster.predict(fh=fh, X=X_test)
# Plot predictions with training and test data
plot_series(y_train['2018-07-01':], y_test, y_pred, labels=["y_train", "y_test", "y_pred"], x_label='Date', y_label='Count pedestrians');
# Evaluate
print('MAPE: %.4f' % mean_absolute_percentage_error(y_test, y_pred, symmetric=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment