我使用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.
任何想法是否&amp;我怎么能得到它?
感谢。
答案 0 :(得分:8)
varImp(fit)
会为您提供。
为了解决这个问题,我查看了names(fit)
,这导致我names(fit$modelInfo)
- 然后您会看到varImp
作为其中一个选项。
答案 1 :(得分: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
答案 2 :(得分:3)
对于&#39;游侠&#39;你可以用
来称呼重要性fit$variable.importance
作为旁注,您可以使用str()
查看模型的所有可用输出str(fit)