🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #171

LightGBM

LightGBM is another highly optimized gradient boosting implementation, built specifically for speed and memory efficiency on very large datasets — its main structural difference from XGBoost is how it grows individual trees.

Leaf-Wise vs Level-Wise Tree Growth

Level-wise (traditional, XGBoost's default)Leaf-wise (LightGBM's default)
Growth strategyExpands every leaf at the current depth before going deeperExpands whichever single leaf reduces loss the most, regardless of depth
Resulting tree shapeBalanced, symmetricCan be unbalanced, deeper in some branches
SpeedSlower for the same number of leavesFaster — fewer wasted splits on unhelpful leaves
Overfitting riskLower on small datasetsHigher on small datasets — needs careful depth/leaf-count limits
Level-wise (balanced) Leaf-wise (unbalanced)

Leaf-wise growth keeps chasing the single most impactful split, producing a deeper, less symmetric tree — usually more accurate per leaf, at higher overfitting risk on small data.

Python Implementation

import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

X_train, X_test, y_train, y_test = train_test_split(
    data.data, data.target, test_size=0.2, random_state=42, stratify=data.target
)

model = lgb.LGBMClassifier(
    n_estimators=200,
    learning_rate=0.1,
    num_leaves=31,       # controls tree complexity directly, since growth is leaf-wise
    random_state=42,
)
model.fit(X_train, y_train)
print(accuracy_score(y_test, model.predict(X_test)))

Notice num_leaves is LightGBM's primary complexity control, rather than max_depth — a direct consequence of leaf-wise growth not respecting depth symmetry the way level-wise growth does.

Why LightGBM Is Faster on Large Data

  • Histogram-based splitting: bins continuous features into discrete buckets before searching for splits, dramatically reducing the number of candidate split points considered
  • Leaf-wise growth: spends computation on the splits that actually reduce loss the most, rather than exhaustively expanding every node at each level
  • Native support for large, sparse, and high-cardinality categorical data without requiring one-hot encoding first

Practical Use Cases

  • Very large tabular datasets where XGBoost's training time becomes impractical
  • Problems with many high-cardinality categorical features, handled more natively than in XGBoost

Common Mistakes

  • Using LightGBM's default num_leaves on a small dataset without tuning it down — leaf-wise growth's higher overfitting risk is most pronounced exactly when data is limited.
  • Assuming LightGBM will always outperform XGBoost — on small-to-medium datasets, the difference is often minor, and XGBoost's more conservative level-wise default can generalize better.

Interview Relevance

Q: "Why does LightGBM use num_leaves instead of max_depth as its primary complexity control?" Because its trees grow leaf-wise, not level-wise — the tree can be very deep in one branch and shallow in another, so max_depth alone poorly describes its actual complexity; num_leaves directly caps the quantity that leaf-wise growth actually controls.

Practice Question

You're training on a dataset with only 500 rows. Would you lean toward XGBoost's level-wise default or LightGBM's leaf-wise default, and why?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

LightGBM – FAQs

Quick answers about learning LightGBM in Machine Learning.

This free note from CodingNow 2.0 explains LightGBM in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including LightGBM, is 100% free with no signup required.
With focused practice, most students grasp LightGBM in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now