Is it a problem if the test data only has a subset of the features that are used to train the xgboost model? All my predictor variables (except 1) are factors, so one hot encoding is done before converting it into xgb.DMatrix. So the different levels of the factor variables become the features and my test doesn't have all of these features, only a subset of it.

At the moment, while running my model on test data in R, I 'm running into an error saying that "Features names stored in object and newdata are different!".

I'm new in the field, so any help would be much appreciated. Thanks!


1 Answer 1


All the variables used to train the model must be present in the test set.

This is because you used all the variables to create the rules. Hence we would need those to score them.

If you are using python to do one hot encoding using fit or fit_transform functions in sklearn you will use the same object to transform the test set using transform function.

This will ensure the variables to be consistent in test and train.

  • $\begingroup$ When you say variables, does it also mean that all levels of the variables must be present? Because all my predictor variables are factors and some levels of these variables are not present in the test set. $\endgroup$
    – FMJ
    Jun 14, 2019 at 7:36
  • $\begingroup$ If your train dataset has all the factors and you have done a fit transform on one hot encoding . If you use the same object to transform your test dataset it will create all the factors in train dataset as columns with their values set to 0. $\endgroup$ Jun 14, 2019 at 7:40
  • $\begingroup$ If possible can you share your code. We can identify the issue and fix it $\endgroup$ Jun 14, 2019 at 7:41
  • $\begingroup$ Thank you! My code is in R. Will you be able to share how to do the fit transform in R? $\endgroup$
    – FMJ
    Jun 14, 2019 at 7:49
  • $\begingroup$ I figured it out in R and fixed the issue using dummyVars from caret package and then using this object to transform train and test. Thanks a lot for clarifying my query and pointing me in the right direction! $\endgroup$
    – FMJ
    Jun 14, 2019 at 12:39

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.