SVM
SVM
# Performing binary classification with the first 100 samples of iris dataset
ran <- sample(nrow(iris), 0.8 * nrow(iris)) # Randomly select 80% for training
train_data <- iris[ran, ] # Train data is the subset of the dataset
str(train_data) # Check the structure of the train data
# Create a binary target variable 'mpg_high' based on the median of 'mpg' column
Auto$mpg_high <- as.factor(ifelse(Auto$mpg > median(Auto$mpg), "High", "Low"))
# Train three different SVM models with varying cost and gamma parameters
# Model 1: SVM with radial kernel, low cost (0.1), and low gamma (0.01)
svm_model1 <- svm(mpg_high ~ ., data = train_data, kernel = "radial", cost = 0.1, gamma =
0.01)
cat("Model 1 support vectors:", summary(svm_model1)$nSV, "\n") # Check number of
support vectors for Model 1
# Model 2: SVM with radial kernel, higher cost (10), and higher gamma (0.1)
svm_model2 <- svm(mpg_high ~ ., data = train_data, kernel = "radial", cost = 10, gamma =
0.1)
cat("Model 2 support vectors:", summary(svm_model2)$nSV, "\n") # Check number of
support vectors for Model 2
# Model 3: SVM with radial kernel, higher cost (10), but default gamma (1/n)
svm_model3 <- svm(mpg_high ~ ., data = train_data, kernel = "radial", cost = 10)
cat("Model 3 support vectors:", summary(svm_model3)$nSV, "\n") # Check number of
support vectors for Model 3
# Using caret and gmodels for detailed performance metrics (confusion matrix)
library(gmodels) # For CrossTable
library(caret) # For confusionMatrix
# These metrics are available directly in the confusion matrix and CrossTable output.