0
0
ML Pythonml~5 mins

LightGBM in ML Python

Choose your learning style9 modes available
Introduction

LightGBM helps computers learn from data fast and well. It builds smart decision trees to make good predictions.

When you want to predict if an email is spam or not.
When you need to guess house prices from features like size and location.
When you want to classify images or texts quickly.
When you have a large dataset and want fast training.
When you want better accuracy than simple models.
Syntax
ML Python
import lightgbm as lgb

model = lgb.LGBMClassifier(
    num_leaves=31,
    learning_rate=0.1,
    n_estimators=100
)

model.fit(X_train, y_train)

predictions = model.predict(X_test)

num_leaves controls the complexity of each tree.

learning_rate controls how fast the model learns.

Examples
This creates a LightGBM model with 50 trees and a slower learning rate for careful learning.
ML Python
model = lgb.LGBMClassifier(n_estimators=50, learning_rate=0.05)
This sets up LightGBM for regression tasks with more leaves and trees for better fit.
ML Python
model = lgb.LGBMRegressor(num_leaves=40, n_estimators=200)
Sample Model

This program trains a LightGBM classifier on breast cancer data to predict if tumors are malignant or benign. It prints the accuracy on test data.

ML Python
import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data
data = load_breast_cancer()
X, y = data.data, data.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create model
model = lgb.LGBMClassifier(num_leaves=31, learning_rate=0.1, n_estimators=100)

# Train model
model.fit(X_train, y_train)

# Predict
y_pred = model.predict(X_test)

# Check accuracy
acc = accuracy_score(y_test, y_pred)
print(f"Accuracy: {acc:.4f}")
OutputSuccess
Important Notes

LightGBM is faster than many other tree-based models because it uses special techniques like histogram-based splitting.

It works well with large datasets and many features.

You can tune parameters like num_leaves and learning_rate to improve results.

Summary

LightGBM builds fast and accurate decision tree models.

It is great for classification and regression tasks.

Easy to use with simple code and good default settings.