简体   繁体   English

游侠的重要性

[英]Variable importance with ranger

I trained a random forest using caret + ranger . 我使用caret + ranger训练了一个随机森林。

fit <- train(
    y ~ x1 + x2
    ,data = total_set
    ,method = "ranger"
    ,trControl = trainControl(method="cv", number = 5, allowParallel = TRUE, verbose = TRUE)
    ,tuneGrid = expand.grid(mtry = c(4,5,6))
    ,importance = 'impurity'
)

Now I'd like to see the importance of variables. 现在我想看看变量的重要性。 However, none of these work : 但是,这些都不起作用:

> importance(fit)
Error in UseMethod("importance") : no applicable method for 'importance' applied to an object of class "c('train', 'train.formula')"
> fit$variable.importance
NULL
> fit$importance
NULL

> fit
Random Forest 

217380 samples
    32 predictors

No pre-processing
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 173904, 173904, 173904, 173904, 173904 
Resampling results across tuning parameters:

  mtry  RMSE        Rsquared 
  4     0.03640464  0.5378731
  5     0.03645528  0.5366478
  6     0.03651451  0.5352838

RMSE was used to select the optimal model using  the smallest value.
The final value used for the model was mtry = 4. 

Any idea if & how I can get it ? 知道我是否以及如何获得它?

Thanks. 谢谢。

varImp(fit) will get it for you. varImp(fit)会为你得到它。

To figure that out, I looked at names(fit) , which led me to names(fit$modelInfo) - then you'll see varImp as one of the options. 为了解决这个问题,我查看了names(fit) ,这导致了我的names(fit$modelInfo) - 然后你会看到varImp作为选项之一。

For 'ranger' package you could call an importance with 对于'游侠'套餐,你可以称之为重要

fit$variable.importance

As a side note, you could see the all available outputs for the model using str() 作为旁注,您可以使用str()查看模型的所有可用输出

str(fit)

according to @fmalaussena 据@fmalaussena说

set.seed(123)
ctrl <- trainControl(method = 'cv', 
                     number = 10,
                     classProbs = TRUE,
                     savePredictions = TRUE,
                     verboseIter = TRUE)

rfFit <- train(Species ~ ., 
               data = iris, 
               method = "ranger",
               importance = "permutation", #***
               trControl = ctrl,
               verbose = T)

You can pass either "permutation" or "impurity" to argument importance . 您可以将"permutation""impurity"传递给参数importance The description for both value can be found here: https://alexisperrier.com/datascience/2015/08/27/feature-importance-random-forests-gini-accuracy.html 有关这两个值的说明,请访问: https//alexisperrier.com/datascience/2015/08/27/feature-importance-random-forests-gini-accuracy.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM