4
$\begingroup$

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:

$\endgroup$

3 Answers 3

6
$\begingroup$

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
    
$\endgroup$
2
$\begingroup$
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)

Use this instead of your original code.

$\endgroup$
12
  • $\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
    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$ 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
    Jun 9, 2017 at 5:19
  • $\begingroup$ are you converting test_data as numpy array . like test_data = numpy.array(test_data) ??? $\endgroup$ 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$ Jun 9, 2017 at 5:30
2
$\begingroup$

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)

$\endgroup$

Your Answer

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

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