【问题标题】:Random Forest: Predictors have more than 53 categories? [duplicate]随机森林:预测器有超过 53 个类别? [复制]
【发布时间】:2018-04-24 13:03:30
【问题描述】:
library(randomForest)
set.seed(113, "L'Ecuyer")      
plot(randomForest(cmedv ~ .,  data = BostonHousing2,
        keep.forest = FALSE))

Error in randomForest.default(m, y, ...) : Can not handle categorical 
predictors with more than 53 categories.

这是我的数据集的str

str(BostonHousing2)

$ town   : Factor w/ 92 levels "Arlington","Ashland",..: 54 77 77 46 46 46 69 
        69 69 69 ...

$ tract  : int  2011 2021 2022 2031 2032 2033 2041 2042 2043 2044 ...

$ lon    : num  -71 -71 -70.9 -70.9 -70.9 ...

$ lat    : num  42.3 42.3 42.3 42.3 42.3 ...

$ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...

$ cmedv  : num  24 21.6 34.7 33.4 36.2 28.7 22.9 22.1 16.5 18.9 ...

$ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...

$ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...

$ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...

$ chas   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...

$ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...

$ rm     : num  6.58 6.42 7.18 7 7.15 ...

$ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...

$ dis    : num  4.09 4.97 4.97 6.06 6.06 ...

$ rad    : int  1 2 2 3 3 3 5 5 5 5 ...

$ tax    : int  296 242 242 222 222 222 311 311 311 311 ...

$ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...

$ b      : num  397 397 393 395 397 ...

$ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...

【问题讨论】:

  • 它告诉你问题。输出显示问题存在。你不问任何问题。你想要什么
  • 这个错误是不言自明的;您的预测器 town 有 92 个级别,randomForest 最多允许 53 个级别。请参阅this post on Cross Validated 进行扩展讨论。
  • 我从数据集中删除了 town 和 medv 列,然后我使用了 randomforest 函数

标签: r dataframe linear-regression


【解决方案1】:

随机森林对处理超过 32 级的分类值有限制,因此可以减少分类值的级别。 为了减少分类值,您可以使用分箱方法,例如 dplyr 中的 decile 使用 ntile() 。它会降低到较低的水平。

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 2014-08-07
    • 2021-03-21
    • 2019-05-04
    • 2014-10-11
    • 2020-10-15
    • 2019-07-10
    • 2021-06-23
    相关资源
    最近更新 更多