14
$\begingroup$

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.

screenshot

screenshot

screenshot

$\endgroup$
2
  • $\begingroup$ You don't need to conduct one_hot if you are using a tree model,cause it is not measuring distance like other method. $\endgroup$
    – Jun Yang
    Jan 15, 2019 at 7:53
  • 2
    $\begingroup$ @JunYang, scikit-learn does currently require encoding categoricals. $\endgroup$
    – Ben Reiniger
    Apr 27, 2020 at 21:29

3 Answers 3

21
$\begingroup$

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.

$\endgroup$
0
0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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