如何在 Light GBM 管道中使用 eval_result 方法?

How to make use of the eval_result method in Light GBM pipeline?

我使用最轻的 GBM 算法并创建了一个类似于以下内容的管道:

#model definition
model_lgbm = LGBMClassifier(
                #training loss
                objective='binary', # write a custom objective function that is cost sensitive
                n_estimators =  params['n_estimators'],
                max_depth =  params['max_depth'])

#pipeline instantiation using a previoulsy defined feature engineering pipeline (it does scaling etc.)
model_pipeline_lgbm = Pipeline(steps=[('preprocessor', feature_pipe_lgbm),
                                      ('model_lgbm', model_lgbm),
                                     ])

#fit of feature pipeline and transformation of validation sets
feature_pipe_lgbm.fit(X_train, Y_train)

X_val_trans = feature_pipe_lgbm.transform(X_val)
X_train_trans = feature_pipe_lgbm.transform(X_train)

encoded_column_names = ['f{}'.format(i) for i in range(X_val_trans.shape[1])]
X_val_trans = pd.DataFrame(data=X_val_trans, columns=encoded_column_names, index=X_val.index)

X_train_trans = pd.DataFrame(
    data=X_train_trans, columns=encoded_column_names, index=X_train.index)

#definition of evaluation set and evaluation metric
eval_metric = "binary_logloss"
eval_set = [(X_train_trans, Y_train), (X_val_trans, Y_val)]

然后我安装管道并希望将评估结果存储在字典中,如此 repo:

evals_result = {}
model_pipeline_lgbm.fit(X=X_train,
                        y=Y_train,
                        model_lgbm__eval_set=eval_set,
                        # validation loss
                        model_lgbm__eval_metric=eval_metric, #same here consider cost sensitvity
                        model_lgbm__early_stopping_rounds= params['early_stopping_patience'],
                        model_lgbm__evals_result=evals_result
                        )

但是,我收到以下错误:

TypeError: fit() got an unexpected keyword argument 'evals_result'

您知道我需要在我的管道中的什么地方定义 eval_results,以便我可以调用它来创建图吗?

您应该可以在 .fit 调用后通过 LGBMClassifier 访问它:

model_pipeline_lgbm.fit(...)

model_pipeline_lgbm.steps['model_lgbm'].evals_result_

谢谢@Berriel,你给了我缺失的信息。我只是没有正确访问管道步骤。最终这奏效了:

model_pipeline_lgbm.steps[1][1].evals_result_