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.y nel nostro caso.trainset.svm()library(e1071)
svm_model<- svm(y ~ .,
data = trainset,
type = "C-classification",
kernel = "linear",
scale = FALSE)
svm_model ottieni: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.5Number of Support Vectors: 55
# Indici dei support vector nel training set svm_model$index# Support vector svm_model$SV# Intercetta negativa (non pesata) svm_model$rho# Coefficienti di pesatura per i support vector 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
# Perfetto!!
Support Vector Machines in R