Learn/R Programming/Machine Learning

Support Vector Machines

Topic: SVM

Advertisement

Introduction

Support Vector Machines (SVM) find a hyperplane that best separates classes. They work well for high-dimensional data.

Building SVM

library(e1071)

# Classification
svm_model <- svm(target ~ ., data = train)

# Regression
svm_model <- svm(y ~ ., data = train)

# With kernel
svm_model <- svm(target ~ ., data = train, kernel = "radial")
svm_model <- svm(target ~ ., data = train, kernel = "polynomial")

Tuning

# Tune SVM
tuned <- tune.svm(target ~ ., data = train,
                 gamma = 10^(-3:0),
                 cost = 10^(-2:2))

# Best model
summary(tuned$best.model)

Predictions

pred <- predict(svm_model, test)

Summary

SVM works well for complex boundaries. Choose appropriate kernel for your data.

Advertisement

Advertisement

Need More Practice?

Get personalized R programming help from ChatWhole's AI-powered platform.

Get Expert Help →