Training LightGBM for Binary Classification

  • Share this:

Code introduction


This function trains a LightGBM model for binary classification using the provided training data and a set of parameters such as the number of leaves, maximum depth, learning rate, and number of estimators.


Technology Stack : LightGBM, NumPy

Code Type : Machine learning

Code Difficulty : Intermediate


                
                    
import lightgbm as lgb
import numpy as np

def train_lightgbm(X_train, y_train, num_leaves=31, max_depth=-1, learning_rate=0.1, n_estimators=100):
    """
    This function trains a LightGBM model on the given training data.
    """
    # Create a LightGBM dataset
    train_data = lgb.Dataset(X_train, label=y_train)
    
    # Specify the parameters for the LightGBM model
    params = {
        'objective': 'binary',
        'metric': 'binary_logloss',
        'boosting': 'gbdt',
        'num_leaves': num_leaves,
        'max_depth': max_depth,
        'learning_rate': learning_rate,
        'n_estimators': n_estimators
    }
    
    # Train the model
    bst = lgb.train(params, train_data)
    
    return bst                
              
Tags: