Support Vector Machines in R
Kailash Awati
Instructor
library(e1071)
svm_model <- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
scale = FALSE)
# Print model summary
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
library(e1071)
svm_model <- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
cost = 100,
scale = FALSE)
# Print model summary
svm_model
Call:
svm(formula = y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
cost = 100,
scale = FALSE)
Parameters:
SVM-Type: C-classification
SVM-Kernel: linear
cost: 100
gamma: 0.5
Number of Support Vectors: 6
# Build model
library(e1071)
svm_model<- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
cost = 100,
scale = FALSE)
# Train and test accuracy
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$y)
0.8208333
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.85
# Trainset contains 80% of data
# Same train/test split as before.
# Build model
svm_model <- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
cost = 1,
scale = FALSE)
# Test accuracy
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$y)
0.8666667
Support Vector Machines in R