Skip to content

Instantly share code, notes, and snippets.

View tomonori-masui's full-sized avatar

Tomonori Masui tomonori-masui

View GitHub Profile
@tomonori-masui
tomonori-masui / train_node_classifier.py
Last active September 23, 2022 06:02
Training and evaluation functions for node classifier
def train_node_classifier(model, graph, optimizer, criterion, n_epochs=200):
for epoch in range(1, n_epochs + 1):
model.train()
optimizer.zero_grad()
out = model(graph)
loss = criterion(out[graph.train_mask], graph.y[graph.train_mask])
loss.backward()
optimizer.step()
@tomonori-masui
tomonori-masui / mlp_on_graph.py
Last active September 16, 2022 04:58
MLP on PyG graph data
import torch.nn as nn
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(dataset.num_node_features, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
@tomonori-masui
tomonori-masui / compare_gbm_log_loss.py
Created February 2, 2022 06:32
Comparing custom GBM's log-loss to sklearn's
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import log_loss
custom_gbm = CustomGradientBoostingClassifier(
n_estimators=20,
learning_rate=0.1,
max_depth=1
)
custom_gbm.fit(x, y)
custom_gbm_log_loss = log_loss(y, custom_gbm.predict_proba(x))
@tomonori-masui
tomonori-masui / custom_gbdt.py
Last active February 5, 2022 06:10
CustomGradientBoostingClassifier
class CustomGradientBoostingClassifier:
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)
@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):
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",
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))),
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)
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()