【发布时间】:2015-07-22 15:50:56
【问题描述】:
我试图了解randomForest 包和caret 包中的随机森林实现之间的区别。
例如,这指定了 2000 棵树,mtry = 2 在 randomForest 中,我显示了每个预测变量的基尼系数:
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