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 TransformedTargetForecaster | |
def create_forecaster_w_detrender(degree=1): | |
# creating forecaster with LightGBM | |
regressor = lgb.LGBMRegressor() | |
forecaster = TransformedTargetForecaster( | |
[ | |
("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=degree))), | |
( |
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.datasets import load_airline | |
ts_al = load_airline() | |
tsplot(ts_al) |
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_al_diff = (ts_al - ts_al.shift(12)).dropna() | |
tsplot(ts_al_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
ts_al_2diff = (ts_al_diff - ts_al_diff.shift(1)).dropna() | |
tsplot(ts_al_2diff) |
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_al) * 0.3) | |
al_train, al_test = ts_al.iloc[:-test_len], ts_al.iloc[-test_len:] | |
forecaster = AutoARIMA(sp=12, suppress_warnings=True) | |
forecaster.fit(al_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
fh = np.arange(test_len) + 1 | |
forecast = forecaster.predict(fh=fh) | |
forecast_int = forecaster.predict_interval(fh=fh, coverage=coverage)['Coverage'][coverage] | |
al_arima_mae, al_arima_mape = plot_forecast(al_train, al_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.transformations.series.detrend import Deseasonalizer | |
def create_forecaster_w_desesonalizer(sp=12, degree=1): | |
# creating forecaster with LightGBM | |
regressor = lgb.LGBMRegressor() | |
forecaster = TransformedTargetForecaster( | |
[ | |
("deseasonalize", Deseasonalizer(model="multiplicative", sp=sp)), | |
("detrend", Detrender(forecaster=PolynomialTrendForecaster(degree=degree))), |
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 fbprophet import Prophet | |
al_train_pp = al_train.to_timestamp(freq="M").reset_index() | |
# Prophet requires specific column names: ds and y | |
al_train_pp.columns = ["ds", "y"] | |
# turning on only yearly seasonality as this is monthly data. | |
# As the seasonality effects varies across years, we need multiplicative seasonality mode | |
m = Prophet( | |
seasonality_mode="multiplicative", |
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
class CustomGradientBoostingRegressor: | |
def __init__(self, learning_rate, n_estimators, max_depth=1): | |
self.learning_rate = learning_rate | |
self.n_estimators = n_estimators | |
self.max_depth = max_depth | |
self.trees = [] | |
def fit(self, X, y): | |
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.ensemble import GradientBoostingRegressor | |
from sklearn.metrics import mean_squared_error | |
custom_gbm = CustomGradientBoostingRegressor( | |
n_estimators=20, | |
learning_rate=0.1, | |
max_depth=1 | |
) | |
custom_gbm.fit(x, y) | |
custom_gbm_rmse = mean_squared_error(y, custom_gbm.predict(x), squared=False) |