'How to match the levels of a qualitative variable in test data in a model built in R and loaded in Python?

I have currently loaded a RandomForest model, built in R, to Google Colab in Python. Basically I used these steps:

https://goddoe.github.io/r/machine%20learning/2017/12/17/how-to-use-r-model-in-python.html

But when trying to predict new probabilities I get the following error:

RuntimeError: Error in predict.randomForest...
Type of predictors in new data do not match that of the training data

I had this same error running the predict function in R and I managed to solve it following the solution: https://stackoverflow.com/a/38097360

xtest <- rbind(xtrain[1, ] , xtest)
xtest <- xtest[-1,]

With that, the variable that previously had the amount of reduced levels in the test data, stayed with the same levels. But in Python I don't know how to leave a variable with the same amount of levels present in the training data, in the test data. Is there any way to do the equivalent I did in R without the need to use dummy variables?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source