xgboost.core.XGBoostError:需要事先调用fit-在尝试通过腌制模型进行预测时

时间:2018-10-02 19:47:57

标签: python xgboost

我创建了一个XGBoost分类器,并使用joblib以dat文件格式转储了python文件。

我能够使用joblib加载它,但是当我尝试使用加载的模型预测新数据时,我看到了错误

 xgboost.core.XGBoostError: need to call fit beforehand

我训练的原始模型是:

 XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
   colsample_bytree=0.6, eval_metric='error', gamma=0,
   learning_rate=0.125, max_delta_step=0, max_depth=8,
   min_child_weight=1, missing=None, n_estimators=600, n_jobs=1,
   nthread=None, objective='binary:logistic', random_state=0,
   reg_alpha=0.2, reg_lambda=0.8, scale_pos_weight=1, seed=None,
   silent=True, subsample=0.8)

腌制的模型是:

XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
   colsample_bytree=1, gamma=0, learning_rate=0.1, max_delta_step=0,
   max_depth=3, min_child_weight=1, missing=None, n_estimators=100,
   n_jobs=1, nthread=None, objective='binary:logistic',random_state=0,
   reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
   silent=True, subsample=1)

任何错误原因以及为何尽管我保存并加载相同模型却两者都不同?

1 个答案:

答案 0 :(得分:0)

使用拟合,保存然后重新加载模型的方式重新运行模型。

保存模型

joblib.dump(pipeline, "xgb1.joblib.dat")

从文件加载模型

loaded_model = joblib.load("xgb1.joblib.dat")

以这种方式加载模型后,错误将消失。对我有用。