Skip to content

Instantly share code, notes, and snippets.

View tomonori-masui's full-sized avatar

Tomonori Masui tomonori-masui

View GitHub Profile
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))),
(
from sktime.datasets import load_airline
ts_al = load_airline()
tsplot(ts_al)
ts_al_diff = (ts_al - ts_al.shift(12)).dropna()
tsplot(ts_al_diff)
ts_al_2diff = (ts_al_diff - ts_al_diff.shift(1)).dropna()
tsplot(ts_al_2diff)
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()
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)
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))),
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",
@tomonori-masui
tomonori-masui / custom_gbrt.py
Last active June 6, 2024 16:54
CustomGradientBoostingRegressor
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):
@tomonori-masui
tomonori-masui / compare_gbm_rmses.py
Last active June 6, 2024 16:54
Comparing custom GBM's RMSE to sklearn's
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)