🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML LightGBM

LightGBM is a fast boosting library that grows its trees leaf by leaf. It shines on big tables with many rows.

LightGBM in four lines

LGBMClassifier follows the same fit-then-score shape as the scikit-learn models.

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier

data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
model = LGBMClassifier(n_estimators=100, random_state=42, verbose=-1)
model.fit(X_train, y_train)
print("Test accuracy:", round(model.score(X_test, y_test), 4))

Leaf-wise growth in one paragraph

Leaf-wise growth picks the one leaf that cuts the error most, and grows only that leaf. Level-wise growth grows every leaf at the same depth before moving deeper. Leaf-wise growth reaches a good tree with fewer splits, so it trains faster on big tables. The risk is a very deep branch that memorises the training rows, so LightGBM caps the leaves with num_leaves. The code below shows that more trees still help: 10 trees score 0.9561, and 100 trees score 0.9649.

for trees in [10, 100]:
    m = LGBMClassifier(n_estimators=trees, random_state=42, verbose=-1)
    m.fit(X_train, y_train)
    print(trees, "trees:", round(m.score(X_test, y_test), 4))

The most split column

LightGBM counts splits per column. The top column split the data 241 times.

pairs = sorted(zip(data.feature_names, model.feature_importances_), key=lambda p: p[1], reverse=True)
print("Top feature:", pairs[0][0], int(pairs[0][1]), "splits")

Example

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from lightgbm import LGBMClassifier

data = load_breast_cancer()
X_train, X_temp, y_train, y_temp = train_test_split(
    data.data, data.target, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(
    X_temp, y_temp, test_size=0.5, random_state=42)
model = LGBMClassifier(n_estimators=100, random_state=42, verbose=-1)
model.fit(X_train, y_train, eval_X=X_val, eval_y=y_val)
print("Test accuracy:", round(model.score(X_test, y_test), 4))
pairs = sorted(zip(data.feature_names, model.feature_importances_),
               key=lambda pair: pair[1], reverse=True)
for name, value in pairs[:3]:
    print(name, round(float(value), 4))

Key points

  • LightGBM: a fast boosting library, short for Light Gradient Boosting Machine
  • Use LGBMClassifier for yes-or-no answers
  • Use num_leaves to cap how many leaves one tree may grow
  • Use eval_X and eval_y to hand the model held-back rows that track its progress while it trains
  • Use feature_importances_ to count how many times each column split the data

In this example, the rows are divided twice, as in the XGBoost lesson. The first cut keeps back 30 percent of the rows, and the second cut divides that piece into validation rows and test rows. LGBMClassifier builds 100 trees, and fit checks progress on the validation rows through eval_X and eval_y. The test accuracy is 0.9302 on the 85 test rows that remain. The last lines rank the columns by split count, and worst texture leads with 221 splits. In short, LightGBM is boosting that grows the best leaf first, which makes it fast on wide tables.

Note

LightGBM counts splits, so its importance numbers are whole counts, not shares.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

How does LightGBM grow a tree?