ValueError: feature_names mismatch: in xgboost in the predict() function

后端 未结 8 1858
悲哀的现实
悲哀的现实 2020-12-25 13:15

I have trained an XGBoostRegressor model. When I have to use this trained model for predicting for a new input, the predict() function throws a feature_names mismatch error,

8条回答
  •  渐次进展
    2020-12-25 13:21

    Check the exception. What you should see are two arrays. One is the column names of the dataframe you’re passing in and the other is the XGBoost feature names. They should be the same length. If you put them side by side in an Excel spreadsheet you will see that they are not in the same order. My guess is that the XGBoost names were written to a dictionary so it would be a coincidence if the names in then two arrays were in the same order.

    The fix is easy. Just reorder your dataframe columns to match the XGBoost names:

    f_names = model.feature_names
    df = df[f_names]
    

提交回复
热议问题