【问题标题】:How to get the Gini coefficient using random forests in the caret R package?如何使用插入符号 R 包中的随机森林获得基尼系数?
【发布时间】:2015-07-22 15:50:56
【问题描述】:

我试图了解randomForest 包和caret 包中的随机森林实现之间的区别。

例如,这指定了 2000 棵树,mtry = 2randomForest 中,我显示了每个预测变量的基尼系数:

library(randomForest)
library(tidyr) 
rf1 <- randomForest(Species ~ ., data = iris, 
                      ntree = 2000, mtry = 2,
                      importance = TRUE)
data.frame(RF = sort(importance(rf1)[, "MeanDecreaseGini"], decreasing = TRUE)) %>% add_rownames() %>% rename(Predictor = rowname)
#      Predictor       RF
# 1  Petal.Width 45.57974
# 2 Petal.Length 41.61171
# 3 Sepal.Length  9.59369
# 4  Sepal.Width  2.47010

我正在尝试在caret 中获取相同的信息,但我不知道如何指定树的数量,或者如何获取基尼系数:

rf2 <- train(Species ~ ., data = iris, method = "rf",
              metric = "Kappa", 
              tuneGrid = data.frame(mtry = 2))
varImp(rf2) # not the Gini coefficient
#              Overall
# Petal.Length 100.000
# Petal.Width   99.307
# Sepal.Width    0.431
# qSepal.Length  0.000

另外,rf1 的混淆矩阵有一些错误,rf2 没有。是什么参数导致了这种差异?:

# rf1 Confusion matrix:
#            setosa versicolor virginica class.error
# setosa         50          0         0        0.00
# versicolor      0         47         3        0.06
# virginica       0          4        46        0.08

table(predict(rf2, iris), iris$Species)
#             setosa versicolor virginica
#  setosa         50          0         0
#  versicolor      0         50         0
#  virginica       0          0        50

这又快又脏。我知道这不是测试分类器性能的正确方法,但我不明白结果的差异。

【问题讨论】:

    标签: random-forest r-caret


    【解决方案1】:

    这可能有助于回答问题 - 请参阅第 2 篇文章:

    caret: using random forest and include cross-validation

    随机森林正在采样替换。如果在插入符号中使用“rf”,则需要在 train::caret(); 中指定 trControl;您希望在插入符号中使用相同的重采样方法,即引导程序,因此您需要设置 trControl="oob"。 TrControl 是定义函数行为方式的值列表;这可以设置为“cv”用于交叉验证,“repeatedcv”用于重复交叉验证等。有关更多信息,请参阅 caret 包文档。

    您应该得到与使用 randomForest 相同的结果,但请记住正确设置种子。

    【讨论】:

      【解决方案2】:

      我最近也在寻找从caretrandomForest 实现中获取MeanDecreasingGini 变量的解决方案。我意识到这是很久以前发布的,所以也许插入符号已经更新,我的建议不再需要,但我很难找到解决方案,所以希望有人觉得这很有用。

      要设置插入符号中的树数,您可以在训练期间使用 ntrees=xx 参数,就像使用 randomForest 一样。然后在caret 中输出MeanDecreasingGini 指定type=2 (1=MeanDecreasingAccuracy[default], 2=MeanDecreasingGini) 和scale=FALSE。完整代码和下面的结果(经过几次运行后,我预测的结果幅度会出现轻微波动,但变量的排名是一致的):

      library(randomForest)
      library(tidyr) 
      library(caret)
      
      ##randomForest
      rf1 <- randomForest(Species ~ ., data = iris, 
                          ntree = 2000, mtry = 2,
                          importance = TRUE)
      data.frame(Gini=sort(importance(rf1, type=2)[,], decreasing=T))
      # Gini
      # Petal.Width  43.924705
      # Petal.Length 43.293731
      # Sepal.Length  9.717544
      # Sepal.Width   2.320682
      
      ##caret
      rf2 <- train(Species ~ ., 
                   data = iris, 
                   method = "rf",
                   ntrees=2000, ##same as randomForest
                   importance=TRUE, ##same as randomForest
                   metric = "Kappa", 
                   tuneGrid = data.frame(mtry = 2),
                   trControl = trainControl(method = "none")) ##Stop the default bootstrap=25
      varImp(rf2, type=2, scale=FALSE)
      # rf variable importance
      # 
      # Overall
      # Petal.Width   44.475
      # Petal.Length  43.401
      # Sepal.Length   9.140
      # Sepal.Width    2.267
      

      然后就混淆矩阵混淆(混淆措辞?)而言,这似乎是您计算混淆矩阵的方式的副产品。当我对两个模型都使用预测函数时,与使用其他方法相比,我的准确率达到了 100%:

      rf1$confusion
      # setosa versicolor virginica class.error
      # setosa         50          0         0        0.00
      # versicolor      0         47         3        0.06
      # virginica       0          3        47        0.06
      
      table(predict(rf1, iris), iris$Species)
      # setosa versicolor virginica
      # setosa         50          0         0
      # versicolor      0         50         0
      # virginica       0          0        50
      
      rf2$finalModel$confusion
      # setosa versicolor virginica class.error
      # setosa         50          0         0        0.00
      # versicolor      0         47         3        0.06
      # virginica       0          5        45        0.10
      
      table(predict(rf2, iris), iris$Species)
      # setosa versicolor virginica
      # setosa         50          0         0
      # versicolor      0         50         0
      # virginica       0          0        50
      

      但是,我不确定rf1$confusionrf2$finalModel$confusion 是否都代表最后一棵树的预测。也许对此有更好理解的人可以提供帮助。

      【讨论】:

        猜你喜欢
        • 2016-04-10
        • 2012-06-03
        • 2016-05-11
        • 2020-07-07
        • 2016-08-10
        • 1970-01-01
        • 2015-06-15
        • 2015-10-08
        • 2021-05-19
        相关资源
        最近更新 更多