执行 XGBClassifier 的增量学习

Ami*_*hak 5 python machine-learning xgboost

参考此链接后,我能够使用 成功实现增量学习XGBoost。我想构建一个分类器,需要检查预测概率,即predict_proba()方法。如果我使用的话这是不可能的XGBoost。在实施时XGBClassifier.fit()XGBoost.train()我无法执行增量学习。xgb_model的参数需要XGBClassifier.fit()时间XGBoost,而我想提供一个XGBClassifier.

XGBClassifier由于我需要使用方法,是否可以进行增量学习predict_proba()

工作代码:

import XGBoost as xgb

train_data = xgb.DMatrix(X, y)
model = xgb.train(
    params = best_params, 
    dtrain = train_data, 
)

new_train_data = xgb.DMatrix(X_new, y_new)
retrained_model = xgb.train(
    params     = best_params, 
    dtrain     = new_train_data, 
    xgb_model  = model
)
Run Code Online (Sandbox Code Playgroud)

上面的代码运行完美,但没有选项retrained_model.predict_proba()

非工作代码:

import XGBoost as xgb

xgb_model = xgb.XGBClassifier(**best_params)
xgb_model.fit(X, y)

retrained_model = xgb.XGBClassifier(**best_params)
retrained_model.fit(X_new, y_new, xgb_model = xgb_model)
Run Code Online (Sandbox Code Playgroud)

上面的代码不起作用,因为它需要加载一个XGBoost或多个模型。Booster instance XGBoost

错误跟踪:

[11:27:51] WARNING: ../src/learner.cc:1061: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.
Traceback (most recent call last):
  File "/project/Data_Training.py", line 530, in train
    retrained_model.fit(X_new, y_new, xgb_model = xgb_model)
  File "/home/user/.local/lib/python3.6/site-packages/xgboost/core.py", line 422, in inner_f
    return f(**kwargs)
  File "/home/user/.local/lib/python3.6/site-packages/xgboost/sklearn.py", line 915, in fit
    callbacks=callbacks)
  File "/home/user/.local/lib/python3.6/site-packages/xgboost/training.py", line 236, in train
    early_stopping_rounds=early_stopping_rounds)
  File "/home/user/.local/lib/python3.6/site-packages/xgboost/training.py", line 60, in _train_internal
    model_file=xgb_model)
  File "/home/user/.local/lib/python3.6/site-packages/xgboost/core.py", line 1044, in __init__
    raise TypeError('Unknown type:', model_file)
TypeError: ('Unknown type:', XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
              importance_type='gain', interaction_constraints='',
              learning_rate=1, max_delta_step=0, max_depth=3,
              min_child_weight=1, missing=nan, monotone_constraints='()',
              n_estimators=100, n_jobs=32, num_parallel_tree=1, random_state=0,
              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=0.7,
              tree_method='exact', validate_parameters=1, verbosity=None))
Run Code Online (Sandbox Code Playgroud)

Ben*_*ger 3

来自文档:

\n
\n

xgb_model \xe2\x80\x93 存储的 XGBoost 模型的文件名或 \xe2\x80\x98Booster\xe2\x80\x99 实例[.] 在训练之前加载的 XGBoost 模型(允许继续训练)。

\n
\n

因此,您应该能够用来xgb_model.get_booster()检索底层Booster实例并传递它。

\n
\n

此外,您还可以从本机 xgboost API 中获取预测概率;Booster.predict返回 时的概率objective='binary:logistic'

\n