# XGBClassifier error! ValueError: feature_names mismatch:

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.

# BUT

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

ValueError: feature_names mismatch:

import numpy as np
train_data = np.array(train_data)
test_data = np.array(test_data)
clf = XGBClassifier()
clf.fit(train,y_train.ravel())
pred = clf.predict_proba(test_data)


• 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()? – vizakshat Jun 9 '17 at 4:56
• check your test data shape and format. data that you provide in training time is not matching with testing time. – Abhishek Verma Jun 9 '17 at 5:16
• 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! – vizakshat Jun 9 '17 at 5:19
• are you converting test_data as numpy array . like test_data = numpy.array(test_data) ??? – Abhishek Verma Jun 9 '17 at 5:28
• the predict function does not take the DataFrame (or a sparse matrix) as input. convert it numpy array , then give it as input. – Abhishek Verma Jun 9 '17 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)

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