LightGBM Random Forest Model for Multiclass Classification

  • Share this:

Code introduction


This function uses the LightGBM library to train and predict a random forest model for multiclass classification. It first creates training and test datasets, then specifies model parameters, trains the model, and makes predictions on the test set.


Technology Stack : LightGBM, numpy

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import lightgbm as lgb
import numpy as np

def random_forest_classification(X_train, y_train, X_test):
    # Create train and test dataset for LightGBM
    train_data = lgb.Dataset(X_train, label=y_train)
    test_data = lgb.Dataset(X_test, reference=train_data)

    # Specify parameters for the LightGBM model
    params = {
        'boosting_type': 'gbdt',
        'objective': 'multiclass',
        'num_class': 3,
        'metric': 'multi_logloss'
    }

    # Train the model
    num_round = 100
    bst = lgb.train(params, train_data, num_round, valid_sets=[test_data])

    # Predict on the test set
    y_pred = bst.predict(X_test)

    return y_pred                
              
Tags: