简体   繁体   中英

How to handle with categorical values over 53 factor levels using randomForest?

I have a training dataset as you can see below:

'data.frame':   229907 obs. of  19 variables:
$ categories             : Factor w/ 2061 levels  "","Accessories,Fashion,Shopping,Cosmetics & Beauty Supply,Beauty & Spas",..: 253 1541 1720 1647 38 396 522 1727 482 641 ...
$ city                   : Factor w/ 61 levels "Ahwatukee","Anthem",..: 18 38 38 38 38 38 38 51 31 43 ...
$ latitude               : num  33.3 33.5 33.5 33.5 33.5 ...
$ longitude              : num  -112 -112 -112 -112 -112 ...
$ open                   : Factor w/ 2 levels "False","True": 2 2 2 2 2 2 2 2 2 2 ...
$ review_count.x         : int  26 127 130 26 8 229 453 24 3 126 ...
$ stars.x                : num  4.5 3.5 4 4 4.5 3.5 4 4 2.5 3.5 ...
$ state                  : Factor w/ 4 levels "AZ","CA","CO",..: 1 1 1 1 1 1 1 1 1 1 ...
$ date                   : Factor w/ 2504 levels "2005-03-07","2005-03-08",..: 2031 1649 1936 1936 2001 1936 1936 2312 2056 1874 ...
$ stars.y                : int  5 4 4 5 4 3 5 5 1 4 ...
$ votes_cool             : int  0 0 1 0 0 0 1 1 0 0 ...
$ votes_funny            : int  0 0 1 0 0 0 1 1 1 0 ...
$ votes_useful           : int  0 0 1 0 1 0 2 1 2 0 ...
$ average_stars          : num  5 4.67 4.43 4.43 4.43 4.43 4.43 4.43 2.75 3.65 ...
$ name.y                 : Factor w/ 8323 levels "a","a.","A","A.",..: 3841 6354 7263 7263 7263 7263 7263 7263 5372 6556 ...
$ review_count.y         : int  2 4 7 7 7 7 7 7 4 20 ...
$ Total_votes_cool_user  : int  1 0 4 4 4 4 4 4 0 7 ...
$ Total_votes_funny_user : int  0 0 3 3 3 3 3 3 1 5 ...
$ Total_votes_useful_user: int  2 0 6 6 6 6 6 6 3 32 ...

My goal is to apply the randomForest algorithm, but randomForest only embrace factors till level 53. Any suggestion to solve this? I have thought about turning all categorical values into integers, but I think it would not help for thereafter prediction efficiency desired. Also I have had problems with NA values. I used rfImput for replacing them and I also had the same problems.

Thanks,

Many of your "factors" are not really factors at all. date is not a factor and should be converted to date. name should be a string. The only one that probably could be seen as a factor is category but not as it is now. You need to parse it and separate all the categories. A link to help you out (maybe): https://www.stat.berkeley.edu/classes/s133/factors.html . userid is not a factor (since possibly each observation has a different value), and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM