My Third project on SVM Implementation using R Studio

setwd("C:/Users/Dhikrullah/Documents/Data with R")

Data Preprocessing

animal_data <- read.csv("animal_data.csv", header = TRUE)
ishorse <- c(rep(-1,10), rep(+1, 10))

Install the following libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(e1071)
my.data <- data.frame(height = animal_data['height'],
                      weight = animal_data['weight'],
                      animal = as.factor(ishorse))

str(animal_data)
## 'data.frame':    20 obs. of  3 variables:
##  $ height: num  126 137 109 148 110 ...
##  $ weight: num  44 52.1 57.1 33 27.8 27.2 32 45.1 56.7 56.9 ...
##  $ animal: int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
# view the created data frame
my.data
##    height weight animal
## 1   126.3   44.0     -1
## 2   136.9   52.1     -1
## 3   109.2   57.1     -1
## 4   148.3   33.0     -1
## 5   110.4   27.8     -1
## 6   107.8   27.2     -1
## 7   128.4   32.0     -1
## 8   120.2   45.1     -1
## 9   140.2   56.7     -1
## 10  139.2   56.9     -1
## 11  154.1  122.1      1
## 12  170.8  123.9      1
## 13  183.1  122.9      1
## 14  164.0  101.1      1
## 15  193.6  128.9      1
## 16  181.7  137.1      1
## 17  164.8  127.0      1
## 18  174.6  103.0      1
## 19  185.8  141.6      1
## 20  176.9  102.4      1
# plot the data 
plot(my.data[,-3], col=(3)/2, pch = 19); abline(h=0, v=0, lty = 3)

Data Modelling

# perform svm by calling the svm method and passing the parameters
svm.model <- svm(animal ~., 
                 data = my.data,
                 type = 'C-classification',
                 kernel = 'linear',
                 scale = FALSE)

# display summary of the SVM value 
summary(svm.model)
## 
## Call:
## svm(formula = animal ~ ., data = my.data, type = "C-classification", 
##     kernel = "linear", scale = FALSE)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  1 
## 
## Number of Support Vectors:  2
## 
##  ( 1 1 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  -1 1
# show the support vectors
# Assuming my.data is your data frame and svm.model is your SVM model
plot(my.data[, 1], my.data[, 2], main = "SVM Plot", xlab = "X-axis Label", ylab = "Y-axis Label")
points(my.data[svm.model$index, c(1, 2)], col = "orange", cex = 2)

# get parameters of the hyperplane 
w <- t(svm.model$coefs) %*% svm.model$SV
b <- svm.model$rho
#  in this 2D case the hyperplane is the line w[1,1]*x1 + w[1,2]*2 +b = 0
# Example data
plot(my.data[, 1], my.data[, 2], main = "SVM Decision Boundary", xlab = "X-axis Label", ylab = "Y-axis Label")

# Assuming w is your weight vector and b is the bias term from your SVM model
abline(a = -b / w[1, 2], b = -w[1, 1] / w[1, 2], col = "blue", lty = 3)

observations <- data.frame(height=c(67,121,100), weight=c(121, 190, 100))
plot(my.data[,-3], col=(ishorse+3)/2, pch = 19, xlim = c(0,250), ylim = c(0,250))
abline(h =0, v=0, lty=3)
points(observations[1,], col ="green", pch = 19)
points(observations[2,], col ="blue", pch = 19)
points(observations[3,], col ="darkorange", pch = 19)

abline(a=-b/w[1,2], b=-w[1,1]/w[1,2], col = "blue", lty = 3)

Data Verfication

predict(svm.model, observations)
##  1  2  3 
## -1  1 -1 
## Levels: -1 1