-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosmanEnes.R
158 lines (119 loc) · 5.26 KB
/
osmanEnes.R
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
source(file = "Preparation.R") # just shows worked dataset after data preparation.
#################################################### Virtualize the mushroom
library(ggplot2)
ggplot(mushroom, aes(x = cap_surface, y = cap_color, col = class)) +
geom_jitter(alpha = 0.5) +
scale_color_manual(breaks = c("edible", "poisonous"),
values = c("green", "red"))
ggplot(mushroom, aes(x = cap_shape, y = cap_color, col = class)) +
geom_jitter(alpha = 0.5) +
scale_color_manual(breaks = c("edible", "poisonous"),
values = c("green", "red"))
ggplot(mushroom, aes(x = gill_color, y = cap_color, col = class)) +
geom_jitter(alpha = 0.5) +
scale_color_manual(breaks = c("edible", "poisonous"),
values = c("green", "red"))
ggplot(mushroom, aes(x = class, y = odor, col = class)) +
geom_jitter(alpha = 0.5) +
scale_color_manual(breaks = c("edible", "poisonous"),
values = c("green", "red"))
###################################################
################################################### Modelling...
set.seed(579642) #Set the seed for reproducibility
train_index <- sample(1:nrow(mushroom), size=nrow(mushroom)*0.8) # randomly choice rows
data_train <- mushroom[train_index,]
data_test <- mushroom[-train_index,]
trControl <- trainControl(method = "cv", number = 10, search = "grid")
############################# for ntree
## k fold cross validation takes too much time. So that, We optimize the 1-4 ntree.
start_time <- Sys.time()
ntreeAccuracy <- list()
for(i in c(1:20)){
set.seed(579642)
rf_ntree <- train(class~.,
data = data_train,
method = "rf",
metric = "Accuracy",
trControl = trControl,
ntree = i)
current_iteration <- toString(i)
ntreeAccuracy[[current_iteration]] <- mean(rf_ntree$results$Accuracy)
}
print(ntreeAccuracy)
plot(unlist(ntreeAccuracy), type="o" , bty="l" , ylab="Accuracy" , xlab="ntreeValues" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
meanNtreeAccuracy <- lapply(ntreeAccuracy, mean)
ntreeMax <- which.max(as.vector(unlist(meanNtreeAccuracy)))
############################# for mtry.
### mtry value continue as 1 after the 6.
tuneGrid <- expand.grid(.mtry = c(1: 6))
rf_mtry <- train(class~.,
data = data_train,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = trControl,
do.trace = TRUE, ## is given about of randomForest.
ntree = ntreeMax)
print(rf_mtry)
plot(rf_mtry, type="o" , bty="l" , ylab="Accuracy" , xlab="mtry Values" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
rf_mtry$results$Accuracy
mean(rf_mtry$results$Accuracy)
tuneGrid <- expand.grid(.mtry = rf_mtry$bestTune$mtry )
############################# for maxnode
AccuracyList <- c()
RecallList <- c()
F_ScoreList <- c()
PrecisionList <- c()
store_maxnode <- list()
indexes <- c()
for (maxnodes in c(5: 15)) {
set.seed(579642)
rf_maxnode <- train(class~.,
data = data_train,
method = "rf",
metric = "Accuracy",
trControl = trControl,
tuneGrid = tuneGrid,
importance = TRUE,
nodesize = 14,
maxnodes = maxnodes,
ntree = ntreeMax)
current_iteration <- toString(maxnodes)
store_maxnode[[current_iteration]] <- rf_maxnode$results$Accuracy
indexes <- c(indexes,maxnodes)
print(maxnodes)
prediction <-predict(rf_maxnode, data_test)
confusion_matrix <- confusionMatrix(prediction, data_test$class)
confusion_matrix
TP <- confusion_matrix$table[2,2]
TN <- confusion_matrix$table[1,1]
FP <- confusion_matrix$table[2,1]
FN <- confusion_matrix$table[1,2]
Accuracy <- (TP+TN)/(TP+TN+FP+FN)
AccuracyList <- c(AccuracyList,Accuracy)
Recall <- TP/(TP+FN)
RecallList <- c(RecallList,Recall)
F_Score <- 2*TP/(2*TP+FP+FN)
F_ScoreList <- c(F_ScoreList , F_Score )
Precision <- TP/(TP+FP)
PrecisionList <- c(PrecisionList , Precision )
}
AccuracyList
RecallList
F_ScoreList
PrecisionList
plot(AccuracyList, type="o" , bty="l" , xlab="maxNodeValues" , ylab="Accuracy" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
plot(RecallList, type="o" , bty="l" , xlab="maxNodeValues" , ylab="RecallList" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
plot(F_ScoreList, type="o" , bty="l" , xlab="maxNodeValues" , ylab="F_ScoreList" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
plot(PrecisionList, type="o" , bty="l" , xlab="maxNodeValues" , ylab="PrecisionList" , col=rgb(0.1,0.5,0.1,0.8) , lwd=0.5 , pch=16 )
mean(AccuracyList)
mean(RecallList)
mean(F_ScoreList)
mean(PrecisionList)
meanMaxnodesAccuracies <- lapply(store_maxnode, mean)
maxnodes <- which.max(as.vector(unlist(meanMaxnodesAccuracies)))
maxAccuracy <- max(as.vector(unlist(meanMaxnodesAccuracies)))
maxnodes <- indexes[maxnodes]
end_time <- Sys.time()
print(paste("running time => ", (end_time-start_time)))
## end.