I have a data set given as follows: target shape (200000, 1) train_data.shape (200000, 48) test_data.shape(100000, 48)

I had used the data to predict_proba using RandomForestClassifier ExtraTreesClassifier RandomForestClassifier AdaBoostClassifier GradientBoostingClassifier and SVC and got results without an error.


clf = XGBClassifier() is showing following errors (with the same code except clf):

ValueError: feature_names mismatch:


3 Answers 3


The problem occurs due to DMatrix..num_col() only returning the amount of non-zero columns in a sparse matrix. Hence, if both train & test data have the same amount of non-zero columns, everything works fine. Otherwise, you end up with different feature names lists. There're currently three solutions to work around this problem:

  1. realign the columns names of the train dataframe and test dataframe using:

    test_df = test_df[train_df.columns]
  2. save the model first and then load the model

  3. change the test data into array before feeding into the model, ie: use,

    test_df.values instead of test_df
import numpy as np
train_data = np.array(train_data)
test_data = np.array(test_data)
clf = XGBClassifier()
pred = clf.predict_proba(test_data)

Use this instead of your original code.

  • $\begingroup$ Thanks I used clf.fit(train,y_train.values.ravel()) but still showing ValueError: feature_names mismatch: the error comes with the following: clf_pred = clf.predict_proba(test_data) is there another way to predict probabilities with XGBClassifier()? $\endgroup$
    – vizakshat
    Commented Jun 9, 2017 at 4:56
  • $\begingroup$ check your test data shape and format. data that you provide in training time is not matching with testing time. $\endgroup$ Commented Jun 9, 2017 at 5:16
  • $\begingroup$ Does the order of the columns in train_data and test_data have to do with this error? I didn't get the order with other classifiers! $\endgroup$
    – vizakshat
    Commented Jun 9, 2017 at 5:19
  • $\begingroup$ are you converting test_data as numpy array . like test_data = numpy.array(test_data) ??? $\endgroup$ Commented Jun 9, 2017 at 5:28
  • 1
    $\begingroup$ the predict function does not take the DataFrame (or a sparse matrix) as input. convert it numpy array , then give it as input. $\endgroup$ Commented Jun 9, 2017 at 5:30

i used this to resolve the issue. as the order of the columns in dataframe were not same.

train_data.sort_index(axis=1, inplace=True) test_data.sort_index(axis=1,inplace=True)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.