I am solving a classification problem using Random Forests. For that I have decided to use Python library scikit-learn. But I am new to both Random Forest algorithm and this tool. My data contains many factor variables. I googled for that and found out that it's not right to give numerical values to factor variables like we do in linear regression, as it will treat it as continuous variable and give wrong result. But I could not find anything about how to deal with factor variables in scikit-learn. Please tell me the options to use or point me to some document where I can get it.

  • $\begingroup$ Converting factors to continuous value is equally bad for RF as for linear regression -- it may decrease your accuracy but it is unlikely that this will completely spoil the model. $\endgroup$
    – user88
    Jun 18 '13 at 13:51

It seems that you're comparing scikit-learn's Random Forest with randomForest package in R, where this package deals with categorical variables automatically.

However in scikit-learn you have to preprocess your data yourself. To do this, you could use DictVectorizer class, which would create new binary features for every new value of your original feature.

  • $\begingroup$ Thanks for the suggestion. I was really confused what to do in these scenarios. $\endgroup$ Jun 24 '13 at 7:10
  • $\begingroup$ It's been two years since the last response suggesting to use DictVectorizer. I wonder if Random Forest in scikit-learn still do not handle categorial variables automatically. $\endgroup$
    – iamgin
    Aug 4 '15 at 14:33
  • $\begingroup$ Here's a PR where someone worked on it; unfortunately not done. github.com/scikit-learn/scikit-learn/pull/3346 $\endgroup$ Oct 24 '15 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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