Training a Random LightGBM Model

  • Share this:

Code introduction


This function uses the LightGBM library to train a classification model. It accepts the training dataset X_train and labels y_train as inputs, and returns the trained LightGBM model.


Technology Stack : LightGBM, NumPy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import lightgbm as lgb
import numpy as np
import random

def generate_random_lightgbm_model(X_train, y_train, num_leaves=31, max_depth=-1):
    # Create a LightGBM dataset
    train_data = lgb.Dataset(X_train, label=y_train)
    
    # Define parameters for the LightGBM model
    params = {
        'objective': 'binary',
        'metric': 'binary_logloss',
        'boosting_type': 'gbdt',
        'num_leaves': num_leaves,
        'max_depth': max_depth
    }
    
    # Train the LightGBM model
    gbm = lgb.train(params, train_data)
    
    return gbm                
              
Tags: