There are several posts about how to encode categorical data to Sklearn Decission trees, but from Sklearn documentation, we got these:
But running the following script:
Use tools provided by Scikit-Learn for this purpose. The main reason for doing so is that they can be easily integrated in a Pipeline. Scikit-Learn itself provides very good classes to handle categorical data. Instead of writing your custom function, you should use LabelEncoder which is specially designed for this purpose.
Refer to the following code from the documentation:
Also note that for many other classifiers, apart from decision trees, such as logistic regression or SVM, you would like to encode your categorical variables using One-Hot encoding. Scikit-learn supports this as well through the OneHotEncoder class. So the origin code can be rewritten as:
* FAQ - Assigning to columns in NumPy?