LGBMClassifier - Training a Binary Classification Model With LightGBM

Python
Supervised Learning

This code snippet initializes and fits a binary classification model using the LGBMClassifier.
The code saves the trained model and plots the feature importances. Finally it makes predictions on the test set and evaluates performance using the classification report, log loss, and ROC AUC score.

 1|  import lightgbm as lgb
 2|  import matplotlib.pyplot as plt
 3|  from sklearn.metrics import classification_report, log_loss, roc_auc_score
 4|  
 5|  # Step 1: Initialise and fit LightGBM binary classification model
 6|  model = lgb.LGBMClassifier(
 7|      num_iterations=1000,
 8|      max_depth=4,
 9|      learning_rate=0.1,
10|      reg_lambda=1,
11|      random_state=101,
12|      n_jobs=-1
13|  )
14|  model.fit(
15|      X_train, y_train,
16|      eval_set=(X_test, y_test),
17|      verbose=False
18|  )
19|  
20|  model.booster_.save_model('lightgbm_classification.model')
21|  
22|  # Step 2: Plot feature importances
23|  features = X_train.columns
24|  importance_values = model.feature_importances_
25|  
26|  plt.barh(y=range(len(features)),
27|           width=importance_values,
28|           tick_label=features)
29|  plt.show()
30|  
31|  # Step 3: Make predictions for test data & evaluate performance
32|  y_pred = model.predict(X_test)
33|  print('Classification Report:', classification_report(y_test, y_pred))
34|  print('Log Loss:', log_loss(y_test, y_pred))
35|  print('ROC AUC:', roc_auc_score(y_test, y_pred))
1 Upvote
Did you find this snippet useful?

Sign up to bookmark this in your snippet library

2