6
$\begingroup$

I am used to R, in which you can use factor(variable) to indicate a categorical variable. However, in scikit-learn, trying to pass a variable of strings causes the DecisionTreeClassifier to give an error.

How are you supposed to use a categorical variable in scikit-learn? Is the only option for encoding a multi-class variable to use dummy variables?

$\endgroup$
3
$\begingroup$

To encode categorical feature as a scipy.sparse matrix you can use the DictVectorizer class. Then call the .toarray() method on the result to convert it to an contiguous numpy array as the scikit-learn trees do not support sparse input yet.

$\endgroup$

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.