Experiment – Implement SVM Algorithm and Estimate the Accuracy of the
Model
Aim:
To implement the Support Vector Machine (SVM) classification algorithm on a given dataset
and evaluate its performance using accuracy, confusion matrix, and classification report.
Objective:
The main objective of this experiment is to:
Understand how the SVM algorithm works for classification problems.
Train an SVM model using real-world or standard datasets.
Analyze the performance metrics and interpret the model’s prediction capability.
Theory:
Support Vector Machine (SVM) is a supervised machine learning algorithm used for
classification and regression tasks.
It works by finding the optimal hyperplane that best separates data points of different
classes in the feature space.
Key concepts:
Hyperplane: A line or plane that divides the data into classes.
Support Vectors: Data points that are closest to the hyperplane and influence its
position.
Kernel Trick: Used to transform non-linear data into higher dimensions for easier
separation.
Common kernel functions:
Linear Kernel
Polynomial Kernel
Radial Basis Function (RBF) Kernel
Algorithm Steps:
1. Import the necessary Python libraries (sklearn, numpy, pandas).
2. Load the dataset (e.g., Iris dataset).
3. Separate the data into features (X) and target labels (y).
4. Split the dataset into training and testing sets.
5. Create an SVM model with an appropriate kernel (e.g., RBF).
6. Train the model using the training data.
7. Predict the output for the test set.
8. Evaluate the performance using metrics such as accuracy, classification report, and
confusion matrix.
# Import necessary libraries
import numpy as np
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import SVC
from [Link] import accuracy_score, classification_report, confusion_matrix
# Load the dataset (Iris dataset)
iris = datasets.load_iris()
# Define features (X) and target (y)
X = [Link]
y = [Link]
print("Feature matrix shape:", [Link])
print("Target vector shape:", [Link])
# Split 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
)
# Create and train the SVM model
svm_model = SVC(kernel='rbf', C=1.0, gamma='scale')
svm_model.fit(X_train, y_train)
# Predict test data
y_pred = svm_model.predict(X_test)
# Evaluate model performance
accuracy = accuracy_score(y_test, y_pred)
print("\nAccuracy of SVM Model:", round(accuracy * 100, 2), "%")
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
Feature matrix shape: (150, 4)
Target vector shape: (150,)
Accuracy of SVM Model: 100.0 %
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 16
versicolor 1.00 1.00 1.00 14
virginica 1.00 1.00 1.00 15
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
Confusion Matrix:
[[16 0 0]
[ 0 14 0]
[ 0 0 15]]
Result:
The Support Vector Machine (SVM) algorithm was successfully implemented using the Iris
dataset.
The model achieved an accuracy of 100%, showing that the SVM classifier can effectively
classify the iris flower species based on the given features.
-Submitted by:
Name: [Link]
Roll no: 23B81A7362
Branch: AIM