Jay
Jay

Reputation: 43

xgboost.core.XGBoostError: need to call fit beforehand - when trying to predict through a pickled model

I have created an XGBoost classifier and dumped the python file using joblib in a dat fileformat.

I am able to load it using joblib but when I try to use the loaded model to predict on new data, I see the error

 xgboost.core.XGBoostError: need to call fit beforehand

The original model that i trained was:

 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)

The pickled model is:

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)

Any reason for the error and why both differ despite me saving and loading the same model?

Upvotes: 2

Views: 11566

Answers (1)

kevin_theinfinityfund
kevin_theinfinityfund

Reputation: 2157

Re-run your model with fit, save, and then load pipeline file like normal.

Save Model

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

load model from file

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

After loading the model this way the error will go away. It did for me.

Upvotes: 3

Related Questions