-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm_hands_on_flowers.R
157 lines (138 loc) · 5.87 KB
/
svm_hands_on_flowers.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
source("josephs_functions.R")
# Library containing the SVM model
check_install_package("e1071")
check_install_package("caret")
#load data
data(iris)
# *********************************************************************
# ************************** understand data **************************
# *********************************************************************
head(iris, 5)
summary(iris)
nrow(iris)
names(iris)
levels(iris$Species)
# *********************************************************************
# ************************** check vazriables combinations *********************
# *********************************************************************
# choosing 2 variables from 4
# 4 choose 2 (n!)/(k!(n-k)!) = 6 combinations
plot(iris$Sepal.Length, iris$Sepal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
plot(iris$Sepal.Length, iris$Petal.Length, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
plot(iris$Sepal.Length, iris$Petal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
plot(iris$Sepal.Width, iris$Petal.Length, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
plot(iris$Sepal.Width, iris$Petal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
plot(iris$Petal.Length, iris$Petal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
#plot(1:150, iris$Sepal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
# *********************************************************************
#hyper params
# *********************************************************************
train_sample_percentage <- 0.75
sampling_random_seed <- 123
# *********************************************************************
#split data set in train and test data
# *********************************************************************
splitted_data <- split_train_test_data(iris, train_sample_percentage, sampling_random_seed)
train_data <- splitted_data$train_data # train data
test_data <- splitted_data$test_data # test data
# fit the model
svm_model <- svm(Species ~ Sepal.Width + Sepal.Length, kernel="linear", data=train_data)
summary(svm_model)
prediction <- predict(svm_model, test_data)
confusion_matrix <- table(prediction, test_data$Species)
obj <- confusionMatrix(confusion_matrix)
obj$overall["Accuracy"]
(accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix))
#plot(x = svm_model, data = train_data, formula = Species ~ Sepal.Width + Sepal.Length)
#, iris$Sepal.Length, iris$Sepal.Width, pch=21, bg=c("red","green3","blue")[unclass(iris$Species)])
#plot
# *********************************************************************
# create a data frame to see several models using candidate variable combinations
# *********************************************************************
result_models <- data.frame(matrix(nrow = 0, ncol = 7))
result_models <- setNames(result_models, c("formula_name","kernel_function","polynomial_degree", "gamma", "coef0", "cost", "accuracy"))
names(result_models)
# *********************************************************************
# Sepal.Width + Sepal.Length - linear
# *********************************************************************
formula <- Species ~ Sepal.Width + Sepal.Length
kernel_function <- "linear"
svm_model <- svm(formula, kernel = kernel_function, data=train_data)
prediction <- predict(svm_model, test_data)
confusion_matrix <- table(prediction, test_data$Species)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
result_models[nrow(result_models) + 1, ] =
c(
deparse(formula),
kernel_function,
"N/A",
"N/A",
"N/A",
"N/A",
accuracy)
# *********************************************************************
# Sepal.Width + Sepal.Length - polynomial
# degree = 3
# *********************************************************************
formula <- Species ~ Sepal.Width + Sepal.Length
kernel_function <- "polynomial"
degree <- 3
svm_model <- svm(formula, kernel = kernel_function, degree = degree, data=train_data)
prediction <- predict(svm_model, test_data)
confusion_matrix <- table(prediction, test_data$Species)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
result_models[nrow(result_models) + 1, ] =
c(
deparse(formula),
kernel_function,
degree,
"N/A",
"N/A",
"N/A",
accuracy)
# *********************************************************************
# Sepal.Width + Sepal.Length - polynomial
# degree = 3, gamma 2.5
# *********************************************************************
formula <- Species ~ Sepal.Width + Sepal.Length
kernel_function <- "polynomial"
degree <- 3
gamma <- 2.5
svm_model <- svm(formula, kernel = kernel_function, degree = degree, data=train_data, gamma = gamma)
prediction <- predict(svm_model, test_data)
confusion_matrix <- table(prediction, test_data$Species)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
result_models[nrow(result_models) + 1, ] =
c(
deparse(formula),
kernel_function,
degree,
gamma,
"N/A",
"N/A",
accuracy)
plot(svm_model, data = iris, Sepal.Width ~ Sepal.Length, fill = TRUE, grid = 10,
svSymbol = "🚀",
symbolPalette = c("red","green4","blue"),
color.palette = hsv_palette()
)
# *********************************************************************
# Sepal.Width + Sepal.Length - radial
# *********************************************************************
formula <- Species ~ Sepal.Width + Sepal.Length
kernel_function <- "radial"
gamma <- 100
svm_model <- svm(formula, kernel = kernel_function, data=train_data, gamma = gamma)
prediction <- predict(svm_model, test_data)
confusion_matrix <- table(prediction, test_data$Species)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
result_models[nrow(result_models) + 1, ] =
c(
deparse(formula),
kernel_function,
"N/A",
gamma,
"N/A",
"N/A",
accuracy)
result_models