[英]Variable Importance for Individual classes in R using Caret
I have used a random forest for predicting classes.我使用随机森林来预测课程。 Now, I am trying to plot variable importance for each class. I have used the below code, but it does not provide me varImp class wise, it is giving me for whole model. Can someone please help me.
现在,我正在尝试为每个 class 设置 plot 变量重要性。我使用了下面的代码,但它并没有为我提供 varImp class 明智的,它为我提供了整个 model。有人可以帮助我吗?
Thank you.谢谢。
odFit = train(x = df_5[,-22],
y = df_5$`kpres$cluster`,
ntree=20,method="rf",metric = "Accuracy",trControl = control,tuneGrid = tunegrid
)
odFit
varImp(odFit)
Just add importance=TRUE
in the train
function, which is the same to do importance(odFit)
in the randomForest
package.只需在
train
function 中添加importance=TRUE
,这与在randomForest
森林 package 中执行importance(odFit)
相同。
Here a reproducible example:这是一个可重现的例子:
library(caret)
data(iris)
control <- trainControl(method = "cv",10)
tunegrid <- expand.grid(mtry=2:ncol(iris)-1)
odFit = train(x = iris[,-5],
y = iris$Species,
ntree=20,
trControl = control,
tuneGrid = tunegrid,
importance=T
)
odFit
varImp(odFit)
and here is the output这是 output
rf variable importance
variables are sorted by maximum importance across the classes
setosa versicolor virginica
Petal.Width 57.21 73.747 100.00
Petal.Length 61.90 79.981 77.49
Sepal.Length 20.01 2.867 40.47
Sepal.Width 20.01 0.000 15.73
you can plot the variable importance with ggplot
您可以使用
ggplot
计算 plot 变量的重要性
library(ggplot2)
vi <- varImp(odFit,scale=T)[[1]]
vi$var <-row.names(vi)
vi <- reshape2::melt(vi)
ggplot(vi,aes(value,var,col=variable))+
geom_point()+
facet_wrap(~variable)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.