【问题标题】:Variable importance with ranger游侠的可变重要性
【发布时间】:2016-09-13 18:43:44
【问题描述】:

我使用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'
)

现在我想看看变量的重要性。但是,这些都不起作用:

> 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. 

知道是否以及如何获得它吗?

谢谢。

【问题讨论】:

    标签: r machine-learning random-forest r-caret


    【解决方案1】:

    varImp(fit) 会帮你拿的。

    为了弄清楚这一点,我查看了names(fit),它引导我找到names(fit$modelInfo) - 然后你会看到varImp 作为选项之一。

    【讨论】:

    • 是的,同时我也通过深入研究caret 的文档找到了它。不过,感谢您提供有用的信息查找方法!事实证明,varImp() 是为大多数使用插入符号的train() 训练的模型获得可变重要性的方法。但对未来用户的注意:我不是 100% 确定,也没有时间检查,但似乎有必要将 importance = 'impurity'(我猜 importance = 'permutation' 也可以)作为参数传递给 train()能够使用varImp()
    • 另一个注意事项:如果你用ranger 训练你的模型但没有caret,那么importance(fit) 将是获得变量重要性的正确方法。如上所述,我认为参数importance = 'impurity'(或'permutation')需要在train()
    • 奇怪的是它不适合我。没有可用的重要性值...嗯
    • 这对我不起作用。该函数存在,但它没有返回可用的重要性值?
    • 明确一点,ranger 的默认值是不计算 importance。您必须明确指定 importance = 'impurity'importance = 'permutation' 才能使这些方法中的任何一个起作用,即使您使用的是 train
    【解决方案2】:

    对于“游侠”包,您可以使用

    来调用重要性
    fit$variable.importance
    

    附带说明,您可以使用 str() 查看模型的所有可用输出

    str(fit)
    

    【讨论】:

      【解决方案3】:

      根据@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)
      

      您可以将"permutation""impurity" 传递给参数importance。 这两个值的描述可以在这里找到:https://alexisperrier.com/datascience/2015/08/27/feature-importance-random-forests-gini-accuracy.html

      【讨论】:

        猜你喜欢
        • 2023-02-14
        • 1970-01-01
        • 2015-06-04
        • 2021-02-15
        • 2016-01-25
        • 2021-05-26
        • 1970-01-01
        • 2020-10-27
        • 2018-11-19
        相关资源
        最近更新 更多