-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogistic Regression.R
356 lines (350 loc) · 12.9 KB
/
Logistic Regression.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
356
#GTID:sjiang98
#0. Data Preprocessing
train <- read.csv('~/Desktop/CSE6242/mnist_train.csv', header = FALSE)
test <- read.csv('~/Desktop/CSE6242/mnist_test.csv', header = FALSE)
test_df <- as.data.frame(test)
train_df <- as.data.frame(train)
test_t <- t(test_df)
train_t <- t(train_df)
dim(test_t)
dim(train_t)
#subsets of data
train_0_1 <- subset(train_t, train_t[,785]==0|train_t[,785]==1)
train_3_5 <- subset(train_t, train_t[,785]==3|train_t[,785]==5)
test_0_1 <- subset(test_t, test_t[,785]==0|test_t[,785]==1)
test_3_5 <- subset(test_t, test_t[,785]==3|test_t[,785]==5)
#corresponding true labels
true_label_train_0_1 <- train_0_1[,785]
true_label_test_0_1 <- test_0_1[,785]
true_label_train_3_5 <- train_3_5[,785]
true_label_test_3_5 <- test_3_5[,785]
#two sets of data
train_0_1 <- train_0_1[,1:784] #12665,784
train_3_5 <- train_3_5[,1:784] #11552,784
test_0_1 <- test_0_1[,1:784]#2115,784
test_3_5 <- test_3_5[,1:784]#1902,784
#sample four numbers
n1 <- sample(1:12665, 1)
n2 <- sample(1:11552, 1)
n3 <- sample(1:2115, 1)
n4 <- sample(1:1902, 1)
#matrix
data1 <- as.matrix(train_0_1[n1,])
data1 <- matrix (data1,nrow=28, ncol=28)#0
data2 <- as.matrix(train_3_5[n2,])
data2 <- matrix (data2,nrow=28, ncol=28)#3
data3 <- as.matrix(test_0_1[n3,])
data3 <- matrix (data3,nrow=28, ncol=28)#1
data4 <- as.matrix(test_3_5[n4,])
data4 <- matrix (data4,nrow=28, ncol=28)#5
#images
image(data1, col=gray.colors(256))
image(data2, col=gray.colors(256))
image(data3, col=gray.colors(256))
image(data4, col=gray.colors(256))
#2. Implementation
#Sigmoid function
sigmoid <- function(z)
{
g <- 1/(1+exp(-z))
return(g)
}
#Cost Function
cost <- function(theta, X, X_label)
{
m <- nrow(X)
g <- sigmoid(X%*%theta)
J <- (1/m)*sum((-X_label*log(g)) - ((1-X_label)*log(1-g)))
return(J)
}
#Intial theta, We will set theta parameters equal to zero initially and check the cost.
initial_theta_0_1 <- rep(0,ncol(train_0_1))
initial_theta_0_1
cost(initial_theta_0_1,train_0_1,true_label_train_0_1)
#You will find cost is 0.693 with initial parameters. Now, our objective is to minimize this cost and derive the optimal value of the thetas. For that we will use gradient descent optimization.
#gradient
grad <- function(X, y, theta){
m = nrow(X)
hx = sigmoid(X %*% theta)
(1/m) * (t(X) %*% (hx - y))
}
#gradient decent
gradient_descent <- function (x, y, theta, iterations, alpha, epsilon) {
m = nrow(x)
temp = theta
for(i in 1:iterations) {
temp = temp - alpha * grad(x,y,theta)
if (abs(norm(as.matrix(temp))-norm(as.matrix(theta))) < epsilon) {
return (temp)
}
theta = temp
}
return (temp)
}
#3. Training
#train_0_1
#a predict function to calculate the predictions
initial_theta_0_1 <- rep(0,ncol(train_0_1))
X_0_1=train_0_1
y_0_1=true_label_train_0_1
initial_theta_0_1 <- gradient_descent(X_0_1, y_0_1, initial_theta_0_1, 100, 0.9, 0.001)
predicted_0_1 <- sigmoid(X_0_1 %*% initial_theta_0_1)
predicted_0_1
# an accuracy function to calculate accuracy
predicted_0_1[predicted_0_1 < 0.1] <- 0
predicted_0_1[predicted_0_1 > 0.1] <- 1
accuray <- sum(predicted_0_1 == true_label_train_0_1)/dim(train_0_1)[1]
accuray #99.25%
#train_3_5
initial_theta_3_5 <- rep(0,ncol(train_3_5))
X_3_5=train_3_5
y_3_5=true_label_train_3_5
y_3_5[y_3_5 == 3] <- 0
y_3_5[y_3_5 == 5] <- 1
initial_theta_3_5 <- gradient_descent(X_3_5, y_3_5, initial_theta_3_5, 100, 0.9, 0.001)
predicted_3_5 <- sigmoid(X_3_5 %*% initial_theta_3_5)
predicted_3_5
predicted_3_5[predicted_3_5 < 0.1] <- 0
predicted_3_5[predicted_3_5 > 0.1] <- 1
accuray_3_5 <- sum(predicted_3_5 == y_3_5)/dim(train_3_5)[1]
accuray_3_5 #91.05%
#test_3_5
initial_theta_3_5_test <- rep(0,ncol(test_3_5))
X_3_5_test=test_3_5
dim(test_3_5)
y_3_5_test=true_label_test_3_5
y_3_5_test[y_3_5_test == 3] <- 0
y_3_5_test[y_3_5_test == 5] <- 1
initial_theta_3_5_test <- gradient_descent(X_3_5_test, y_3_5_test, initial_theta_3_5_test, 100, 0.9, 0.001)
predicted_3_5_test <- sigmoid(X_3_5_test %*% initial_theta_3_5_test)
predicted_3_5_test
predicted_3_5_test[predicted_3_5_test < 0.1] <- 0
predicted_3_5_test[predicted_3_5_test > 0.1] <- 1
accuray_3_5_test<- sum(predicted_3_5_test == y_3_5_test)/dim(test_3_5)[1]
accuray_3_5_test #93.10%
#test_0_1
initial_theta_0_1_test <- rep(0,ncol(test_0_1))
X_0_1_test=test_0_1
y_0_1_test=true_label_test_0_1
initial_theta_0_1_test <- gradient_descent(X_0_1_test, y_0_1_test, initial_theta_0_1_test, 100, 0.9, 0.001)
predicted_0_1_test <- sigmoid(X_0_1_test %*% initial_theta_0_1_test)
predicted_0_1_test
predicted_0_1_test[predicted_0_1_test < 0.1] <- 0
predicted_0_1_test[predicted_0_1_test > 0.1] <- 1
accuray_0_1_test<- sum(predicted_0_1_test == y_0_1_test)/dim(test_0_1)[1]
accuray_0_1_test #99.76%
#sample 80% data for 10 times
tentimes <- list()
#subsets of data
train_0_1 <- subset(train_t, train_t[,785]==0|train_t[,785]==1)
train_3_5 <- subset(train_t, train_t[,785]==3|train_t[,785]==5)
test_0_1 <- subset(test_t, test_t[,785]==0|test_t[,785]==1)
test_3_5 <- subset(test_t, test_t[,785]==3|test_t[,785]==5)
#sample data
train_0_1 <- train_0_1[sample(nrow(train_0_1),nrow(train_0_1)*0.8),]
train_3_5 <- train_3_5[sample(nrow(train_3_5),nrow(train_3_5)*0.8),]
test_0_1 <- test_0_1[sample(nrow(test_0_1),nrow(test_0_1)*0.8),]
test_3_5 <- test_3_5[sample(nrow(test_3_5),nrow(test_3_5)*0.8),]
#corresponding true labels
true_label_train_0_1 <- train_0_1[,785]
true_label_test_0_1 <- test_0_1[,785]
true_label_train_3_5 <- train_3_5[,785]
true_label_test_3_5 <- test_3_5[,785]
#two sets of data
train_0_1 <- train_0_1[,1:784] #12665*0.8,784
train_3_5 <- train_3_5[,1:784] #11552*0.8,784
test_0_1 <- test_0_1[,1:784]#2115*0.8,784
test_3_5 <- test_3_5[,1:784]#1902*0.8,784
#train_0_1
#a predict function to calculate the predictions
initial_theta_0_1 <- rep(0,ncol(train_0_1))
X_0_1=train_0_1
y_0_1=true_label_train_0_1
initial_theta_0_1 <- gradient_descent(X_0_1, y_0_1, initial_theta_0_1, 100, 0.9, 0.001)
predicted_0_1 <- sigmoid(X_0_1 %*% initial_theta_0_1)
predicted_0_1
# an accuracy function to calculate accuracy
predicted_0_1[predicted_0_1 < 0.1] <- 0
predicted_0_1[predicted_0_1 > 0.1] <- 1
accuray_0_1 <- sum(predicted_0_1 == true_label_train_0_1)/dim(train_0_1)[1]
accuray_0_1 #99.25%
tentimes[1]=accuray_0_1
#train_3_5
initial_theta_3_5 <- rep(0,ncol(train_3_5))
X_3_5=train_3_5
y_3_5=true_label_train_3_5
y_3_5[y_3_5 == 3] <- 0
y_3_5[y_3_5 == 5] <- 1
initial_theta_3_5 <- gradient_descent(X_3_5, y_3_5, initial_theta_3_5, 100, 0.9, 0.001)
predicted_3_5 <- sigmoid(X_3_5 %*% initial_theta_3_5)
predicted_3_5
predicted_3_5[predicted_3_5 < 0.1] <- 0
predicted_3_5[predicted_3_5 > 0.1] <- 1
accuray_3_5 <- sum(predicted_3_5 == y_3_5)/dim(train_3_5)[1]
accuray_3_5 #91.05%
tentimes[2]=accuray_3_5
#test_3_5
initial_theta_3_5_test <- rep(0,ncol(test_3_5))
X_3_5_test=test_3_5
dim(test_3_5)
y_3_5_test=true_label_test_3_5
y_3_5_test[y_3_5_test == 3] <- 0
y_3_5_test[y_3_5_test == 5] <- 1
initial_theta_3_5_test <- gradient_descent(X_3_5_test, y_3_5_test, initial_theta_3_5_test, 100, 0.9, 0.001)
predicted_3_5_test <- sigmoid(X_3_5_test %*% initial_theta_3_5_test)
predicted_3_5_test
predicted_3_5_test[predicted_3_5_test < 0.1] <- 0
predicted_3_5_test[predicted_3_5_test > 0.1] <- 1
accuray_3_5_test<- sum(predicted_3_5_test == y_3_5_test)/dim(test_3_5)[1]
accuray_3_5_test #93.10%
tentimes[3]=accuray_3_5_test
#test_0_1
initial_theta_0_1_test <- rep(0,ncol(test_0_1))
X_0_1_test=test_0_1
y_0_1_test=true_label_test_0_1
initial_theta_0_1_test <- gradient_descent(X_0_1_test, y_0_1_test, initial_theta_0_1_test, 100, 0.9, 0.001)
predicted_0_1_test <- sigmoid(X_0_1_test %*% initial_theta_0_1_test)
predicted_0_1_test
predicted_0_1_test[predicted_0_1_test < 0.1] <- 0
predicted_0_1_test[predicted_0_1_test > 0.1] <- 1
accuray_0_1_test<- sum(predicted_0_1_test == y_0_1_test)/dim(test_0_1)[1]
accuray_0_1_test
tentimes[4]=accuray_0_1_test
tentimes
#4a and 4b
tentimes <- data.frame(matrix(ncol = 2, nrow = 0))
y <- c("1", "2")
colnames(tentimes) <- y
i=1
#subsets of data
train_3_5 <- subset(train_t, train_t[,785]==3|train_t[,785]==5)
test_3_5 <- subset(test_t, test_t[,785]==3|test_t[,785]==5)
#sample data
train_3_5 <- train_3_5[sample(nrow(train_3_5),nrow(train_3_5)*0.8),]
test_3_5 <- test_3_5[sample(nrow(test_3_5),nrow(test_3_5)*0.8),]
#corresponding true labels
true_label_train_3_5 <- train_3_5[,785]
true_label_test_3_5 <- test_3_5[,785]
#two sets of data
train_3_5 <- train_3_5[,1:784] #11552*0.8,784
test_3_5 <- test_3_5[,1:784]#1902*0.8,784
#train_3_5
initial_theta_3_5 <- rep(10,ncol(train_3_5))
X_3_5=train_3_5
y_3_5=true_label_train_3_5
y_3_5[y_3_5 == 3] <- 0
y_3_5[y_3_5 == 5] <- 1
initial_theta_3_5 <- gradient_descent(X_3_5, y_3_5, initial_theta_3_5, 100, 0.9, 0.001)
predicted_3_5 <- sigmoid(X_3_5 %*% initial_theta_3_5)
predicted_3_5
predicted_3_5[predicted_3_5 < 0.1] <- 0
predicted_3_5[predicted_3_5 > 0.1] <- 1
accuray_3_5 <- sum(predicted_3_5 == y_3_5)/dim(train_3_5)[1]
accuray_3_5
tentimes[i,1]=accuray_3_5
#test_3_5
initial_theta_3_5_test <- rep(10,ncol(test_3_5))
X_3_5_test=test_3_5
dim(test_3_5)
y_3_5_test=true_label_test_3_5
y_3_5_test[y_3_5_test == 3] <- 0
y_3_5_test[y_3_5_test == 5] <- 1
initial_theta_3_5_test <- gradient_descent(X_3_5_test, y_3_5_test, initial_theta_3_5_test, 100, 0.9, 0.001)
predicted_3_5_test <- sigmoid(X_3_5_test %*% initial_theta_3_5_test)
predicted_3_5_test
predicted_3_5_test[predicted_3_5_test < 0.1] <- 0
predicted_3_5_test[predicted_3_5_test > 0.1] <- 1
accuray_3_5_test<- sum(predicted_3_5_test == y_3_5_test)/dim(test_3_5)[1]
accuray_3_5_test
tentimes[i,2]=accuray_3_5_test
tentimes
write.xlsx(tentimes, "test_0.xlsx")
#5a and 5b (I used excel to store and visualize data in this case because my functions are not modular at the beginning. I would argue
#excel is a better choice in this case.)
df <- data.frame(matrix(ncol = 8, nrow = 0))
x <- c("1", "2", "3","4","5","6","7","8")
colnames(df) <- x
i=5
#subsets of data
train_0_1 <- subset(train_t, train_t[,785]==0|train_t[,785]==1)
train_3_5 <- subset(train_t, train_t[,785]==3|train_t[,785]==5)
test_0_1 <- subset(test_t, test_t[,785]==0|test_t[,785]==1)
test_3_5 <- subset(test_t, test_t[,785]==3|test_t[,785]==5)
#sample data
train_0_1 <- train_0_1[sample(nrow(train_0_1),nrow(train_0_1)*1),]
train_3_5 <- train_3_5[sample(nrow(train_3_5),nrow(train_3_5)*1),]
test_0_1 <- test_0_1[sample(nrow(test_0_1),nrow(test_0_1)*1),]
test_3_5 <- test_3_5[sample(nrow(test_3_5),nrow(test_3_5)*1),]
#corresponding true labels
true_label_train_0_1 <- train_0_1[,785]
true_label_test_0_1 <- test_0_1[,785]
true_label_train_3_5 <- train_3_5[,785]
true_label_test_3_5 <- test_3_5[,785]
#two sets of data
train_0_1 <- train_0_1[,1:784] #12665*0.8,784
train_3_5 <- train_3_5[,1:784] #11552*0.8,784
test_0_1 <- test_0_1[,1:784]#2115*0.8,784
test_3_5 <- test_3_5[,1:784]#1902*0.8,784
#train_0_1
#a predict function to calculate the predictions
initial_theta_0_1 <- rep(0,ncol(train_0_1))
X_0_1=train_0_1
y_0_1=true_label_train_0_1
initial_theta_0_1 <- gradient_descent(X_0_1, y_0_1, initial_theta_0_1, 100, 0.9, 0.001)
df[i,1] <- cost(initial_theta_0_1,X_0_1,y_0_1)
predicted_0_1 <- sigmoid(X_0_1 %*% initial_theta_0_1)
predicted_0_1
# an accuracy function to calculate accuracy
predicted_0_1[predicted_0_1 < 0.1] <- 0
predicted_0_1[predicted_0_1 > 0.1] <- 1
accuray_0_1 <- sum(predicted_0_1 == true_label_train_0_1)/dim(train_0_1)[1]
accuray_0_1
df[i,2]=accuray_0_1
#train_3_5
initial_theta_3_5 <- rep(0,ncol(train_3_5))
X_3_5=train_3_5
y_3_5=true_label_train_3_5
y_3_5[y_3_5 == 3] <- 0
y_3_5[y_3_5 == 5] <- 1
initial_theta_3_5 <- gradient_descent(X_3_5, y_3_5, initial_theta_3_5, 100, 0.9, 0.001)
df[i,3] <- cost(initial_theta_3_5,X_3_5,y_3_5)
predicted_3_5 <- sigmoid(X_3_5 %*% initial_theta_3_5)
predicted_3_5
predicted_3_5[predicted_3_5 < 0.1] <- 0
predicted_3_5[predicted_3_5 > 0.1] <- 1
accuray_3_5 <- sum(predicted_3_5 == y_3_5)/dim(train_3_5)[1]
accuray_3_5
df[i,4]=accuray_3_5
#test_3_5
initial_theta_3_5_test <- rep(0,ncol(test_3_5))
X_3_5_test=test_3_5
dim(test_3_5)
y_3_5_test=true_label_test_3_5
y_3_5_test[y_3_5_test == 3] <- 0
y_3_5_test[y_3_5_test == 5] <- 1
initial_theta_3_5_test <- gradient_descent(X_3_5_test, y_3_5_test, initial_theta_3_5_test, 100, 0.9, 0.001)
df[i,5] <- cost(initial_theta_3_5_test,X_3_5_test,y_3_5_test)
predicted_3_5_test <- sigmoid(X_3_5_test %*% initial_theta_3_5_test)
predicted_3_5_test
predicted_3_5_test[predicted_3_5_test < 0.1] <- 0
predicted_3_5_test[predicted_3_5_test > 0.1] <- 1
accuray_3_5_test<- sum(predicted_3_5_test == y_3_5_test)/dim(test_3_5)[1]
accuray_3_5_test
df[i,6]=accuray_3_5_test
#test_0_1
initial_theta_0_1_test <- rep(0,ncol(test_0_1))
X_0_1_test=test_0_1
y_0_1_test=true_label_test_0_1
initial_theta_0_1_test <- gradient_descent(X_0_1_test, y_0_1_test, initial_theta_0_1_test, 100, 0.9, 0.001)
df[i,7] <- cost(initial_theta_0_1_test,X_0_1_test,y_0_1_test)
predicted_0_1_test <- sigmoid(X_0_1_test %*% initial_theta_0_1_test)
predicted_0_1_test
predicted_0_1_test[predicted_0_1_test < 0.1] <- 0
predicted_0_1_test[predicted_0_1_test > 0.1] <- 1
accuray_0_1_test<- sum(predicted_0_1_test == y_0_1_test)/dim(test_0_1)[1]
accuray_0_1_test
df[i,8]=accuray_0_1_test
df
library(xlsx)
write.xlsx(df, "test_1.xlsx")
accuray_0_1_test #99.76%