-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM.R
173 lines (152 loc) · 5.45 KB
/
SVM.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
library(e1071)
library(readr)
library(mosaic)
library(tidyr)
###### For Vowel dataset #####
#Set data up for model
data_setup <- function(data, test_size, target_col){
data[, -target_col] <- data[, -target_col]
data_split = {}
all_rows <- 1:nrow(data)
test_rows = complete.cases(sample(all_rows, trunc(length(all_rows) * test_size)))
data_split$Test = data[test_rows,]
data_split$Train = data[-test_rows,]
return(data_split)
}
#model for svm
svm_model <- function (vowel, k, g, cost, erroror_change) {
model <- svm(Class~., data = vowel$Train, kernel = k, gamma = g, cost = cost)
prediction <- predict(model, vowel$Test)
confusionMatrix <- table(pred = prediction, true = vowel$Test$Class)
agreement <- prediction == vowel$Test$Class
accuracy <- prop.table(table(agreement))
true_pred <- ifelse(length(table(agreement)) == 1, table(agreement)[1], table(agreement)[2])
error <- (true_pred - highest_agreemnt) / length(all_rows)
if (true_pred > highest_agreemnt || (error > erroror_change)) {
highest_agreemnt <<- true_pred
best_cost <<- cost
highest_gamma <<- g
highest_accuracy <<- accuracy
}
}
# vowel$Speaker <- as.numeric(vowel$Speaker)
# vowel$Sex <- as.numeric(vowel$Sex)
# My own way of one hot encoding
vowel <- read.csv("vowel.csv", head=TRUE, sep=",", stringsAsFactors=TRUE)
vowel <- vowel %>%
mutate(value = 1) %>%
spread(Sex, value, fill = 0 ) %>%
mutate(value = 1) %>%
spread(Speaker, value, fill = 0)
clean_data <- data_setup(vowel, .3, 11)
#way to see the highest accuracy you obtained along with the parameters
#need some sort of way to measure the changes in erroror
#need to initialize what the best is
highest_agreemnt <- 0
highest_gamma <- 0
best_cost <- 0
highest_accuracy <- 0
all_rows <- 1:nrow(vowel)
#different parameters combination set up
for (cost in seq(2^(1:10))) {
for (gamma in seq(.01, 1, by=.1)) {
svm_model(clean_data, k = "radial", g = gamma, cost = cost, erroror_change = .1)
}
}
#model for svm
svm_model <- function (vowel, k, g, cost, erroror_change) {
model <- svm(Class~., data = vowel$Train, kernel = k, gamma = g, cost = cost)
prediction <- predict(model, vowel$Test)
confusionMatrix <- table(pred = prediction, true = vowel$Test$Class)
agreement <- prediction == vowel$Test$Class
accuracy <- prop.table(table(agreement))
true_pred <- ifelse(length(table(agreement)) == 1, table(agreement)[1], table(agreement)[2])
error <- (true_pred - highest_agreemnt) / length(all_rows)
if (true_pred > highest_agreemnt || (error > erroror_change)) {
highest_agreemnt <<- true_pred
best_cost <<- cost
highest_gamma <<- g
highest_accuracy <<- accuracy
}
}
# Print our results to see
print("Results for Vowels")
print("Agreement")
print(highest_agreemnt)
print("Accuracy")
print(highest_accuracy)
print("Cost")
print(best_cost)
print("Gamma")
print(highest_gamma)
###### For letter dataset #####
###### For letter dataset #####
#Set data up for model
data_setup <- function(data, test_size, target_col){
data[, -target_col] <- data[, -target_col]
data_split = {}
all_rows <- 1:nrow(data)
test_rows = complete.cases(sample(all_rows, trunc(length(all_rows) * test_size)))
data_split$Test = data[test_rows,]
data_split$Train = data[-test_rows,]
return(data_split)
}
#model for svm
svm_model <- function (letter, k, g, cost, erroror_change) {
model <- svm(letter ~., data = letter$Train, kernel = k, gamma = g, cost = cost)
prediction <- predict(model, letter$Test)
confusionMatrix <- table(pred = prediction, true = letter$Test$letter )
agreement <- prediction == letter$Test$letter
accuracy <- prop.table(table(agreement))
true_pred <- ifelse(length(table(agreement)) == 1, table(agreement)[1], table(agreement)[2])
error <- (true_pred - highest_agreemnt) / length(all_rows)
if (true_pred > highest_agreemnt || (error > erroror_change)) {
highest_agreemnt <<- true_pred
best_cost <<- cost
highest_gamma <<- g
highest_accuracy <<- accuracy
}
}
# My own way of one hot encoding
letter <- read.csv("letters.csv", head=TRUE, sep=",", stringsAsFactors=TRUE)
clean_data <- data_setup(letter, .3, 11)
#way to see the highest accuracy you obtained along with the parameters
#need some sort of way to measure the changes in erroror
#need to initialize what the best is
highest_agreemnt <- 0
highest_gamma <- 0
best_cost <- 0
highest_accuracy <- 0
all_rows <- 1:nrow(letter)
#different parameters combination set up
for (cost in seq(2^(1:10))) {
for (gamma in seq(.01, 1, by=.1)) {
svm_model(clean_data, k = "radial", g = gamma, cost = cost, erroror_change = .1)
}
}
#model for svm
svm_model <- function (letter, k, g, cost, erroror_change) {
model <- svm(letter ~., data = letter$Train, kernel = k, gamma = g, cost = cost)
prediction <- predict(model, letter$Test)
confusionMatrix <- table(pred = prediction, true = letter$Test$letter )
agreement <- prediction == letter$Test$letter
accuracy <- prop.table(table(agreement))
true_pred <- ifelse(length(table(agreement)) == 1, table(agreement)[1], table(agreement)[2])
error <- (true_pred - highest_agreemnt) / length(all_rows)
if (true_pred > highest_agreemnt || (error > erroror_change)) {
highest_agreemnt <<- true_pred
best_cost <<- cost
highest_gamma <<- g
highest_accuracy <<- accuracy
}
}
# Print our results to see
print("Results for Letters")
print("Agreement")
print(highest_agreemnt)
print("Accuracy")
print(highest_accuracy)
print("Cost")
print(best_cost)
print("Gamma")
print(highest_gamma)