9
$\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$
  • $\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 at 7:53
7
$\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
$\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$

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.