-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistical Machine Learning_SML2.r
356 lines (263 loc) · 11 KB
/
Statistical Machine Learning_SML2.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
############ Exercise 3 #############
mnist_train <- read_csv("https://pjreddie.com/media/files/mnist_train.csv", col_names = FALSE)
mnist_test <- read_csv("https://pjreddie.com/media/files/mnist_test.csv", col_names = FALSE)
library(readr)
library(dplyr)
library(psych)
library(dplyr)
library(knitr)
library(ggplot2)
library(xtable)
library(scales)
library(corrplot)
library(ggpubr)
library(class)
library(MASS)
library(kernlab)
library(mlbench)
library(reshape2)
library(ROCR)
library(ggplot2)
library(ggfortify)
################### Question 1 ###############
mnist_train <- mnist_train
mnist_test <- mnist_test
#m.tr = as.data.frame(filter(mnist_train, X1 == c(1,7)))
#m.te =as.data.frame(filter(mnist_test, X1 == c(1,7)))
getwd()
#### selection
m.tr = mnist_train%>%filter(X1==1 | X1==7)
m.tr = mnist_train[mnist_train$X1==1 | mnist_train$X1==7,]
dim(m.tr)
dim(m.te)
n <- nrow(m.tr) + nrow(m.te)
p <- ncol(m.tr)
pos <- 1
# Split the data
x_train = m.tr[,2:p]
y_train = m.tr[,1, drop=TRUE]
x_test = m.te[,2:p]
y_test = m.te[,1,drop=TRUE]
dim(y_train)
dim(x_train)
#### Test confusion matrix
y.te.hat9 <- knn(x_train,x_test,y_train, k=9)
y.te.hat7 <- knn(x_train,x_test,y_train, k=7) # Predicted responses in test set
y.te.hat1 <- knn(x_train,x_test,y_train, k=1) # Predicted responses in test set
conf.mat.te7 <- table(y_test, y.te.hat7)
conf.mat.te1 <- table(y_test, y.te.hat1)
conf.mat.te9 <- table(y_test, y.te.hat9)
conf.mat.te7
conf.mat.te1
conf.mat.te9
libray(xtable)
xtable(conf.mat.te9)
#### Train confusion matrix
y.tr.hat9 <- knn(x_train,x_train,y_train, k=9)
y.tr.hat7 <- knn(x_train,x_train,y_train, k=7) # Predicted responses in test set
y.tr.hat1 <- knn(x_train,x_train,y_train, k=1) # Predicted responses in test set
conf.mat.tr7 <- table(y_train, y.tr.hat7)
conf.mat.tr1 <- table(y_train, y.tr.hat1)
conf.mat.tr9 <- table(y_train, y.tr.hat9)
conf.mat.tr7
conf.mat.tr1
conf.mat.tr9
xtable(conf.mat.tr1)
########################## Question 2 ###################################
library(ROCR) #### ROC for Training
y.roc <- ifelse(y=='pos',1,0)
kNN.mod <- class::knn(x_train, x_train, y_train, k=1, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.1NN <- prediction(prob, y_train)
perf.1NN <- performance(pred.1NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, y_train, k=7, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.7NN <- prediction(prob, y_train)
perf.7NN <- performance(pred.7NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, y_train, k=5, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.9NN <- prediction(prob, y_train)
perf.9NN <- performance(pred.5NN, measure='tpr', x.measure='fpr')
plot(perf.1NN, col=2, lwd= 2, lty=2, main=paste('Comparative ROC curves in Training'))
plot(perf.7NN, col=3, lwd= 2, lty=3, add=TRUE)
plot(perf.9NN, col=4, lwd= 2, lty=4, add=TRUE)
abline(a=0,b=1)
legend('bottomright', inset=0.05, c('1NN','7NN','9NN'), col=2:4, lty=2:4)
########### ROC For Testing
y.roc <- ifelse(y=='pos',1,0)
kNN.mod <- class::knn(x_train, x_test, y_train, k=1, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.1NN <- prediction(prob, x_test)
perf.1NN <- performance(pred.1NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_test, y_train, k=7, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.7NN <- prediction(prob, x_test)
perf.7NN <- performance(pred.7NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_test, y_train, k=5, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.9NN <- prediction(prob, x_test)
perf.9NN <- performance(pred.5NN, measure='tpr', x.measure='fpr')
plot(perf.1NN, col=2, lwd= 2, lty=2, main=paste('Comparison of Predictive ROC curves'))
plot(perf.7NN, col=3, lwd= 2, lty=3, add=TRUE)
plot(perf.9NN, col=4, lwd= 2, lty=4, add=TRUE)
abline(a=0,b=1)
legend('bottomright', inset=0.05, c('1NN','7NN','9NN'), col=2:4, lty=2:4)
################# Question 3 ##########################
#In the Report
################## Question 4 ###########################
# In the report
################ Question 5 ######################
f_b = rbind(m.te,m.tr)
pca = prcomp(f_b)
summary(pca)
autoplot(pca)
autoplot(pca,data = f_b) + theme_minimal()
############### Question 6 ####################
pca1 = pca$x[,1:2]
dff_tr_pca = head(dff_pca, n_tr)
dff_te_pca = tail(dff_pca, n_te)
ypca = knn(dff_tr_pca, dff_te_pca, y_tr, k =7, prob = TRUE)
prob <- attr(ypca, 'prob')
prob <- 2*ifelse(ypca == "1", 1-prob, prob) - 1
pred.pca <- prediction(prob, y_te)
perf.pca <- performance(pred.pca, measure='tpr', x.measure='fpr')
plot(perf.pca, col=2, lwd= 2, lty=2, main=paste('Comparison of ROC curves Between PCA and KNN at K=7'))
plot(perf.7NN, col=3, lwd= 2, lty=3, add=TRUE)
abline(a=0,b=1)
legend('bottomright', inset=0.05, c('PCA','7NN'), col=2:3, lty=2:3)
#### Exercise (Bonus): (20 points)
# Question 1
prostate <- read.csv(file.choose())
View(prostate)
n <- nrow(prostate) # Sample size
p <- ncol(prostate)-1 # Dimensionality of the input space
pos <- p+1 # Position of the response
x <- prostate[,-pos] # Data matrix: n x p matrix
y <- prostate[, pos] # Response vector
n; p;pos
dim(prostate)
# Split the data
nte <- prostate # Number of observations in the test set
ntr <- prostate # Number of observations in the train set
x_train = ntr[,2:p]
y_train = ntr[,1, drop=TRUE]
x_test = nte[,2:p]
y_test = nte[,1,drop=TRUE]
## Predictive performance of the 1,3,5,7-Nearest Neighbors Classifier
y.te.hat1 <- knn(x_train, x_test, y_train, k=1)
y.te.hat3 <- knn(x_train, x_test, y_train, k=3)
y.te.hat5 <- knn(x_train, x_test, y_train, k=5)
y.te.hat7 <- knn(x_train, x_test, y_train, k=7)
### Confusion matrix
conf.mat.te1 <- table(y_test, y.te.hat1)
conf.mat.te3 <- table(y_test, y.te.hat3)
conf.mat.te5 <- table(y_test, y.te.hat5)
conf.mat.te7 <- table(y_test, y.te.hat7)
xtable(conf.mat.te1)
xtable(conf.mat.te3)
xtable(conf.mat.te5)
xtable(conf.mat.te7)
########### ROC #####
##Comparative ROC Curves on the training set for k = 1,3,5,7
library(ROCR)
y.roc <- ifelse(y=='pos',1,0)
kNN.mod <- class::knn(x_train, x_train, y_train, k=1, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.1NN <- prediction(prob, y_train)
perf.1NN <- performance(pred.1NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, y_train, k=3, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.3NN <- prediction(prob, y_train)
perf.3NN <- performance(pred.3NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, y_train, k=5, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.5NN <- prediction(prob, y_train)
perf.5NN <- performance(pred.5NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, y_train, k=7, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.7NN <- prediction(prob, y_train)
perf.7NN <- performance(pred.7NN, measure='tpr', x.measure='fpr')
plot(perf.1NN, col=2, lwd= 2, lty=2, main=paste('Comparative ROC curves in Training'))
plot(perf.3NN, col=3, lwd= 2, lty=3, add=TRUE)
plot(perf.5NN, col=4, lwd= 2, lty=4, add=TRUE)
plot(perf.7NN, col=5, lwd= 2, lty=5, add=TRUE)
abline(a=0,b=1)
legend('bottomright', inset=0.05, c('1NN','3NN', '5NN','7NN'), col=2:5, lty=2:5)
#######Comparative ROC Curves on the test set for k = 1,3,5,7###############
library(ROCR)
y.roc <- ifelse(y=='pos',1,0)
kNN.mod <- class::knn(x_train, x_test, y_train, k=1, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.1NN <- prediction(prob, x_test)
perf.1NN <- performance(pred.1NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_test, y_train, k=3, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.3NN <- prediction(prob, x_test)
perf.3NN <- performance(pred.3NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_train, x_test, k=5, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.5NN <- prediction(prob, x_test)
perf.5NN <- performance(pred.5NN, measure='tpr', x.measure='fpr')
kNN.mod <- class::knn(x_train, x_test, y_train, k=7, prob=TRUE)
prob <- attr(kNN.mod, 'prob')
prob <- 2*ifelse(kNN.mod == "0", 1-prob, prob) - 1
pred.7NN <- prediction(prob, x_test)
perf.7NN <- performance(pred.7NN, measure='tpr', x.measure='fpr')
plot(perf.1NN, col=2, lwd= 2, lty=2, main=paste('Comparison of Predictive ROC curves'))
plot(perf.3NN, col=3, lwd= 2, lty=3, add=TRUE)
plot(perf.5NN, col=4, lwd= 2, lty=4, add=TRUE)
plot(perf.7NN, col=5, lwd= 2, lty=5, add=TRUE)
abline(a=0,b=1)
legend('bottomright', inset=0.05, c('1NN','3NN', '5NN','7NN'), col=2:5, lty=2:5)
##################### Question 2 (Comment on question 1)
#In the report
################### Question 3 Predictive performance using stochastic hold out
set.seed (19671210) # Set seed for random number generation to be reproducible
n = nrow(prostate)
epsilon <- 1/3 # Proportion of observations in the test set
nte <- round(n*epsilon) # Number of observations in the test set
ntr <- n - nte
n
R <- 100 # Number of replications
test.err <- matrix(0, nrow=R, ncol=4)
for(r in 1:R)
{
# Split the data
id.tr <- sample(sample(sample(n)))[1:ntr] # For a sample of ntr indices from {1,2,..,n}
id.te <- setdiff(1:n, id.tr)
y.te <- y[id.te] # True responses in test set
# First machine: 1NN
y.te.hat <- knn(x[id.tr,], x[id.te,], y[id.tr], k=1) # Predicted responses in test set
ind.err.te <- ifelse(y.te!=y.te.hat,1,0) # Random variable tracking error. Indicator
test.err[r,1] <- mean(ind.err.te)
# Second machine: 3NN
y.te.hat <- knn(x[id.tr,], x[id.te,], y[id.tr], k=3) # Predicted responses in test set
ind.err.te <- ifelse(y.te!=y.te.hat,1,0)
test.err[r,2] <- mean(ind.err.te)
# Third machine: 5NN
y.te.hat <- knn(x[id.tr,], x[id.te,], y[id.tr], k=5) # Predicted responses in test set
ind.err.te <- ifelse(y.te!=y.te.hat,1,0) # Random variable tracking error. Indicator
test.err[r,3] <- mean(ind.err.te)
# Fourth machine: 7NN
y.te.hat <- knn(x[id.tr,], x[id.te,], y[id.tr], k=7) # Predicted responses in test set
ind.err.te <- ifelse(y.te!=y.te.hat,1,0) # Random variable tracking error. Indicator
test.err[r,4] <- mean(ind.err.te)
}
test <- data.frame(test.err)
Method<-c('1NN', '3NN', '5NN', '7NN')
colnames(test) <- Method
boxplot(test)
###########################################################