You can download this code by clicking the button below.
This code is now available for download.
This function uses the LightGBM library to train a regression model, accepting a training dataset and a set of parameters as input.
Technology Stack : LightGBM, NumPy
Code Type : Function
Code Difficulty : Intermediate
import lightgbm as lgb
import numpy as np
def train_lightgbm_model(X_train, y_train, num_leaves=31, max_depth=-1, learning_rate=0.1, n_estimators=100):
"""
Train a LightGBM model with given parameters.
"""
# Create a LightGBM dataset
train_data = lgb.Dataset(X_train, label=y_train)
# Create a LightGBM model
gbm = lgb.train({
'objective': 'regression', # or 'binary', 'multiclass'
'num_leaves': num_leaves,
'max_depth': max_depth,
'learning_rate': learning_rate,
'n_estimators': n_estimators
},
train_data)
return gbm