Support Vector Machine + Python & R Codes

Subscribe to get access

??Subscribe to read the rest of the comics, the fun you can’t miss ??

Support Vector Classifier (SVC) is a powerful supervised learning algorithm used for classification tasks. It works by finding the optimal hyperplane that best separates the classes in the input data. SVC is capable of handling both linear and non-linear data by using different kernel functions and is known for its ability to handle high-dimensional data efficiently, making it suitable for a wide range of applications, including image recognition, text categorization, and bioinformatics.

Python & R Codes

Now, let’s dive into an example of using SVM for Binary Classification with the Breast Cancer Dataset

Python codes

# Importing necessary libraries
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns

# Load the Breast Cancer dataset
data = datasets.load_breast_cancer()
X = data.data
y = data.target

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Initialize and fit the SVM model
model = SVC(kernel='linear', C=1.0, random_state=42)
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

R codes

Use `install.packages(“e1071”)` to install the e1071 package to use svm

# Load necessary libraries
library(caret)
library(e1071)

# Load the mtcars dataset
data("mtcars")

# Convert mpg to binary classification
median_mpg <- median(mtcars$mpg)
mtcars$mpg_class <- ifelse(mtcars$mpg > median_mpg, "High", "Low")

# Convert the target variable to a factor
mtcars$mpg_class <- as.factor(mtcars$mpg_class)

# Prepare data for modeling
x <- mtcars[, -which(names(mtcars) %in% c("mpg_class"))]  # Exclude the 'mpg_class' column
y <- mtcars$mpg_class

# Split the data into training and testing sets
set.seed(42)
trainIndex <- createDataPartition(y, p = 0.7, list = FALSE)
xTrain <- x[trainIndex, ]
yTrain <- y[trainIndex]
xTest <- x[-trainIndex, ]
yTest <- y[-trainIndex]

# Train the SVM model
model <- svm(x = xTrain, y = yTrain, kernel = "radial", cost = 1, scale = TRUE)

# Make predictions
predictions <- predict(model, xTest)


Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!