Skip to content

Commit

Permalink
update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
RektPunk committed Sep 27, 2024
1 parent 010f340 commit c5480aa
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 23 deletions.
13 changes: 0 additions & 13 deletions examples/README.md

This file was deleted.

17 changes: 8 additions & 9 deletions examples/engine.py → examples/binary_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import lightgbm as lgb
from scipy.special import expit
from sklearn.datasets import load_breast_cancer
from sklearn.metrics import accuracy_score, log_loss, roc_auc_score
from sklearn.model_selection import train_test_split
Expand Down Expand Up @@ -33,14 +32,15 @@
}

# Train standard LightGBM model
bst_standard = lgb.train(
lgb_standard = lgb.train(
params_standard, train_data, num_boost_round=100, valid_sets=[test_data]
)

# Parameters for Imbalanced LightGBM model
params_imbalanced = {
"objective": "weighted", # focal
"metric": "binary_logloss", # auc
"objective": "binary_focal", # binary_weighted
"gamma": 2.0, # alpha with binary_weighted
"metric": "auc",
"learning_rate": 0.05,
"num_leaves": 31,
"feature_fraction": 0.9,
Expand All @@ -50,25 +50,24 @@
"early_stopping_rounds": 10,
}

bst_focal = imlgb.train(
# Train imbalanced LightGBM model
imlgb_focal = imlgb.train(
params_imbalanced, train_data, num_boost_round=100, valid_sets=[test_data]
)

# Predict using standard LightGBM model
y_pred_standard = bst_standard.predict(X_test)
y_pred_standard = lgb_standard.predict(X_test)
y_pred_standard_binary = (y_pred_standard > 0.5).astype(int)

# Predict using Imbalanced LightGBM model
y_pred_focal_raw = bst_focal.predict(X_test)
y_pred_focal = expit(y_pred_focal_raw)
y_pred_focal = imlgb_focal.predict(X_test)
y_pred_focal_binary = (y_pred_focal > 0.5).astype(int)

# Evaluate models
accuracy_standard = accuracy_score(y_test, y_pred_standard_binary)
logloss_standard = log_loss(y_test, y_pred_standard)
rocauc_standard = roc_auc_score(y_test, y_pred_standard)


accuracy_focal = accuracy_score(y_test, y_pred_focal_binary)
logloss_focal = log_loss(y_test, y_pred_focal)
rocauc_focal = roc_auc_score(y_test, y_pred_focal)
Expand Down
5 changes: 4 additions & 1 deletion examples/sklearn.py → examples/binary_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

# Initialize the ImbalancedLGBMClassifier using binary focal loss
clf = imlgb.ImbalancedLGBMClassifier(
objective="binary_focal",
objective="binary_focal", # binary_weighted
gamma=2.0, # alpha with binary_weighted
learning_rate=0.05,
num_leaves=31,
)

# Train the classifier on the training data
Expand Down
81 changes: 81 additions & 0 deletions examples/multiclass_engine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import lightgbm as lgb
import numpy as np
from sklearn.datasets import make_classification
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split

import imlightgbm as imlgb

# Generate dataset
X, y = make_classification(
n_samples=5000,
n_features=10,
n_classes=3,
n_informative=5,
weights=[0.05, 0.15, 0.8],
flip_y=0,
random_state=42,
)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

# Create LightGBM datasets
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test)

# Parameters for standard LightGBM model
params = {
"objective": "multiclass",
"num_class": 3,
"metric": "multi_logloss",
"learning_rate": 0.05,
"num_leaves": 31,
"feature_fraction": 0.9,
"bagging_fraction": 0.8,
"bagging_freq": 5,
"seed": 42,
"early_stopping_rounds": 10,
}

# Train standard LightGBM model
lgb_standard = lgb.train(
params, train_data, num_boost_round=100, valid_sets=[test_data]
)

# Predict using standard LightGBM model
y_pred_standard = lgb_standard.predict(X_test)
y_pred_standard_label = np.argmax(y_pred_standard, axis=1)

# Parameters for Imbalanced LightGBM model
params = {
"objective": "multiclass_focal", # multiclass_weighted
"num_class": 3,
"gamma": 2.0, # alpha with binary_weighted
"metric": "multi_logloss",
"learning_rate": 0.05,
"num_leaves": 31,
"feature_fraction": 0.9,
"bagging_fraction": 0.8,
"bagging_freq": 5,
"seed": 42,
"early_stopping_rounds": 10,
}

# Train Imbalanced LightGBM model
imlgb_focal = imlgb.train(
params, train_data, num_boost_round=100, valid_sets=[test_data]
)

# Predict using Imbalanced LightGBM model
y_pred_focal = imlgb_focal.predict(X_test)
y_pred_focal_label = np.argmax(y_pred_focal, axis=1)


# Evaluate models
print("\nClassification Report for Standard:")
print(classification_report(y_test, y_pred_standard_label))
print("\nClassification Report for Imbalanced:")
print(classification_report(y_test, y_pred_focal_label))

0 comments on commit c5480aa

Please sign in to comment.