LightGBM with the scikit-learn API

LightGBM · Essentials · 9 min

The scikit-learn wrapper gets you a strong tabular model in a few lines and fits into pipelines and searches.

LightGBM is a gradient boosting library: it adds decision trees one at a time, each correcting the errors of the ensemble so far. The lightgbm package provides scikit-learn compatible estimators:

  • LGBMClassifier (binary and multi-class),
  • LGBMRegressor,
  • LGBMRanker for learning to rank.

The usual API applies: constructor hyperparameters, fit(X, y), predict, predict_proba, score, and full compatibility with Pipeline, cross_val_score and GridSearchCV. Inputs can be NumPy arrays, pandas DataFrames (column names are kept; category columns are used natively) or SciPy sparse matrices.

Defaults: 100 trees (n_estimators), learning_rate=0.1, num_leaves=31, no depth limit. No feature scaling is needed and missing values are handled natively. Unlike XGBoost, class labels may be strings: the classifier encodes them and classes_ lists them.

LightGBM logs warnings such as "No further splits with positive gain" during training. Pass verbose=-1 to the constructor to silence them once you know what they mean.

Its main selling point is speed: histogram-based, leaf-wise training with careful engineering, often several times faster than other boosting libraries on large data with similar accuracy.

Example

import lightgbm as lgb
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)

model = lgb.LGBMClassifier(n_estimators=200, learning_rate=0.05, verbose=-1)
model.fit(X_tr, y_tr)

print(round(model.score(X_te, y_te), 3))           # 0.944
print(model.predict_proba(X_te.iloc[:2]).round(3))
# [[0. 1.]
#  [1. 0.]]

labels = y_tr.map({0: "malignant", 1: "benign"})    # string labels work too
print(lgb.LGBMClassifier(verbose=-1).fit(X_tr, labels).classes_)  # ['benign' 'malignant']

Remember

  • LGBMClassifier/LGBMRegressor follow the scikit-learn API
  • No scaling needed; NaN and pandas categories are handled natively
  • verbose=-1 silences training warnings

Watch out for

  • Silencing warnings with verbose=-1 before ever reading them; they often reveal data problems.

Check yourself

  1. Which of these does LightGBM's LGBMClassifier accept without extra preprocessing?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: lightgbm.readthedocs.io/en/stable/pythonapi/lightgbm.LGBMClassifier.html