-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStats202CodeNotes.R
306 lines (271 loc) · 9.58 KB
/
Stats202CodeNotes.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
# STATS202 project - Carl Johan Rising - August 2014
#Data attributes:
# (1) query_id (2) url_id (3) query_length (4) is_homepage
# (5-12) sig1-8 (13) relevance [1/0]
# Training : 65535 rows Header=T - query_id 4631- data[,1:13]
# Test : ... rows Header=T data[,1:12]
#On start
FullTrain <- read.csv("training.csv", header=TRUE)
class(FullTrain) #Should return: [1] "data.frame"
indexes = sample(1:nrow(FullTrain), size=0.2*nrow(FullTrain))
maintest = FullTrain[indexes,]
maintrain = FullTrain[-indexes,]
trans_Train<-maintrain
trans_Train[,7:10][trans_Train[,7:10]==0] <- 0.001
trans_Train[,7:10] <- log(trans_Train[,7:10]) #transforms column 7:10 to log value
trans_Test <- maintest
trans_Test[,7:10][trans_Test[,7:10]==0] <- 0.001
trans_Test[,7:10] <- log(trans_Test[,7:10]) #transforms column 7:10 to log value
library("rpart")
library("class")
library("e1071")
install.packages("randomForest")
library("randomForest")
summary(trans_Test)
summary(trans_Train[,7:10])
summary(trans_Test[,7:10])
cor(trans_Test[,5:12])
# Load of data-sets
FullTrain <- read.csv("training.csv", header=TRUE)
FullTest <- read.csv("test.csv", header=TRUE)
testtest <- read.csv("untitled.csv",header=FALSE)
is.na(FullTrain) # Tests for missing values
is.na(FullTest) # Tests for missing values
is.na(testtest)
#Libraries
library("")
library("rpart")
library("class")
library("e1071")
install.packages("randomForest")
library("randomForest")
install.packages("corrgram")
library("corrgram")
# Samples
# Correlations
corrdata <- Sample1
corrgram(maintrain, main="Correlations for Training Data")
corrgram(maintest, main="Correlations for Test Data")
cor(FullTrain[,5:12])
cor(maintest[,5:12])
cor(maintrain[,5:12])
cor(trans_Train[,5:12])
is.factor(trans_Train[,10])
# Modify data
# Split data
class(FullTrain) #Should return: [1] "data.frame"
indexes = sample(1:nrow(FullTrain), size=0.2*nrow(FullTrain))
maintest = FullTrain[indexes,]
maintrain = FullTrain[-indexes,]
#Count rows
nrow(FullTrain) #80046 - 100%
nrow(maintrain) #64037 - 80%
nrow(maintest) #16009 - 20%
nrow(trans_Test)
nrow(trans_Train)
# Transform
trans_Train[,7:10][trans_Train[,7:10]==0] <- 0.001
trans_Train[,7:10] <- log(trans_Train[,7:10]) #transforms column 7:10 to log value
trans_Test[,7:10][trans_Test[,7:10]==0] <- 0.001
trans_Test[,7:10] <- log(maintest[,7:10]) #transforms column 7:10 to log value
# Plot data - sig1-8
hist(maintrain[,5],main="Histogram: sig1",xlab="Value",col="lightgreen") #sig1
hist(maintrain[,6],main="Histogram: sig2",xlab="Value",col="lightgreen") #sig2
hist(maintrain[,7],main="Histogram: sig3",xlab="Value",col="magenta") #sig3
hist(log(maintrain[,7]),main="Histogram: log(sig3)",xlab="Value",col="lightblue") #log(sig3)
hist(maintrain[,8],main="Histogram: sig4",xlab="Value",col="magenta") #sig4
hist(log(maintrain[,8]),main="Histogram: log(sig4)",xlab="Value",col="lightblue") #log(sig4)
hist(maintrain[,9],main="Histogram: sig5",xlab="Value",col="magenta") #sig5
hist(log(maintrain[,9]),main="Histogram: log(sig5)",xlab="Value",col="lightblue") #log(sig5)
hist(maintrain[,10],main="Histogram: sig6",xlab="Value",col="magenta") #sig6
hist(log(maintrain[,10]),main="Histogram: log(sig6)",xlab="Value",col="lightblue") #log(sig6)
hist(maintrain[,11],main="Histogram: sig7",xlab="Value",col="lightgreen") #sig7
hist(maintrain[,12],main="Histogram: sig8",xlab="Value",col="lightgreen") #sig8
hist(trans_Train[,])
##############################################
##############################################
# CLASSIFIERS
# Decision tree - rpart
# depth=5 test_error: 0.36123
# depth=7 test_error: 0.347
y<-as.factor(trans_Train[,13])
x<-trans_Train[,3:12]
y_test<-as.factor(trans_Test[,13])
x_test<-trans_Test[,3:12]
i<-1
dep<-i
for (i in 1:10) {
fit<-rpart(y~.,x,control=rpart.control(minsplit=0,minbucket=0,cp=-1,maxcompete=0,maxsurrogate=0, usesurrogate=0,xval=0,maxdepth=i))
train_error[i]<-sum(y!=predict(fit,x,type="class"))/length(y)
test_error[i]<-sum(y_test!=predict(fit,x_test,type="class"))/length(y_test)
dep[i]<-i
i<-i+1
}
plot(dep,train_error,type="l",pch=19, ylim=c(0,1), col="blue", xlab="Depth of tree", ylab="Error rate", main="Carl Johan Rising")
par(new=T)
plot(dep,test_error,type="l",pch=19, ylim=c(0,1), col="black", xlab="Depth of tree", ylab="Error rate", main="Carl Johan Rising")
legend(7,.8,c("Train error","Test error"),col=c("blue","black"),pch=19)
min(test_error)
test_error
# SVM - Support Vector Machine
# Result: trans_Test: .3354/.3309 [,3:12]
# for maintrain and maintest: test errorrate: .365, cost=1
#train<-read.csv("training.csv",header=TRUE)
# second test: .364 [1:1000,3:12] -> next .3309 / .3354
# [1] 0.3356862 cost = 8 #final
i<-1
cost<-i
for(i in 1:10){
y<-as.factor(trans_Train[,13])
x<-trans_Train[,3:12]
y_test<-as.factor(trans_Test[,13])
x_test<-trans_Test[,3:12]
fit<-svm(x,y,cost=i)
train_error[i]<-1-sum(y==predict(fit,x))/length(y)
test_error[i]<-1-sum(y_test==predict(fit,x_test))/length(y_test)
cost[i]<-i
i<-i+1
}
plot(cost,test_error, type="l")
legend(7,.338,c("Test error"), col="black",pch=19)
test_error
# KNN - K Nearest Neighbour
# Results: trans_Test: 0.4346305 for k=5
# for maintest: .448
y<-as.factor(maintrain[,13])
x<-maintrain[,3:12]
y_test<-as.factor(maintest[,13])
x_test<-maintest[,3:12]
i<-1
fit_test<-knn(x,x_test,y,k=i)
fit<-knn(x,x,y,k=i)
train_error<-
1-sum(y==fit)/length(y)
test_error<-
1-sum(y_test==fit_test)/length(y_test)
i<-i+1
summary(x,x_test,y)
# 0.3862827 - k=9
for (i in 1:10){
y<-as.factor(trans_Train[,13])
x<-trans_Train[,3:12]
y_test<-as.factor(trans_Test[,13])
x_test<-trans_Test[,3:12]
fit_test<-knn(x,x_test,y,k=i)
fit<-knn(x,x,y,k=i)
train_error[i] <- 1-sum(y==fit)/length(y)
test_error[i] <- 1-sum(y_test==fit_test)/length(y_test)
i<-i+1
}
print(test_error)
plot(train_error,type="l",pch=19, col="blue",main="Carl Johan Rising",ylim=c(0,1),ylab="Error rate",xlab="k value")
par(new=T)
plot(test_error,type="l",pch=19, col="black",main="Carl Johan Rising",ylim=c(0,1),ylab="Error rate",xlab="k value")
legend(8,.8,c("Test error"),col="black",lty=1,pch=19)
min(test_error)
test_error
# NaiveBayes
# test_error .380, laplace 0
?naiveBayes()
y<-as.factor(trans_Train[,13])
x<-trans_Train[,3:12]
y_test<-as.factor(trans_Test[,13])
x_test<-trans_Test[,3:12]
fit<-naiveBayes(as.factor(y)~.,data=x, laplace = 0)
sum(predict(fit,x_test)!=y_test)/length(y_test)
# Ensemble methods:
# RandomForest test_error: .343 [,3:12]
y<-as.factor(trans_Train[,13])
x<-trans_Train[,3:12]
y_test<-as.factor(trans_Test[,13])
x_test<-trans_Test[,3:12]
fit<-randomForest(x,y)
1-sum(y_test==predict(fit,x_test))/length(y_test)
test_error
# AdaBoost
?rep
y<-is.numeric(trans_Train[1:1000,13]*2-1) #y values must be -1 and 1
x<-trans_Train[1:1000,3:12]
y_test<-is.numeric(trans_Test[1:1000,13]*2-1) #y values must be -1 and 1
x_test<-trans_Test[1:1000,3:12]
train_error<-rep(0,500) # Keep track of errors
test_error<-rep(0,500)
f<-rep(0,130) # 130 pts in training data
f_test<-rep(0,78) # 78 pts in test data
i<-1
library(rpart)
while(i<=500){
w<-exp(-y*f) # This is a shortcut to compute w + w<-w/sum(w)
fit<-rpart(y~.,x,w,method="class")
g<-1+2*(predict(fit,x)[,2]>.5) # make -1 or 1
g_test<-1+2*(predict(fit,x_test)[,2]>.5)
e<-sum(w*(y*g<0))
alpha<-.5*log ( (1-e) / e ) # or .5/.1
f<-f+alpha*g
f_test<-f_test+alpha*g_test
train_error[i]<-sum(1*f*y<0)/130
test_error[i]<-sum(1*f_test*y_test<0)/78
i<-i+1
}
while(i<=500){
w = exp(-y*f)
fit = rpart(y~.,x,w,method="class")
g = -1+2*(predict(fit,x)[,2]>.5)
g_test = -1+2*(predict(fit,x_test)[,2]>.5)
e = sum(w*(y*g < 0))
alpha = .5*log ( (1-e) / e )
f = f + alpha * g
f_test = f_test + alpha * g_test
train_error[i] = sum(1*f*y<0)/130
test_error[i] = sum(1*f_test*y_test<0)/78
i = i+1
}
plot(seq(1,500),test_error,type="l",ylim=c(0,.5),ylab="Error Rate",xlab="Iterations",lwd=2,main="Carl Johan Rising")
lines(train_error,lwd=2,col="purple")
legend(4,.5,c("Training Error","Test Error"), col=c("purple","black"),lwd=2)
=IF(D17>MIN($D$7:D16);IF(C17>=MAX($C$7:C16);INDEX(F$7:F17;MATCH(MIN($D$7:D16);$D$7:D17;0))+(C17-INDEX($C$7:C17;MATCH(MIN($D$7:D16);$D$7:D17;0)))/D17;0);SUM($E$7:E17)+C17/D17)
#########
train<-read.csv("sonar_train.csv",header=FALSE)
test<-read.csv("sonar_test.csv",header=FALSE)
y<-is.numeric(trans_Train[,13]*2-1) #y values must be -1 and 1
x<-trans_Train[,3:12]
y_test<-trans_Test[,13]
x_test<-trans_Test[,3:12]
train_error<-rep(0,500) # Keep track of errors
test_error<-rep(0,500)
f<-rep(0,130) # 130 pts in training data
f_test<-rep(0,78) # 78 pts in test data
i<-1
y_sonar_train<-train[,61]
x_sonar_train<-train[,1:60]
y_sonar_test<-test[,61]
x_sonar_test<-test[,1:60]
i<-1
library(rpart)
train_error_Boost<-rep(0,500) # Keep track of errors
test_error<-rep(0,500)
f_train<-rep(0,130) # 130 pts in training data
f_test<-rep(0,78) # 78 pts in test data
w_train<-rep(0,500)
w_test<-rep(0,500)
while(i<=500){
w_train = exp(-y_sonar_train*f_train)
train_exp_loss[i] = log(sum(w_train))
w_test = exp(-y_sonar_test*f_test)
test_exp_loss[i] = log(sum(w_test))
w = w_train/sum(w_train)
fit_Boost = rpart(y_sonar_train~.,x_sonar_train,w,method="class")
g = -1+2*(predict(fit_Boost,x_sonar_train)[,2]>.5)
g_test = -1+2*(predict(fit_Boost,x_sonar_test)[,2]>.5)
e = sum(w*(y_sonar_train*g < 0))
alpha = .5*log ( (1-e) / e )
f_train = f_train + alpha * g
f_test = f_test + alpha * g_test
train_error_Boost[i] = sum(1*f_train*y_sonar_train<0)/130
test_error_Boost[i] = sum(1*f_test*y_sonar_test<0)/78
i = i+1
}
plot(seq(1,500),test_error_Boost,type="l",ylim=c(0,.2),ylab="Error Rate",xlab="Iterations",main="Carl Johan Rising",lwd=2)
lines(train_error_Boost,lwd=2,col="purple")
legend(350,.07,c("Test Error","Training Error"),col=c("purple","black"),lwd=2)
#Write table write.table