# How can I fit categorical data types for random forest classification?

I need to find the accuracy of a training dataset by applying Random Forest Algorithm. But my the type of my data set are both categorical and numeric. When I tried to fit those data, I get an error.

'Input contains NaN, infinity or a value too large for dtype('float32')'.

May be the problem is for object data types. How can I fit categorical data without transforming for applying RF?

Here's my code.

• You don't need to conduct one_hot if you are using a tree model,cause it is not measuring distance like other method. Jan 15 '19 at 7:53
• @JunYang, scikit-learn does currently require encoding categoricals. Apr 27 '20 at 21:29

You need to convert the categorical features into numeric attributes. A common approach is to use one-hot encoding, but that's definitely not the only option. If you have a variable with a high number of categorical levels, you should consider combining levels or using the hashing trick. Sklearn comes equipped with several approaches (check the "see also" section): One Hot Encoder and Hashing Trick

If you're not committed to sklearn, the h2o random forest implementation handles categorical features directly.

There are some problem for getting this types of error as far as I know. First one is, in my datasets there exists extra space that why showing error, 'Input Contains NAN value; Second, python is not able to work with any types of object value. We need to convert this object value into numeric value. For converting object to numeric there exist two type encoding process: Label encoder and One hot encoder. Where label encoder encode object value between 0 to n_classes-1 and One hot encoder encode value between 0 and 1. In my work, before fitting my data for any types of classification method I use Label encoder for converting value and before converting I ensure that no blank space exist in my data set.

Yes, you can numericalize with df.category_name.codes but you will see like 1 or 0 or -1 so you have to write a function like this.

def numericalize(df, col, name, max_n_cat):

if not is_numeric_dtype(col) and (max_n_cat is None or len(col.cat.categories) > max_n_cat):
df[name] = col.cat.codes + 1