Search code examples
pythonlightgbm

How to implement record_evaluation callback in LightGBM python?


I am using the model = lgb.train function. When trying to plot the evaluation metric against epochs of a LightGBM model (i.e., lgb.plot_metric(model)) I get the following error:

TypeError: booster must be dict or LGBMModel. To use plot_metric with Booster type, first record the metrics using record_evaluation callback then pass that to plot_metric as argument booster

But I cannot find any info in the documentation about parameters to set up the mentioned callback. Is there any way to implement this without resorting to the scikit-learn version of LightGBM?


Solution

  • The following should help to plot the metrics. I guess the documentation isn't really clear on usage but here is an example notebook. The evals dictionary contains an OrderedDict and can be plotted using the plot_metric method.

    train_dt = lgb.Dataset(data=train,label=train_y)
    valid_dt = lgb.Dataset(data = valid, 
                           label=valid_y, 
                           reference=train_dt)
    
    params = {
            'objective': 'regression',
            'metric': 'root_mean_squared_error',
            'num_leaves': 41,
    }
    evals={}
    mod = lgb.train(params=params, 
                    train_set = train_dt, 
                    valid_sets=[train_dt, valid_dt],
                    callbacks = [lgb.record_evaluation(evals)])
    
    lgb.plot_metric(evals)