MCKV Institute of Engineering
243, G.T. Road (N), Liluah, Howrah-711204
Machine Learning Lab
Assignment 15
Problem Statement:
Build an SVM classifier for the Iris dataset. Experiment with linear, polynomial, and RBF
kernels.
Program :
import numpy as np
import pandas as pd
import matplotlib . pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn . svm import SVC
from sklearn . model_selection import train_test_split
from sklearn . preprocessing import StandardScaler
from sklearn . metrics import accuracy_score , c l a s s i f i c a t i o n _ r e p o r t
# Step 1: Load Iris dataset
iris = datasets . load_iris ()
X = iris . data
y = iris . target
feature_names = iris . feature_names
target_names = iris . target_names
# Step 2: Use only 2 features ( petal length & petal width ) for 2 D
,→ visualization
X_2d = X [: , [2 , 3]] # petal length and width
X_train , X_test , y_train , y_test = train_test_split ( X_2d , y ,
,→ test_size =0.3 , random_state =42 , stratify = y )
# Step 3: Standardize the features
scaler = StandardScaler ()
X_train_scaled = scaler . fit_transform ( X_train )
X_test_scaled = scaler . transform ( X_test )
# Step 4: Train SVM with different kernels
kernels = [ ’ linear ’ , ’ poly ’ , ’ rbf ’]
models = {}
1
MCKV Institute of Engineering Machine Learning Lab
for kernel in kernels :
if kernel == ’ poly ’:
clf = SVC ( kernel = ’ poly ’ , degree =3 , gamma = ’ auto ’)
else :
clf = SVC ( kernel = kernel , gamma = ’ auto ’)
clf . fit ( X_train_scaled , y_train )
models [ kernel ] = clf
y_pred = clf . predict ( X_test_scaled )
acc = accuracy_score ( y_test , y_pred )
print ( f " \n - - - SVM with { kernel . upper () } kernel ---" )
print ( f " Accuracy : { acc :.2 f } " )
print ( " Classification Report : " )
print ( c l a s s i f i c a t i o n _ r e p o r t ( y_test , y_pred , target_names =
,→ target_names ) )
# Step 5: Visualize decision boundaries
def pl ot_ d e c i s i o n _ b o u n d a r y ( model , X , y , title ) :
h = 0.02
x_min , x_max = X [: , 0]. min () - 0.5 , X [: , 0]. max () + 0.5
y_min , y_max = X [: , 1]. min () - 0.5 , X [: , 1]. max () + 0.5
xx , yy = np . meshgrid ( np . arange ( x_min , x_max , h ) ,
np . arange ( y_min , y_max , h ) )
Z = model . predict ( np . c_ [ xx . ravel () , yy . ravel () ])
Z = Z . reshape ( xx . shape )
plt . figure ( figsize =(6 , 4) )
plt . contourf ( xx , yy , Z , cmap = plt . cm . coolwarm , alpha =0.6)
sns . scatterplot ( x = X [: , 0] , y = X [: , 1] , hue = iris . target_names [ y ] ,
,→ edgecolor = ’k ’)
plt . xlabel ( feature_names [2])
plt . ylabel ( feature_names [3])
plt . title ( title )
plt . tight_layout ()
plt . show ()
# Plot for each kernel
for kernel in kernels :
pl ot _ d e c i s i o n _ b o u n d a r y ( models [ kernel ] , X_test_scaled , y_test , f "
,→ SVM with { kernel . upper () } Kernel " )
Output:
--- SVM with LINEAR kernel ---
2 of 5
MCKV Institute of Engineering Machine Learning Lab
Accuracy: 0.91
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 15
versicolor 0.82 0.93 0.88 15
virginica 0.92 0.80 0.86 15
accuracy 0.91 45
macro avg 0.92 0.91 0.91 45
weighted avg 0.92 0.91 0.91 45
--- SVM with POLY kernel ---
Accuracy: 0.91
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 15
versicolor 0.79 1.00 0.88 15
virginica 1.00 0.73 0.85 15
accuracy 0.91 45
macro avg 0.93 0.91 0.91 45
weighted avg 0.93 0.91 0.91 45
--- SVM with RBF kernel ---
Accuracy: 0.91
Classification Report:
precision recall f1-score support
setosa 1.00 1.00 1.00 15
versicolor 0.82 0.93 0.88 15
virginica 0.92 0.80 0.86 15
accuracy 0.91 45
macro avg 0.92 0.91 0.91 45
weighted avg 0.92 0.91 0.91 45
3 of 5
MCKV Institute of Engineering Machine Learning Lab
Viva Questions
4 of 5
MCKV Institute of Engineering Machine Learning Lab
Sl. Question and Answer Marks
No.
1 Q: What is the basic idea behind a Support Vector Machine 2
(SVM)?
A: SVM is a supervised learning algorithm that finds the optimal hyper-
plane which maximally separates classes in a feature space. It works well
for both linear and non-linear classification tasks.
2 Q: Why are kernel functions used in SVM? 2
A: Kernels allow SVM to operate in a high-dimensional feature space
without explicitly computing the coordinates. They enable the algorithm
to perform non-linear classification by transforming the input space.
3 Q: What is the difference between linear, polynomial, and RBF 2
kernels?
A: The linear kernel separates data with a straight line. The polynomial
kernel uses polynomial curves of a specified degree. The RBF (Gaussian)
kernel maps data into an infinite-dimensional space and works well for
complex boundaries.
4 Q: Why is feature scaling important before training an SVM? 2
A: SVM relies on distances between data points. If features are on
different scales, one may dominate the others, leading to poor model
performance. Scaling ensures all features contribute equally.
5 Q: How do we visualize decision boundaries in a multi-class 2
SVM problem?
A: For 2D problems, we can plot the predicted class regions using con-
tour plots. Each region shows where the SVM classifies new points.
Different colors indicate different classes, and data points are overlaid for
comparison.
5 of 5