LightGBM Python
LightGBM (Light Gradient Boosting Machine) is a popular open-source machine learning library for gradient boosting on decision trees. It is designed to be efficient and optimized for large-scale data with high-dimensional features. LightGBM is written in C++ and provides Python bindings for easy use in Python environments. In Python, you can use the lightgbm
package to work with this library.
Here’s a step-by-step guide on how to use LightGBM in Python:
- Install LightGBM: To get started, you need to install the
lightgbm
package. You can do this using pip:
pip install lightgbm
- Import the necessary libraries: Once LightGBM is installed, you need to import the required libraries in your Python script or Jupyter Notebook.
import lightgbm as lgb
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
- Prepare your data: Load your dataset and split it into training and testing sets.
# Assuming you have your dataset loaded in a pandas DataFrame called 'data'
X = data.drop('target', axis=1)
y = data['target']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
- Create a LightGBM dataset: To train a LightGBM model, you need to convert your data into a LightGBM dataset format.
train_data = lgb.Dataset(X_train, label=y_train)
test_data = lgb.Dataset(X_test, label=y_test)
- Set hyperparameters and train the model: Define your model’s hyperparameters and train the LightGBM model.
params = {
'objective': 'binary', # for binary classification task
'metric': 'binary_logloss', # evaluation metric
'boosting_type': 'gbdt', # gradient boosting type
'num_leaves': 31,
'learning_rate': 0.05,
'feature_fraction': 0.9,
'bagging_fraction': 0.8,
'bagging_freq': 5,
'verbose': 0
}
# Training the model
num_rounds = 100 # Number of boosting rounds (trees)
model = lgb.train(params, train_data, num_rounds)
- Make predictions: Once the model is trained, you can use it to make predictions on new data.
# Predicting on the test set
y_pred = model.predict(X_test)
# If you’re working on a binary classification problem and need to get the binary labels (0 or 1)
y_pred_binary = [round(pred) for pred in y_pred]
- Evaluate the model: Evaluate the performance of the model using appropriate metrics.
from sklearn.metrics import accuracy_score, roc_auc_score
# For binary classification, you can use accuracy and ROC-AUC score as evaluation metrics
accuracy = accuracy_score(y_test, y_pred_binary)
roc_auc = roc_auc_score(y_test, y_pred)
print(“Accuracy:”, accuracy)
print(“ROC-AUC Score:”, roc_auc)
That’s a basic guide to using LightGBM in Python for binary classification. LightGBM also supports multiclass classification and regression tasks. The hyperparameters and data preparation might vary depending on the problem type, and you can further fine-tune the model using cross-validation and hyperparameter tuning techniques.
Python Training Demo Day 1
Conclusion:
Unogeeks is the No.1 IT Training Institute for Python Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Python here – Python Blogs
You can check out our Best In Class Python Training Details here – Python Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks