extract used column names/features from decision tree

Viewed 180

Could someone please be so kind and point out a way of extracting the columns/features used in the fitted tree, using code like this:

library(dplyr)
library(caret)
library(rpart)

df <- data.frame(
    x1 = rnorm(100, 3, 1)
    , x2 = rnorm(100, 5, 2)
    , y = rnorm(100, 3, 1)
)

ivs <- df %>%
    dplyr::select(
        x1
        , x2
    )

fitted <- caret::train(
     x = ivs
     , y = df$y        
)

model <-fitted$finalModel
model

This:

model$terms

gives you all the terms (in x) - so not necessarily the once used.

PS:

Something along those lines may be the way forward:

model$frame %>%
    select(
        var
    ) %>%
    filter(
        var != "<leaf>"
    )
1 Answers

The randomforest model is an ensembl of decision trees, you can use the function varUsed to get the variable used by tree. If you are using caret, you need to set keep.forest=TRUE :

library(caret)
set.seed(111)

X = matrix(runif(2000),ncol=20)
colnames(X) = paste0("col",1:20)
y = rnorm(100)

fitted <- train(x = X,y = y,trControl = trainControl(method="cv"),
keep.forest=TRUE,tuneGrid = data.frame(mtry=3:5))  

And we look at the variables used, the table below tells you for each variable(row), how many times it's used in each tree(column):

model <-fitted$finalModel
varUsed(model,by.tree=TRUE)

dim(varUsed(model,by.tree=TRUE))
[1]  20 500

head(varUsed(model,by.tree=TRUE))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    3    1    4    1    2    2    2    2     2     1     0     3     2
[2,]    2    4    2    2    3    3    1    4    1     3     1     4     0     4
[3,]    3    1    5    1    1    0    2    2    0     1     2     1     2     1
[4,]    4    0    2    3    1    0    2    1    4     2     1     2     1     1
[5,]    0    0    1    0    1    4    3    2    2     2     3     4     0     0
[6,]    1    2    1    3    2    2    2    1    1     1     1     1     2     3

You can check this in detail using:

head(getTree(model,1,labelVar=TRUE))
  left daughter right daughter split var split point status  prediction
1             2              3     col13   0.9651637     -3  0.04972823
2             4              5     col13   0.5825061     -3 -0.05466984
3             0              0      <NA>   0.0000000     -1  2.55528193
4             6              7      col6   0.8524606     -3 -0.30132135
5             8              9     col16   0.8011010     -3  0.51518018
6            10             11      col3   0.3629737     -3 -0.45289996

If we tabulate the split var column we get the same result as the previous table.

Related