在参考了此链接之后,我成功地实现了使用XGBoost
的增量学习。我想要建立一个分类器,并需要检查预测概率,即predict_proba()
方法。如果我使用XGBoost
,这是不可能的。在实现XGBClassifier.fit()
而不是XGBoost.train()
时,我无法执行增量学习。XGBClassifier.fit()
的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
)
以上代码运行良好,但没有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)
上面的代码不起作用,因为它期望加载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))
发布于 2021-03-25 18:17:03
从医生那里:
xgb_model -存储的XGBoost模型或“Booster”实例的文件名。要在培训前加载XGBoost模型(允许继续培训)。
因此,您应该能够使用xgb_model.get_booster()
检索底层Booster
实例并传递该实例。
此外,您还可以从本机xgboost API中获得预测的概率;Booster.predict
在objective='binary:logistic'
时返回概率。
https://stackoverflow.com/questions/66794560
复制相似问题