在参考了这个链接后,我成功实现了使用XGBoost
的增量学习。我想要构建一个分类器,并需要检查预测概率,即predict_proba()
方法。如果使用XGBoost
,这是不可能实现的。在实现XGBClassifier.fit()
而不是XGBoost.train()
时,我无法进行增量学习。XGBClassifier.fit()
的xgb_model
参数接收XGBoost
,而我想提供一个XGBClassifier
。
由于我需要使用predict_proba()
方法,那么XGBClassifier
是否可以进行增量学习呢?
可运行的代码:
上面的代码运行得很好,但没有retrained_model.predict_proba()
的选项。
不可运行的代码:
上面的代码无法运行,因为它期望加载一个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))
回答:
从文档中得知:
xgb_model – 存储的XGBoost模型的文件名或‘Booster’实例[。] 在训练前加载的XGBoost模型(允许训练继续进行)。
所以您应该能够使用xgb_model.get_booster()
来检索底层的Booster
实例并传递它。
此外,您确实可以从原生xgboost API中获取预测概率;当objective='binary:logistic'
时,Booster.predict
会返回概率。