-
Notifications
You must be signed in to change notification settings - Fork 129
/
permutation-importances-classifier.Rmd
195 lines (147 loc) · 5.87 KB
/
permutation-importances-classifier.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---
title: "Terence Parr Feat Imp"
author: "Christopher Csiszar"
date: "3/22/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = F, message = F)
```
## Biases in RF importance calculations
Simple RF Classification done in R to see how biases their RF feature importance metrics are
```{r cars}
# RF Classification - feature importances
library(tidyverse)
library(randomForest)
library(cowplot)
library(gridExtra)
setwd("~/Downloads/")
rent <- read.csv('rent.csv')
class(rent$interest_level)
#interest.map <- c("low"=1, "medium"=2, "high"=3)
#ent$interest_level <- interest.map[as.character(rent$interest_level)]
summary(rent)
## plotting functions ##
create_rfplot <- function(rf, type){
imp <- importance(rf, type=type, scale = F)
featureImportance <- data.frame(Feature=row.names(imp), Importance=imp[,1])
p <- ggplot(featureImportance, aes(x=reorder(Feature, Importance), y=Importance)) +
geom_bar(stat="identity", fill="#53cfff", width = 0.65) +
coord_flip() +
theme_light(base_size=20) +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.x = element_text(size = 15, color = "black"),
axis.text.y = element_text(size = 15, color = "black"))
return(p)
}
create_ggplot <- function(featureImportance){
p <- ggplot(featureImportance, aes(x=reorder(Feature, Importance), y=Importance)) +
geom_bar(stat="identity", fill="#53cfff", width = 0.65) +
coord_flip() +
theme_light(base_size=20) +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.x = element_text(size = 15, color = "black"),
axis.text.y = element_text(size = 15, color = "black"))
return(p)
}
```
```{r}
rent$interest_level <- as.factor(rent$interest_level)
head(rent)
```
## Type = 1, mean decrease in Accuracy
```{r cars3}
####### no random column #########
set.seed(1)
rent$random <- sample(100, size = nrow(rent), replace = TRUE)
#Fit Random Forest Model
rf1 = randomForest(interest_level ~ .,
ntree = 40,
data = rent[, 1:6],
nodesize = 1, importance = TRUE)
#print(rf)
importance(rf1, type = 1)
#round(importance(rf), 2)
# Variable Importance
k = varImpPlot(rf1,
sort = T,
main="Top - Variable Importance")
#p1 <- create_rfplot(rf1, type = 1)
#ggsave('../article/images/cls_permute_R.svg',
# plot = p1, device = 'svg', height = 4, width = 6)
######## with random column ########
#Fit Random Forest Model
rf2 = randomForest(interest_level ~ .,
ntree = 40,
data = rent,
nodesize = 1, importance = TRUE)
#print(rf)
#importance(rf2, type = 1)
imp1 <- data.frame(importance(rf2, type = 2))
write.csv(imp1, file="imp_R_class_gini.csv")
#round(importance(rf), 2)
imp1 <- data.frame(importance(rf2, type = 1))
write.csv(imp1, file="imp_R_class_acc.csv")
# Variable Importance
#varImpPlot(rf,
#sort = T,
#main="Top - Variable Importance")
#p2 <- create_rfplot(rf2, type = 1)
#ggsave('../article/images/cls_permute_random_R.svg',
# plot = p2, device = 'svg', height = 4, width = 6)
```
## Type = 2, mean decrease in Gini
```{r cars4}
####### no random column #########
#p1 <- create_rfplot(rf1, type = 2)
#ggsave('../article/images/cls_dflt_R.svg',
#plot = p1, device = 'svg', height = 4, width = 6)
######## with random column ########
#imp1 <- data.frame(importance(rf2, type = 2))
#write.csv(imp1, file="imp_R_class_gini.csv")
#p2 <- create_rfplot(rf2, type = 2)
#ggsave('../article/images/cls_dflt_random_R.svg',
#plot = p2, device = 'svg', height = 4, width = 6)
```
## Cost by dropping column analysis
```{r cars5, eval=FALSE}
####### no random column #########
get_drop_imp <- function(rent, columns){
X <- rent[,c(columns, 'interest_level')] # data
rf <- randomForest(interest_level~., data = X,
ntree = 40, mtry=2, nodesize=1, importance=T)
full_rsq <- -1*mean(rf$err.rate) #
imp <- c()
for (c in columns){
X_sub <- X[, !(colnames(X) == c)]
rf <- randomForest(interest_level~., data = X_sub,
ntree = 40, mtry=2, nodesize=1, importance=T)
sub_rsq <- -1*mean(rf$err.rate) #
diff_rsq <- full_rsq - sub_rsq
imp <- c(imp, diff_rsq)
}
featureImportance <- data.frame(Feature=columns, Importance=imp)
return(featureImportance)
}
columns <- c('bathrooms', 'bedrooms', 'longitude', 'latitude', 'price')
featureImportance <- get_drop_imp(rent[, 1:6], columns)
write.csv(featureImportance, file="imp_R_class_gini.csv")
#p1 <- create_ggplot(featureImportance)
#ggsave('../article/images/cls_drop_R.svg',
#plot = p1, device = 'svg', height = 4, width = 6)
columns <- c('bathrooms', 'bedrooms', 'longitude', 'latitude', 'random', 'price')
featureImportance <- get_drop_imp(rent, columns)
write.csv(featureImportance, file="imp_R_class_drop.csv")
#p2 <- create_ggplot(featureImportance)
#ggsave('../article/images/cls_drop_random_R.svg',
#plot = p2, device = 'svg', height = 4, width = 6)
```
## Takeaways
It appears that RF feature importance in R has several different metrics when evaluating. It seems that the "decrease in accuracy" metric places the `random` column dead last, as expected, while the "decrease in Gini" metric is terribly biased due to high cardinality, placing the `random` column as second most important.
Another thing to note is, due to low cardinality, `bedrooms` is a less important feature for Gini decrease metrics.
More of RF feature importance interpretation in R:
https://cran.r-project.org/web/packages/randomForest/randomForest.pdf
https://stats.stackexchange.com/questions/197827/how-to-interpret-mean-decrease-in-accuracy-and-mean-decrease-gini-in-random-fore
https://stackoverflow.com/questions/736514/r-random-forests-variable-importance