-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM.r
42 lines (22 loc) · 1.07 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
library(tidyverse)
library(caTools)
library(e1071)
# Data --------------------------------------------------------------------
vers.virg <- filter(iris, Species != "setosa")
# Separating into training and test sets ----------------------------------
svm_sample <- sample.split(vers.virg$Species, SplitRatio = .75)
training.set <- subset(vers.virg, svm_sample == TRUE)
test.set <- subset(vers.virg, svm_sample == FALSE)
# Training the SVM --------------------------------------------------------
?e1071::svm
svm_model <- svm(Species ~ Petal.Width + Petal.Length, data=training.set,
method = "C-classification", kernel = "linear")
svm_model
# Plotting the SVM --------------------------------------------------------
plot(svm_model, data = training.set[, c(3,4,5)],
formula = Petal.Width ~ Petal.Length)
# Testing the SVM ---------------------------------------------------------
pred.training <- predict(svm_model, training.set)
mean(pred.training == training.set$Species)
pred.test <- predict(svm_model, test.set)
mean(pred.test == test.set$Species)