-
Notifications
You must be signed in to change notification settings - Fork 1
/
DigitRecognition.R
44 lines (28 loc) · 993 Bytes
/
DigitRecognition.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
#MNIST Digit recognition using SVM & PCA
#Author Raghav Nayak M
library(e1071)
#Read training and test dataset
train <- read.csv("train.csv", header=TRUE)
test <- read.csv("test.csv", header=TRUE)
labels <- train[,1]
train <- train[,-1]
#Start applying PCA for dimentionality reduction
nw <- cov(train);
nd <- svd(nw);
u <- nd$u[,1:120];
train_names <- colnames(train);
train_names <- train_names[1:120];
train <- as.matrix(train);
train <- train %*% u
colnames(train) <- train_names;
dataset =data.frame(label=as.factor(labels), train);
#Train SVM with polynomial kernel of degree 9
model = svm(label ~., data = dataset, kernel = "polynomial", degree = 2)
test <- as.matrix(test);
test <- test %*% u;
#Column names for training dataset and testing would be same :)
colnames(test) <- train_names;
#Prepare output file
pred = predict(model, newdata = test)
predictions = data.frame(ImageId=1:nrow(test), Label=pred)
write.csv(predictions,"predictions-new.csv", row.names= FALSE)