This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import statsmodels.api as sm | |
data = sm.datasets.sunspots.load_pandas() | |
ts_sun = data.data.set_index('YEAR').SUNACTIVITY | |
ts_sun.plot(figsize=(12, 5)) | |
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ts_sun_diff = (ts_sun - ts_sun.shift(1)).dropna() | |
tsplot(ts_sun_diff) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
import warnings | |
warnings.filterwarnings("ignore") | |
# adapted from https://www.kaggle.com/kashnitsky/topic-9-part-1-time-series-analysis-in-python?scriptVersionId=50985180&cellId=80 | |
def tsplot(y, lags=None, figsize=(12, 7)): | |
""" | |
Plot time series, its ACF and PACF, calculate Dickey–Fuller test | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_len = int(len(ts_sun) * 0.2) | |
sun_train, sun_test = ts_sun.iloc[:-test_len], ts_sun.iloc[-test_len:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sktime.forecasting.arima import AutoARIMA | |
forecaster = AutoARIMA(start_p=8, max_p=9, suppress_warnings=True) | |
sun_train.index = sun_train.index.astype(int) | |
forecaster.fit(sun_train) | |
forecaster.summary() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.metrics import mean_absolute_error | |
from sklearn.metrics import mean_absolute_percentage_error | |
def plot_forecast(series_train, series_test, forecast, forecast_int=None): | |
mae = mean_absolute_error(series_test, forecast) | |
mape = mean_absolute_percentage_error(series_test, forecast) | |
plt.figure(figsize=(12, 6)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fh = np.arange(test_len) + 1 | |
forecast, forecast_int = forecaster.predict(fh=fh, return_pred_int=True, alpha=0.05) | |
sun_arima_mae, sun_arima_mape = plot_forecast(sun_train, sun_test, forecast, forecast_int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sktime.forecasting.compose import make_reduction, TransformedTargetForecaster | |
from sktime.forecasting.model_selection import ExpandingWindowSplitter, ForecastingGridSearchCV | |
from sktime.performance_metrics.forecasting import MeanAbsolutePercentageError | |
import lightgbm as lgb | |
def create_forecaster(): | |
# creating forecaster with LightGBM | |
regressor = lgb.LGBMRegressor() | |
forecaster = make_reduction(regressor, window_length=5, strategy="recursive") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ts_nl = sm.datasets.get_rdataset("Nile").data | |
ts_nl = ts_nl.set_index('time').value | |
tsplot(ts_nl) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ts_nl_diff = (ts_nl - ts_nl.shift(1)).dropna() | |
tsplot(ts_nl_diff) |
OlderNewer