我们可以使用 iris 数据集,它有 3 个物种:
数据(虹膜)
表(iris$物种)
setosa versicolor virginica
50 50 50
我们拟合一个随机森林:
library(randomForest)
mdl = randomForest(Species~.,data=iris,importance=TRUE)
# let's do it without options
importance(mdl)
setosa versicolor virginica MeanDecreaseAccuracy
Sepal.Length 6.364533 6.2112640 7.632076 10.365371
Sepal.Width 4.790211 0.4339124 5.500338 5.153676
Petal.Length 22.027701 34.5777755 29.080648 35.215194
Petal.Width 22.500729 31.1403378 30.714576 33.335003
MeanDecreaseGini
Sepal.Length 9.223319
Sepal.Width 2.189763
Petal.Length 44.703684
Petal.Width 43.163546
上表是你的所有结果,如果你做重要性(mdl,type = 1)你会降低这个变量所有类的平均准确度。您会看到您可以预测的每个类别(setosa、versicolor、virginica)的三个单独的列,所以如果您这样做:
importance(mdl,type=1,class="setosa")
setosa
Sepal.Length 6.364533
Sepal.Width 4.790211
Petal.Length 22.027701
Petal.Width 22.500729
您可以更改与此类相关的准确性。
因此,在您的代码中,当您执行 importance(rf, type = 1, class = 1) 并且您的模型是 randomForest(y ~ ., data = df... ) 时,您正试图找出变量的重要性,该变量与 y 中标签为 1 的预测相关。
最后,您可以对它们进行排序:
res = importance(mdl,type=1,class="setosa")
res = res[order(res[,1],decreasing=TRUE),drop=FALSE,]
res