Feature Importance in (Caret) package
Solution 1:
Per the varImp()
documentation, the scale
argument in the caret::varImp()
function scales the variable importance values from 0 to 100.
Absent a reproducible example, we'll use the vowel data from the Elements of Statistical Learning book to generate a random forest, and rescale the variable importance data so the sum is equal to 1 by dividing each variable importance number by the sum of all importance numbers.
library(readr)
vowel.train <- subset(read_csv("https://web.stanford.edu/~hastie/ElemStatLearn/datasets/vowel.train"),
select = -row.names)
vowel.test <- subset(read_csv("https://web.stanford.edu/~hastie/ElemStatLearn/datasets/vowel.test"),
select = -row.names)
library(caret)
library(randomForest)
vowel.train$y <- as.factor(vowel.train$y)
vowel.test$y <- as.factor(vowel.test$y)
set.seed(33833)
tr1Control <- trainControl(method="boot")
modFit <- train(y ~ .,method="rf",trControl=tr1Control,data=vowel.train)
# Variable Importance: caret function, extract importance data frame & rescale
v <- varImp(modFit,scale = TRUE)[["importance"]]
v$Overall <- v$Overall / sum(v$Overall)
v
..and the output:
> v
Overall
x.1 0.318660495
x.2 0.327734091
x.3 0.018931795
x.4 0.021533916
x.5 0.126744531
x.6 0.089627688
x.7 0.000000000
x.8 0.067066743
x.9 0.027072197
x.10 0.002628545
...and to demonstrate that sum(v$Overall)
is now 1:
sum(v$Overall)
> sum(v$Overall)
[1] 1