-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMLProject.R
228 lines (180 loc) · 7.14 KB
/
MLProject.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#install.packages("Amelia")
#install.packages("caret")
library(arm)
library(caret)
library(purrr)
library(Amelia)
library(pROC)
#include data preparation file
source(file = "Preparation.R") # just shows worked dataset after data preparation.
#----------------
TRAIN_SIZE = 0.8
NUM_OF_FOLD = 10
TRAIN_DATA = list()
#show current dataset
#head(mushroom)
#summary(mushroom)
set.seed(14536) #Set the seed for reproducibility
#Create 10 equally size folds
folds <- cut(seq(1,nrow(mushroom)),breaks=NUM_OF_FOLD,labels=FALSE)
#begin the logistic function (Model fitting)
AccList <- NULL
RecList <- NULL
FscList <- NULL
PrcList <- NULL
SnvList <- NULL
SpcList <- NULL
RnnTime <- NULL
for(i in 1:(NUM_OF_FOLD)){
#Segement your data by fold using the which() function
testIndexes <- which(folds==i,arr.ind=TRUE)
test <- mushroom[testIndexes, ]
train <- mushroom[-testIndexes, ]
start_time <- Sys.time()
model = glm(formula = gill_spacing ~ ., data = train, family = binomial())
#summary(model)
#NA değerler var, bunun sebebi birden fazla attribute un iyi bir şekilde eşleşmesi
#bu durumu istemiyoruz o yüzden NA'lı attributeları kaldırıyoruz
drops <- c("stalk_color_above_ring","stalk_color_below_ring","veil_color","ring_number"
,"ring_type","spore_print_color","habitat") #we remove this column in our dataset.
train <- train[ , !(names(train) %in% drops)] #remove
test <- test[ , !(names(test) %in% drops)] #remove
#Warning: glm.fit: algorithm did not converge hatası iterasyon sayısı ile ilgili
#default olarak maxit=25'dir biz 100 yapıyoruz
model <- glm(gill_spacing ~ ., data = train,family = binomial,maxit = 100)
#summary(model)
#şimdi modeli test datamız üzerinde test ediyoruz
predicted <- predict.glm(model,newdata = test[,-8],type = "response")
#tahmin edilen değerler ortalama üzeri olanlar poison
predicted <- predicted >= mean(predicted)
predicted <- gsub("FALSE","close",predicted)
predicted <- gsub("TRUE","crowded",predicted)
actual <- test[,8]
train_err <- mean(predicted != train$gill_spacing)
confusion_matrix <- table(predicted,actual)
TP <- confusion_matrix[2,2]
TN <- confusion_matrix[1,1]
FP <- confusion_matrix[2,1]
FN <- confusion_matrix[1,2]
Accuracy <- (TP+TN)*100/(TP+TN+FP+FN)
Recall <- TP*100/(TP+FN)
F_Score <- 2*TP*100/(2*TP+FP+FN)
Precision <- TP*100/(TP+FP)
Sens <- TP*100/(TP+FN)
Spec <- TN*100/(TN+FP)
end_time <- Sys.time()
AccList <- c(AccList, Accuracy)
RecList <- c(RecList, Recall)
FscList <- c(FscList, F_Score)
PrcList <- c(PrcList, Precision)
SnvList <- c(SnvList,Sens)
SpcList <- c(SpcList,Spec)
RnnTime <- c(RnnTime,(end_time-start_time))
print(paste(i, "-> running time => ", (end_time-start_time), "Accuracy : ", Accuracy,
" Recall : ", Recall
," F-Score : ", F_Score
," Precision : ", Precision
,"Sensivity : ",Sens
,"Specificity : ",Spec))
# Calculate sensitivity and false positive measures for logit model
}
#Draw data
library(ggplot2)
library(gridExtra)
plot(RnnTime,type = "o", ylab="Running time for gill_spacing", xlab =mean(RnnTime),
border="blue", col=rainbow(3))
plot(AccList,type = "o", ylab="Accuracy Rate for gill_spacing", xlab =mean(AccList),
border="blue", col=rainbow(3))
plot(RecList,type = "o", ylab="Recall Rate for gill_spacing", xlab =mean(RecList),
border="blue", col=rainbow(3))
plot(FscList,type = "o", ylab="F-Score Rate for gill_spacing", xlab =mean(FscList),
border="blue", col=rainbow(3))
plot(PrcList,type = "o", ylab="Precision Rate for gill_spacing", xlab =mean(PrcList),
border="blue", col=rainbow(3))
#-------------------------cross class
TRAIN_SIZE = 0.8
NUM_OF_FOLD = 10
TRAIN_DATA = list()
#begin the logistic function (Model fitting)
set.seed(48425) #Set the seed for reproducibility
AccList <- NULL
RecList <- NULL
FscList <- NULL
PrcList <- NULL
SnvList <- NULL
SpcList <- NULL
RnnTime <- NULL
for(i in 1:(NUM_OF_FOLD)){
#Segement your data by fold using the which() function
testIndexes <- which(folds==i,arr.ind=TRUE)
test <- mushroom[testIndexes, ]
train <- mushroom[-testIndexes, ]
start_time <- Sys.time()
model = glm(formula = class ~ ., data = train, family = binomial())
#summary(model)
#NA değerler var, bunun sebebi birden fazla attribute un iyi bir şekilde eşleşmesi
#bu durumu istemiyoruz o yüzden NA'lı attributeları kaldırıyoruz
drops <- c("stalk_color_above_ring","stalk_color_below_ring","veil_color","ring_number"
,"ring_type","spore_print_color","habitat") #we remove this column in our dataset.
train <- train[ , !(names(train) %in% drops)] #remove
test <- test[ , !(names(test) %in% drops)] #remove
#Warning: glm.fit: algorithm did not converge hatası iterasyon sayısı ile ilgili
#default olarak maxit=25'dir biz 100 yapıyoruz
model <- glm(class ~ ., data = train,family = binomial ,maxit = 100)
#summary(model)
#şimdi modeli test datamız üzerinde test ediyoruz
predicted <- predict.glm(model,newdata = test[,-1],type = "response")
#tahmin edilen değerlerin 0.5 üzeri olanlar poison
predicted <- predicted >= mean(predicted)
predicted <- gsub("TRUE","poisonous",predicted)
predicted <- gsub("FALSE","edible",predicted)
actual <- test[,1]
train_err <- mean(predicted != train$class)
confusion_matrix <- table(predicted,actual)
TP <- confusion_matrix[2,2]
TN <- confusion_matrix[1,1]
FP <- confusion_matrix[2,1]
FN <- confusion_matrix[1,2]
Accuracy <- (TP+TN)*100/(TP+TN+FP+FN)
Recall <- TP*100/(TP+FN)
F_Score <- 2*TP*100/(2*TP+FP+FN)
Precision <- TP*100/(TP+FP)
end_time <- Sys.time()
AccList <- c(AccList, Accuracy)
RecList <- c(RecList, Recall)
FscList <- c(FscList, F_Score)
PrcList <- c(PrcList, Precision)
SnvList <- c(SnvList,Sens)
SpcList <- c(SpcList,Spec)
RnnTime <- c(RnnTime,(end_time-start_time))
print(paste(i, "-> running time => ", (end_time-start_time), "Accuracy : ", Accuracy,
" Recall : ", Recall
," F-Score : ", F_Score
," Precision : ", Precision
,"Sensivity : ",Sens
,"Specificity : ",Spec))
}
#Draw data
library(ggplot2)
library(gridExtra)
plot(RnnTime,type = "o", ylab="Running time for class", xlab =mean(RnnTime),
border="blue", col=rainbow(3))
plot(AccList,type = "o", ylab="Accuracy Rate for class", xlab =mean(AccList),
border="blue", col=rainbow(3))
plot(RecList,type = "o", ylab="Recall Rate for class", xlab =mean(RecList),
border="blue", col=rainbow(3))
plot(FscList,type = "o", ylab="F-Score Rate for class", xlab =mean(FscList),
border="blue", col=rainbow(3))
plot(PrcList,type = "o", ylab="Precision Rate for class", xlab =mean(PrcList),
border="blue", col=rainbow(3))
#ROC Curve
#install.packages("ROCR")
#library(ROCR)
#p <- predict(model, newdata=subset(test), type="response")
#pr <- prediction(p, test$class)
#prf <- performance(pr, measure = "tpr", x.measure = "fpr")
#plot(prf)
#auc <- performance(pr, measure = "auc")
#auc <- [email protected][[1]]
#auc
#https://www.r-bloggers.com/how-to-perform-a-logistic-regression-in-r/