Python Tutorial 72 of 96
Python Fundamentals
- Introduction
- Applications
- Input and Output
- Variables
- Operators
- Keywords
- Data Types
- Conditional Statements
- Loops
- Project: Rock Paper Scissor Game
Functions
- Functions
- Pass in Functions
- Global and Local Variables
- Recursion
- *args and **kwargs in Function
- First Class Function
- Lambda Function
- Map
- Reduce
- Filter Function
- Inner Function
- Decorators
- Project: Number Guessing Game
Data Structures
- Strings
- List
- Tuples
- Dictionary
- Sets
- Arrays
- List Comprehension
- Project: Tic Tac Toe Game
- Counters
- Heapq
- Deque
- OrderedDict
- Defaultdict
- Complete Tutorial on DSA with Python
OOP Concepts
- Python OOP
- Classes and Objects
- Constructors
- ‘Self’ as Default Argument
- Polymorphism
- Inheritance
- Abstraction
- Encapsulation
- Iterators
- Project: Banking System
File Handling
- File Handling
- Read Files
- Write/Create Files
- OS Module
- pathlib Module
- Directory Management
Database Handling
- MongoDB Introduction
- MySQL Introduction
- Project: Management System
Packages or Libraries
- Packages
- Built-in Modules
- DSA Libraries
- GUI Libraries
- Project: Screen Recorder
Data Science with Python
- NumPy
- Pandas
- Matplotlib
- Seaborn
- StatsModel
- Scikit-learn
- XGBoost
- LightGBM in Python
- TensorFlow in Python
- Keras
- PyTorch
- Complete Tutorial on Data Science
- Project: Sentiment Analysis
Web Development with Python
- Flask
- Django
- SQLite
- SQLAlchemy in Python
- MySQL
- Jinja2 (Flask)
- Django Templates
- Flask-RESTful
- Django REST Framework (DRF)
- Complete Tutorial on Web Development
- Project: ToDo webapp
Chapter-wise Practice Quiz
- Python Fundamentals Quiz
- Control Flow & Functions Quiz
- Data Structures Quiz
- Collections Module Quiz
- Object-Oriented Programming Quiz
- Exception & File Handling Quiz
Interview Questions and Practice Problems
LightGBM in Python
Learn how to use LightGBM in Python for fast gradient boosting, including installation, datasets, classification, regression, model training, evaluation, and practical examples.
LightGBM in Python
LightGBM is a gradient boosting framework designed for efficient and scalable machine learning. It is developed by Microsoft and is commonly used for classification, regression, ranking, and other supervised learning tasks.
LightGBM is particularly useful when working with large datasets because it is designed to provide fast training while maintaining strong predictive performance.
In Python, LightGBM can be used through its native API as well as its scikit-learn-compatible classes.
Common applications include:
- Classification
- Regression
- Ranking
- Binary prediction
- Multi-class prediction
- Feature importance analysis
- Large-scale machine learning
What Is LightGBM?
LightGBM stands for Light Gradient Boosting Machine.
It is a gradient boosting framework based on decision trees.
Instead of building one large decision tree, gradient boosting builds a sequence of trees where each new tree attempts to improve the errors made by the existing model.
Conceptually:
Training Data
↓
Decision Tree 1
↓
Errors / Residuals
↓
Decision Tree 2
↓
Errors / Residuals
↓
Decision Tree 3
↓
...
↓
Final Prediction
LightGBM uses several techniques to make this process efficient, including histogram-based learning and leaf-wise tree growth.
Why Use LightGBM?
LightGBM is popular because it can efficiently train gradient boosting models on large tabular datasets.
Some important characteristics include:
- Fast training
- Low memory usage compared with many traditional tree-based approaches
- Support for large datasets
- Support for categorical features
- Good performance on structured/tabular data
- Parallel and distributed training capabilities
- Support for classification, regression, and ranking
- Scikit-learn-compatible API
It is commonly compared with other gradient boosting frameworks such as XGBoost and CatBoost.
Installing LightGBM
Install LightGBM using pip:
pip install lightgbm
If you use Jupyter Notebook:
!pip install lightgbm
After installation, verify it:
import lightgbm
print(lightgbm.__version__)
Importing LightGBM
The package can be imported as:
import lightgbm as lgb
For the scikit-learn API, you can import specific model classes:
from lightgbm import LGBMClassifier
from lightgbm import LGBMRegressor
LightGBM Workflow in Python
A typical LightGBM workflow looks like this:
Load Dataset
↓
Clean and Prepare Data
↓
Split Features and Target
↓
Train/Test Split
↓
Create LightGBM Model
↓
Train Model
↓
Make Predictions
↓
Evaluate Model
↓
Tune Parameters
LightGBM Classification
Classification is used when the target represents categories.
For example:
Spam / Not Spam
Fraud / Not Fraud
Pass / Fail
Customer Churn / No Churn
Let's create a classification example using scikit-learn's built-in dataset.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier
from sklearn.metrics import accuracy_score
data = load_breast_cancer()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
model = LGBMClassifier(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
The model learns patterns from the training data and then predicts the classes of unseen test data.
Understanding LGBMClassifier
The LGBMClassifier class is designed for classification problems.
Basic syntax:
from lightgbm import LGBMClassifier
model = LGBMClassifier()
You can configure parameters:
model = LGBMClassifier(
n_estimators=200,
learning_rate=0.05,
max_depth=6,
num_leaves=31,
random_state=42
)
Then train:
model.fit(X_train, y_train)
And predict:
predictions = model.predict(X_test)
LightGBM Regression
Regression is used when the target is a continuous numeric value.
Examples include:
- House price prediction
- Sales forecasting
- Demand prediction
- Revenue prediction
- Temperature prediction
Use LGBMRegressor for regression.
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error
data = load_diabetes()
X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
model = LGBMRegressor(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
mse = mean_squared_error(y_test, predictions)
print("MSE:", mse)
Understanding LGBMRegressor
The LGBMRegressor class is designed for regression tasks.
Example:
from lightgbm import LGBMRegressor
model = LGBMRegressor(
n_estimators=200,
learning_rate=0.05,
num_leaves=31,
random_state=42
)
Train it:
model.fit(X_train, y_train)
Generate predictions:
predictions = model.predict(X_test)
LightGBM with Pandas
LightGBM works well with Pandas DataFrames.
Example:
import pandas as pd
from lightgbm import LGBMClassifier
data = pd.read_csv("customers.csv")
X = data.drop("churn", axis=1)
y = data["churn"]
model = LGBMClassifier(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
model.fit(X, y)
Here:
X
contains the input features, while:
y
contains the target.
Train-Test Split
It is important to evaluate the model on data that was not used for training.
Use:
from sklearn.model_selection import train_test_split
Example:
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
This creates:
- 80% training data
- 20% testing data
The exact split depends on the problem and dataset.
Training a LightGBM Model
Training is performed using:
model.fit(X_train, y_train)
For example:
from lightgbm import LGBMClassifier
model = LGBMClassifier(
n_estimators=100,
learning_rate=0.05
)
model.fit(X_train, y_train)
After training, the model can make predictions.
Making Predictions
Use:
predictions = model.predict(X_test)
For classification:
predictions = model.predict(X_test)
For regression:
predictions = model.predict(X_test)
The returned values depend on the model type.
Predicting Probabilities
For classification, you can obtain class probabilities using:
probabilities = model.predict_proba(X_test)
Example:
probabilities = model.predict_proba(X_test)
print(probabilities[:5])
For binary classification, the output commonly contains probabilities for both classes.
You can obtain the probability of the positive class with:
positive_probability = model.predict_proba(X_test)[:, 1]
Evaluating a Classification Model
Accuracy is one possible evaluation metric:
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(
y_test,
predictions
)
print("Accuracy:", accuracy)
Other useful metrics include:
- Precision
- Recall
- F1-score
- ROC-AUC
- Log loss
For example:
from sklearn.metrics import classification_report
print(
classification_report(
y_test,
predictions
)
)
Evaluating a Regression Model
Common regression metrics include:
- Mean Absolute Error
- Mean Squared Error
- Root Mean Squared Error
- R² score
Example:
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(
y_test,
predictions
)
print("MAE:", mae)
Important LightGBM Parameters
LightGBM provides many parameters for controlling model behavior.
Some important parameters are:
n_estimators
learning_rate
num_leaves
max_depth
min_child_samples
subsample
colsample_bytree
reg_alpha
reg_lambda
Let's look at the most important ones.
n_estimators
n_estimators controls the number of boosting iterations or trees.
Example:
model = LGBMClassifier(
n_estimators=200
)
Increasing the number of trees can improve learning up to a point, but too many trees can contribute to overfitting depending on the other parameters and data.
learning_rate
learning_rate controls how much each boosting iteration contributes to the final model.
Example:
model = LGBMClassifier(
learning_rate=0.05
)
A smaller learning rate often requires more trees.
For example:
model = LGBMClassifier(
n_estimators=500,
learning_rate=0.03
)
The relationship between learning rate and number of estimators is important when tuning a boosting model.
num_leaves
num_leaves controls the maximum number of leaves in a tree.
Example:
model = LGBMClassifier(
num_leaves=31
)
Increasing the number of leaves can make the model more expressive, but excessive complexity can increase overfitting.
max_depth
max_depth limits the maximum depth of a tree.
Example:
model = LGBMClassifier(
max_depth=6
)
A smaller maximum depth can constrain model complexity.
When using LightGBM, num_leaves is especially important because LightGBM's tree growth strategy is leaf-wise.
min_child_samples
This parameter controls the minimum number of data points required in a leaf.
Example:
model = LGBMClassifier(
min_child_samples=20
)
Increasing this value can help prevent overly specific leaves.
subsample
subsample controls the fraction of training data used during certain boosting iterations.
Example:
model = LGBMClassifier(
subsample=0.8
)
Using a fraction of the data can sometimes help reduce overfitting.
colsample_bytree
This parameter controls the fraction of features considered when building trees.
Example:
model = LGBMClassifier(
colsample_bytree=0.8
)
This can help introduce randomness and reduce over-reliance on particular features.
Regularization Parameters
LightGBM supports regularization parameters such as:
reg_alpha
reg_lambda
For example:
model = LGBMClassifier(
reg_alpha=0.1,
reg_lambda=0.1
)
Regularization can help control model complexity and reduce overfitting.
Feature Importance
LightGBM can provide feature importance information.
For example:
importance = model.feature_importances_
print(importance)
If you have feature names:
for name, value in zip(
X.columns,
model.feature_importances_
):
print(name, value)
This can help identify which features contributed most to the trained model according to the selected importance measure.
Plotting Feature Importance
LightGBM provides plotting utilities.
import lightgbm as lgb
import matplotlib.pyplot as plt
lgb.plot_importance(model)
plt.show()
You can also specify the number of features:
lgb.plot_importance(
model,
max_num_features=10
)
plt.show()
Feature importance should be interpreted carefully because importance scores do not necessarily establish causation.
LightGBM and Categorical Features
LightGBM can work with categorical features.
With Pandas, a categorical column can be represented using the category dtype.
Example:
import pandas as pd
data["city"] = data["city"].astype("category")
Then the model can be trained with appropriate categorical feature handling.
For example:
model.fit(
X_train,
y_train,
categorical_feature=["city"]
)
The exact handling depends on the LightGBM API and dataset structure.
Handling Missing Values
LightGBM supports missing values in many common numeric feature scenarios.
For example, a DataFrame may contain:
import pandas as pd
data = pd.DataFrame({
"age": [25, 30, None, 40],
"income": [50000, 60000, 55000, None]
})
The model can generally handle missing numeric values without requiring every missing value to be manually replaced first.
However, data preprocessing should still be designed carefully based on the dataset and application.
LightGBM with Early Stopping
Early stopping can help prevent unnecessary boosting iterations.
Conceptually, training can stop when the validation performance stops improving.
Example:
from lightgbm import LGBMClassifier
import lightgbm as lgb
model = LGBMClassifier(
n_estimators=1000,
learning_rate=0.05,
random_state=42
)
model.fit(
X_train,
y_train,
eval_set=[(X_test, y_test)],
callbacks=[
lgb.early_stopping(50)
]
)
Here, training can stop when the validation metric does not improve for the specified number of iterations.
For production model evaluation, use a validation set separate from the final test set when appropriate.
LightGBM with Cross-Validation
Cross-validation can be used to evaluate model performance across multiple data splits.
Scikit-learn provides tools such as:
from sklearn.model_selection import cross_val_score
Example:
from lightgbm import LGBMClassifier
from sklearn.model_selection import cross_val_score
model = LGBMClassifier(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
scores = cross_val_score(
model,
X,
y,
cv=5,
scoring="accuracy"
)
print(scores)
print("Mean:", scores.mean())
This provides performance measurements across five folds.
Hyperparameter Tuning
LightGBM models can be tuned using tools such as:
GridSearchCVRandomizedSearchCV- Bayesian optimization approaches
For example:
from sklearn.model_selection import GridSearchCV
from lightgbm import LGBMClassifier
model = LGBMClassifier(
random_state=42
)
parameters = {
"n_estimators": [100, 200],
"learning_rate": [0.03, 0.05],
"num_leaves": [15, 31]
}
grid = GridSearchCV(
model,
parameters,
cv=3,
scoring="accuracy"
)
grid.fit(X_train, y_train)
print(grid.best_params_)
Hyperparameter search can be computationally expensive, especially with large datasets.
LightGBM vs Traditional Decision Trees
A single decision tree might look like:
Feature A
/ \
Yes No
/ \
Feature B Prediction
Gradient boosting builds many trees sequentially.
Tree 1
↓
Tree 2
↓
Tree 3
↓
...
↓
Final Model
Each new tree contributes to improving the overall prediction.
LightGBM Tree Growth
One notable characteristic of LightGBM is its leaf-wise tree growth strategy.
Many traditional tree-based boosting approaches use level-wise growth:
Level 1
↓
Level 2
↓
Level 3
LightGBM instead chooses the leaf that provides the largest reduction in loss and splits that leaf.
Conceptually:
Root
/ \
A B
/ \
C D
If leaf D offers the largest potential improvement, LightGBM may split that leaf next.
This can produce efficient models but also requires careful control of model complexity, especially with parameters such as num_leaves.
LightGBM Native API
LightGBM also provides a native training API.
For example:
import lightgbm as lgb
train_data = lgb.Dataset(
X_train,
label=y_train
)
params = {
"objective": "binary",
"learning_rate": 0.05,
"num_leaves": 31,
"verbosity": -1
}
model = lgb.train(
params,
train_data,
num_boost_round=100
)
The native API provides more direct access to LightGBM's training functionality.
For many scikit-learn workflows, however, LGBMClassifier and LGBMRegressor provide a convenient interface.
LightGBM for Binary Classification
Binary classification has two possible classes.
Examples:
Fraud / Not Fraud
Churn / No Churn
Spam / Not Spam
Approved / Rejected
Example:
from lightgbm import LGBMClassifier
model = LGBMClassifier(
objective="binary",
n_estimators=200,
learning_rate=0.05
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
LightGBM for Multi-Class Classification
LightGBM can also be used for multiple classes.
For example:
Class A
Class B
Class C
Example:
model = LGBMClassifier(
objective="multiclass",
num_class=3,
n_estimators=100,
learning_rate=0.05
)
model.fit(X_train, y_train)
The target labels should be prepared appropriately for the model and dataset.
LightGBM for Regression
Regression predicts continuous numeric values.
Example:
from lightgbm import LGBMRegressor
model = LGBMRegressor(
objective="regression",
n_estimators=200,
learning_rate=0.05
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
Advantages of LightGBM
Some commonly cited advantages of LightGBM include:
- Efficient training on large tabular datasets
- Good predictive performance for many structured-data problems
- Support for categorical features
- Efficient memory usage
- Parallel learning capabilities
- Flexible configuration
- Scikit-learn integration
- Native support for several learning objectives
Limitations and Considerations
LightGBM is not automatically the right choice for every machine learning problem.
Consider the following:
- It is primarily designed for structured/tabular data.
- Hyperparameter tuning can be important.
- Leaf-wise growth can overfit when model complexity is not controlled.
- Very small datasets may not benefit from all of its scalability advantages.
- Feature engineering and data quality still have a major impact on results.
- Feature importance should not automatically be interpreted as causal importance.
For image, audio, or highly unstructured text tasks, other model families may be more appropriate.
LightGBM Best Practices
When working with LightGBM:
- Start with a simple baseline model.
- Separate training, validation, and test data appropriately.
- Choose evaluation metrics based on the problem.
- Tune
learning_rateandn_estimatorstogether. - Monitor model complexity using parameters such as
num_leavesandmax_depth. - Use early stopping when appropriate.
- Check for class imbalance in classification problems.
- Inspect feature importance as a diagnostic tool, not as proof of causality.
- Use cross-validation when appropriate.
- Avoid overfitting the test set during repeated experimentation.
Complete LightGBM Classification Example
Here is a complete example using the breast cancer dataset:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
from lightgbm import LGBMClassifier
# Load data
data = load_breast_cancer()
X = data.data
y = data.target
# Split data
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42,
stratify=y
)
# Create model
model = LGBMClassifier(
n_estimators=200,
learning_rate=0.05,
num_leaves=31,
random_state=42
)
# Train
model.fit(X_train, y_train)
# Predict
predictions = model.predict(X_test)
# Evaluate
accuracy = accuracy_score(
y_test,
predictions
)
print("Accuracy:", accuracy)
print(
classification_report(
y_test,
predictions
)
)
The workflow is:
Load Dataset
↓
Split Dataset
↓
Create LGBMClassifier
↓
Train Model
↓
Predict
↓
Evaluate
LightGBM vs XGBoost
LightGBM and XGBoost are both gradient boosting frameworks commonly used for structured machine learning problems.
| Feature | LightGBM | XGBoost |
|---|---|---|
| Algorithm | Gradient boosting | Gradient boosting |
| Tree growth | Leaf-wise | Typically depth/level-oriented behavior |
| Large datasets | Designed for efficient large-scale training | Also supports large datasets |
| Categorical handling | Native categorical feature support | Traditionally requires encoding, with newer capabilities depending on API/version |
| Python API | Native + scikit-learn API | Native + scikit-learn API |
| Main use | Classification, regression, ranking | Classification, regression, ranking |
The appropriate choice depends on the dataset, preprocessing, constraints, and evaluation results rather than the library name alone.
Key Points to Remember
- LightGBM stands for Light Gradient Boosting Machine.
- It is a gradient boosting framework developed by Microsoft.
- It is especially useful for structured and tabular machine learning data.
LGBMClassifieris used for classification.LGBMRegressoris used for regression.- LightGBM also provides a native
lgb.train()API. - Important parameters include
n_estimators,learning_rate,num_leaves, andmax_depth. - LightGBM uses leaf-wise tree growth.
- Early stopping can help avoid unnecessary training iterations.
- LightGBM supports categorical features and missing values in appropriate scenarios.
- Feature importance can help analyze model behavior.
- Cross-validation and hyperparameter tuning can improve model evaluation.
- Model quality depends on data, features, evaluation methodology, and parameter choices.
Conclusion
LightGBM is a powerful gradient boosting framework for machine learning with Python, particularly for structured and tabular datasets.
A simple classification model can be created with:
from lightgbm import LGBMClassifier
model = LGBMClassifier(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
For regression, use:
from lightgbm import LGBMRegressor
model = LGBMRegressor(
n_estimators=100,
learning_rate=0.05,
random_state=42
)
Once the basics are understood, you can move on to validation, early stopping, hyperparameter tuning, feature importance, categorical features, and production-oriented model evaluation.
Related
-
TensorFlow in Python
Learn TensorFlow in Python, including installation, tensors, operations, neural networks, model training, prediction, evaluation, and practical machine learning examples.
-
AI Output Always Looks Right. Here Is How to Tell When It Is Not
A correct AI answer and a confidently wrong one look identical. The useful question is not whether AI can do the task, but how fast you would find out if it got it wrong.
-
SQLAlchemy in Python
Learn SQLAlchemy in Python, including Core, ORM, engines, sessions, CRUD operations, queries, relationships, transactions, and database integration.
-
Python Interview Questions and Answers
Prepare for Python interviews with commonly asked questions and answers covering Python basics, data types, functions, OOP, exceptions, collections, modules, and advanced concepts.