Support Vector Machines in R
Kailash Awati
Instructor
df
.# Set seed for reproducibility
set.seed(1)
# Set the upper bound for the number of rows to be in the training set
sample_size <- floor(0.8 * nrow(df))
# Assign rows to training/test sets randomly in 80/20 proportion
train <- sample(seq_len(nrow(df)), size = sample_size)
# Separate training and test sets
trainset <- df[train, ]
testset <- df[-train, ]
e1071
library.svm()
functionlibrary(e1071)
svm_model<- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
scale = FALSE)
svm_model
gives:svm_model
Call: svm(formula = y ~ ., data = trainset, type = "C-classification", kernel = "linear", scale = FALSE) Parameters: SVM-Type: C-classification SVM-Kernel: linear cost: 1 gamma: 0.5
Number of Support Vectors: 55
# Index of support vectors in training dataset svm_model$index
# Support vectors svm_model$SV
# Negative intercept (unweighted) svm_model$rho
# Weighting coefficients for support vectors svm_model$coefs
4 8 10 11 18 37 38 39 47 59 60 74 76 77 78 80 83 ...
x1 x2 5 0.519095949 0.44232464
-0.1087075
[,1] [1,] 1.0000000
# Training accuracy
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
1
# Test accuracy
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
1
# Perfect!!
Support Vector Machines in R