-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter-recognition.R
328 lines (245 loc) · 10.6 KB
/
letter-recognition.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
# Loading libraries
library(MASS) # LDA and QDA
library(class) # KNN
library(tree) # Tree
library(rpart) # Tree
library(randomForest) # Random Forest
library(gbm) # Boosting
library(e1071) # SVM
# Load data
letter.recognition <- read.csv("letter-recognition.data")
attach(letter.recognition)
# Basic information about the data
names(letter.recognition)
dim(letter.recognition)
# Training
train <- sample(1:nrow(letter.recognition), 16000)
# Correlation
plot(X1,X3, pch = 20, col = "red", xlab = "X1", ylab = "X3")
cor(X1,X3)
###############################################################################
# Linear Discriminant Analysis #
###############################################################################
# Fit LDA model with all predictors
lda.fit <- lda(Letter~., letter.recognition[train,])
# Make prediction
lda.test.pred <- predict(lda.fit, letter.recognition[-train,])
lda.test.class <- lda.test.pred$class
# Test error
lda.test.rate <- mean(lda.test.class == Letter[-train])
# New fit with predictors interaction
lda2.fit <- lda(Letter~.*.+.:.:., letter.recognition[train,])
# Make prediction
lda2.test.pred <- predict(lda2.fit, letter.recognition[-train,])
lda2.test.class <- lda2.test.pred$class
# Test error
lda2.test.rate <- mean(lda2.test.class == Letter[-train])
###############################################################################
# Quadratic Discriminant Analysis #
###############################################################################
# Let's see with QDA
qda.fit <- qda(Letter~., letter.recognition[train,])
# Make prediction
qda.test.pred <- predict(qda.fit, letter.recognition[-train,])
qda.test.class <- qda.test.pred$class
# Compute error
qda.test.rate <- mean(qda.test.class == Letter[-train])
# Now we use combinations
qda2.fit <- qda(Letter~.*., letter.recognition[train,])
# Make again the prediction
qda2.test.pred <- predict(qda2.fit, letter.recognition[-train,])
qda2.test.class <- qda2.test.pred$class
# Compute new error
qda2.test.rate <- mean(qda2.test.class==Letter[-train])
###############################################################################
# KNN #
###############################################################################
# Set labels
label <- letter.recognition[train,1]
# Explore different K values
error.rate <- numeric(10)
for(i in 1:10){
knn.pred <- knn(letter.recognition[train,-1],
letter.recognition[-train,-1],label, k = i)
error.rate[i] <- 1-mean(knn.pred == letter.recognition[-train, 1])
}
# Plot error rates
plot(1:10, error.rate,"b", pch = 20,
col = "red", xlab = "K", ylab = "Error Rate")
# Make prediction with K = 1
knn.pred <- knn(letter.recognition[train,-1],
letter.recognition[-train,-1], label, k = 1)
# Save test rate
knn.test.rate <- mean(knn.pred == letter.recognition[-train, 1])
# Same with Cross Validation
knn.cv.pred <- knn.cv(letter.recognition[,-1], letter.recognition[,1], k = 1)
knn.cv.test.rate <- mean(knn.cv.pred == letter.recognition[, 1])
###############################################################################
# Tree #
###############################################################################
# Fit a tree
tree.fit <- tree(Letter~., letter.recognition[1:200,])
summary(tree.fit)
# Plot fitted tree
plot(tree.fit)
text(tree.fit,pretty=0)
# Make prediction
tree.pred <- predict(tree.fit, letter.recognition[-train,], type="class")
# Test rate
tree.test.rate <- mean(tree.pred == letter.recognition[-train, 1])
# Using rpart (more vervosity)
tree2.fit <- rpart(Letter~., letter.recognition, method="class")
summary(tree2.fit)
# Plot it
plot(tree2.fit)
text(tree2.fit)
# Make charts
par(mfrow=c(1,2))
rsq.rpart(tree2.fit)
par(mfrow=c(1,1))
# Make test prediction
tree2.pred <- predict(tree2.fit, letter.recognition[,], type="class")
# Compute test rate
tree2.test.rate <- mean(tree2.pred == letter.recognition[, 1])
###############################################################################
# Bagging #
###############################################################################
# Fit a bagging model
bagging.fit <- randomForest(Letter~.,letter.recognition[train,],
mtry=16,importance=TRUE)
# See fit info
bagging.fit
# Make prediction and save error rate
bagging.pred <- predict(bagging.fit,letter.recognition[-train,])
bagging.test.rate <- mean(bagging.pred == letter.recognition[-train, 1])
# It's almost OOB estimate error rate
###############################################################################
# Random Forest #
###############################################################################
# Default fit
randomForest.fit <- randomForest(Letter ~ ., data=letter.recognition[train, ])
# Review the prefious fit
randomForest.fit # 500 trees (4 lenght)
# Let's see each variable importance
importance(randomForest.fit)
# Make a prediction
randomForest.pred <- predict(randomForest.fit , letter.recognition[-train, ])
# Test rate
randomForest.test.rate <- mean(
randomForest.pred == letter.recognition[-train, 1])
# Compute number of variables to select
sqrt(dim(letter.recognition)[2]-1)
# Make the fit with differents parameters
randomForest2.fit <- randomForest(Letter ~ .,
data=letter.recognition[train, ],
ntree=100, mtry=4,
importance=TRUE)
randomForest2.pred <- predict(randomForest2.fit , letter.recognition[-train, ])
randomForest2.test.rate<- mean(
randomForest2.pred == letter.recognition[-train, 1])
# Explore with other parameters to ensure optimum on 4
randomForest3.fit <- randomForest(Letter ~ .,
data=letter.recognition[train, ],
ntree=500, mtry=5,
importance=TRUE)
randomForest3.pred <- predict(randomForest3.fit , letter.recognition[-train, ])
randomForest3.test.rate <- mean(
randomForest3.pred == letter.recognition[-train, 1])
# Let's combine previous random forestsfits
rf.1 <- randomForest(Letter ~ ., data=letter.recognition[train,])
rf.2 <- randomForest(Letter ~ ., data=letter.recognition[train,])
rf.3 <- randomForest(Letter ~ ., data=letter.recognition[train,])
rf.all <- combine(rf.1, rf.2, rf.3)
# Make prediction
randomForest.c.pred <- predict(rf.all , letter.recognition[-train,])
randomForest.c.test.rate <- mean(
randomForest.c.pred == letter.recognition[-train, 1])
# Spin the training data
train1 <- sample(1:nrow(letter.recognition[train, ]), 16000)
rf1 <- randomForest(Letter ~ .*., data=letter.recognition[train1, ])
train2 <- sample(1:nrow(letter.recognition[train, ]), 16000)
rf2 <- randomForest(Letter ~ .*., data=letter.recognition[train2, ])
train3 <- sample(1:nrow(letter.recognition[train, ]), 16000)
rf3 <- randomForest(Letter ~ .*., data=letter.recognition[train3, ])
train4 <- sample(1:nrow(letter.recognition[train, ]), 16000)
rf4 <- randomForest(Letter ~ .*., data=letter.recognition[train4, ])
train5 <- sample(1:nrow(letter.recognition[train, ]), 16000)
rf5 <- randomForest(Letter ~ .*., data=letter.recognition[train5, ])
rfall <- combine(rf1, rf2, rf3, rf4, rf5)
# Make prediction
randomForest.a.pred <- predict(rfall , letter.recognition[-train,])
randomForest.a.test.rate <- mean(
randomForest.a.pred == letter.recognition[-train, 1])
###############################################################################
# Boosting #
###############################################################################
# Fit model
boosting.fit <- gbm(Letter ~ .,
data=letter.recognition[train,],
distribution="multinomial")
# See fit summary
boosting.fit
summary(boosting.fit)
# Make prediction into test
boosting.pred <- predict(boosting.fit ,
letter.recognition[-train,],
type="response",
n.trees = 100)
# We have to transform prediction into a matrix, getting the letter
# with highest probability
pred.matrix <- matrix(boosting.pred, ncol = ncol(boosting.pred))
boosting.pred.values <- character(0)
for(i in 1:nrow(boosting.pred)){
boosting.pred.values[i] <- colnames(boosting.pred)[which.max(pred.matrix[i,])]
}
boosting.pred.values <- as.factor(boosting.pred.values)
# Assign the error rate
boosting.test.rate <- mean(boosting.pred.values == letter.recognition[-train, 1])
###############################################################################
# SVN #
###############################################################################
# Fit SVM
svm.fit <- svm(Letter~., letter.recognition[train,])
# Make prediction and save test rate
svm.pred <- predict(svm.fit, letter.recognition[-train,])
svm.test.rate <- mean(svm.pred == letter.recognition[-train,1])
# Tune SVM
tuned.fit <- tune.svm(Letter~.,
data = letter.recognition[train,],
gamma = c(0.05,0.1,0.25,0.15,0.2),
cost = 10^(2:4))
# Same as tune.out$best.model
svm.tuned.fit <- svm(Letter~., letter.recognition[train,],
gamma = 0.1,
cost = 100,
cross = 10)
svm.tuned.pred = predict(svm.tuned.fit, letter.recognition[-train,])
svm.tuned.test.rate = mean(svm.tuned.pred == letter.recognition[-train,1])
# Change kernel to see if test.rate rises
svm.tuned.fit <- svm(Letter~., letter.recognition[train,],
kernel = "polynomial",
gamma = 0.1, cost = 100,
cross = 10, epsilon = 2)
svm.tuned.pred = predict(svm.tuned.fit, letter.recognition[-train,])
svm.tuned.test.rate = mean(svm.tuned.pred == letter.recognition[-train,1])
###############################################################################
# Results #
###############################################################################
# LDA
print(lda.test.rate)
print(lda2.test.rate)
# QDA
print(qda.test.rate)
print(qda2.test.rate)
# KNN
print(knn.cv.test.rate)
# Tree(simple)
print(tree2.test.rate)
# Bagging
print(bagging.test.rate)
# Random Forests
print(randomForest.a.test.rate)
# Boosting
print(boosting.test.rate)
# SVM
print(svm.tuned.test.rate)