close

[Solved] ValueError: feature_names mismatch: in xgboost in the predict() function

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: feature_names mismatch: in xgboost in the predict() function in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: feature_names mismatch: in xgboost in the predict() function Error Occurs?

Today I get the following error ValueError: feature_names mismatch: in xgboost in the predict() function in python.

How To Solve ValueError: feature_names mismatch: in xgboost in the predict() function Error ?

  1. How To Solve ValueError: feature_names mismatch: in xgboost in the predict() function Error ?

    To Solve ValueError: feature_names mismatch: in xgboost in the predict() function Error This is the case where the order of column-names while model building is different from order of column-names while model scoring.

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

    To Solve ValueError: feature_names mismatch: in xgboost in the predict() function Error This is the case where the order of column-names while model building is different from order of column-names while model scoring.

Solution 1

This is the case where the order of column-names while model building is different from order of column-names while model scoring.

I have used the following steps to overcome this error

First load the pickle file

model = pickle.load(open("saved_model_file", "rb"))

extraxt all the columns with order in which they were used

cols_when_model_builds = model.get_booster().feature_names

reorder the pandas dataframe

pd_dataframe = pd_dataframe[cols_when_model_builds]

Solution 2

Try converting data into ndarray before passing it to fit/predict. For eg: if your train data is train_df and test data is test_df. Use below code:

train_x = train_df.values
test_x = test_df.values

Now fit the model:

xgb.fit(train_x,train_y)

Finally, predict:

pred = xgb.predict(test_x)

Hope this helps!

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read