Skip to content

Integrate with LightGBM

Comet can automatically log your model graph, logging metrics, and parameters from your LightGBM code. You need only add the following lines of code to your LightGBM script:

from comet_ml import Experiment
import lightgbm as lgbm

experiment = Experiment()

# Your code here...

gbm = lgbm.train()

Log automatically

The Comet LightGBM auto-logger can automatically log:

  • Model/graph description
  • Metrics (such as loss and accuracy)
  • Hyperparameters
  • Command-line arguments

Configure Comet for LightGBM

ItemExperiment ParameterEnvironment SettingConfiguration File Setting
Model/graph descriptionlog_graphCOMET_AUTO_LOG_GRAPHcomet.auto_log.graph
Command-line argumentsparse_argsCOMET_AUTO_LOG_CLI_ARGUMENTScomet.auto_log.cli_arguments

End-to-end example

Here is a simple end-to-end LightGBM example.

# Get the data for this script:
# wget -qq
# wget -qq
import comet_ml

import lightgbm as lgb
import os
import pandas as pd
from sklearn.metrics import mean_squared_error

experiment = comet_ml.Experiment(
    api_key="<Your API Key>",
    project_name="<Your Project Name>"

dirname = os.path.dirname(__file__)
df_train = pd.read_csv(os.path.join(dirname, "regression.train"), header=None, sep="\t")
df_test = pd.read_csv(os.path.join(dirname, "regression.test"), header=None, sep="\t")

y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)

lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)

params = {
    "boosting_type": "gbdt",
    "objective": "regression",
    "metric": {"rmse", "l2", "l1", "huber"},
    "num_leaves": 31,
    "learning_rate": 0.05,
    "feature_fraction": 0.9,
    "bagging_fraction": 0.8,
    "bagging_freq": 5,
    "verbosity": -1,

gbm = lgb.train(

y_pred = gbm.predict(X_test, num_iteration=gbm.best_iteration)
print("The rmse of prediction is:", mean_squared_error(y_test, y_pred) ** 0.5)


There are alternatives to setting the API key programatically. See more here.

Try it out!

Here's an example for using Comet with LightGBM.

Open In Colab

Aug. 3, 2022