2016-05-17 75 views
5

我訓練了一個隨機森林使用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. 

任何想法,如果&我怎麼能得到它?

感謝。

回答

4

varImp(fit)將爲您提供。

爲了解決這個問題,我查看了names(fit),這導致我得到了names(fit$modelInfo) - 那麼您將看到varImp作爲其中一個選項。

+2

是的,我也發現它同時也是通過深入'caret'文檔。不過謝謝你找到信息的有用方法!事實證明'varImp()'是通過脫字符的'train()'訓練的大多數模型變得重要的方法。注意未來的用戶:雖然我不是100%確定的,沒有時間檢查,但似乎有必要有'important ='雜質'(我猜'重要性='permutation''也可以工作)作爲'train()'中的參數傳遞,以便能夠使用'varImp()'。 –

+2

另一個說明:看起來如果你用'ranger'訓練你的模型,但沒有'caret',那麼'重要性(fit)'是獲得不同重要性的正確方法。如上所述,我認爲參數'重要性=雜質'(或'排列')需要在train()中。 –

+0

奇怪它不適合我。沒有重要的值可用...嗯 –

0

根據@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"或​​來論證importance。 兩個值的描述可以在這裏找到:http://alexperrier.github.io/jekyll/update/2015/08/27/feature-importance-random-forests-gini-accuracy.html

0

對於「遊俠」包你可以調用與

fit$variable.importance 

的重要性,作爲一個側面說明,你可以使用看到所有可用輸出的模型str()

str(fit)