0% found this document useful (0 votes)
15 views4 pages

SVM Classifier Implementation Guide

This document outlines a programming assignment focused on implementing Support Vector Machines (SVMs) classifiers for both linearly and non-linearly separable datasets. The assignment consists of two parts: creating a Linear SVM classifier from scratch and extending it to handle non-linear data using the kernel trick. Students are required to visualize decision boundaries, evaluate model performance, and submit a well-structured Jupyter Notebook by November 5, 2025.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

SVM Classifier Implementation Guide

This document outlines a programming assignment focused on implementing Support Vector Machines (SVMs) classifiers for both linearly and non-linearly separable datasets. The assignment consists of two parts: creating a Linear SVM classifier from scratch and extending it to handle non-linear data using the kernel trick. Students are required to visualize decision boundaries, evaluate model performance, and submit a well-structured Jupyter Notebook by November 5, 2025.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming Assignment-6 (PH227): Support

Vector Machines (SVMs) Classifier

Objective
This assignment aims to provide hands-on experience in implementing and understanding
the Support Vector Machine (SVM) algorithm. You will build an SVM classifier for both
linearly and non-linearly separable datasets, and visualize the decision boundaries and
margins learned by the model.

Part 1: Linear SVM Classifier

Problem Description
In this section, you will implement a Linear SVM classifier from scratch. You will work with
a 2D linearly separable dataset and visualize the learned decision boundary and margin.

Instructions
Complete the following steps for implementing and evaluating the Linear SVM classifier:

1.​ Dataset Loading:


○​ Load 2D dataset (Dataset-1) with two classes that are linearly separable.
2.​ Initial Visualization:
○​ Create a scatter plot of the Dataset-1, with data points color-coded as per
their respective classes.
○​ Ensure the plot is well-labeled with appropriate axis titles (e.g., "Feature 1,"
"Feature 2") and a descriptive title (e.g., "Linearly Separable Dataset-1").
○​
2.​ Data Splitting:
○​ Split the dataset into training and testing sets (e.g., 80% training, 20%
testing).
3.​ SVM Algorithm Implementation (from scratch):
○​ Implement the core components of a Linear SVM classifier. Your
implementation should include:
■​ Initialization: Set learning rate, and number of iterations.
■​ Weight and Bias Initialization: Initialize weights w and bias b to small
random values.
■​ Cost Function: Implement the hinge loss function.
( Hinge loss is a loss function used in machine learning for training
SVM classifiers
■​ Gradient Descent: Implement the gradient descent algorithm to
update w and b. The update rules should differentiate between
correctly classified points within the margin, misclassified points, and
correctly classified points outside the margin.
■​ Fit Method: A fit method that iteratively updates w and b using
gradient descent on the training data.
■​ Predict Method: A predict method that classifies new data points
based on the learned w and b.
4.​ Model Training:
○​ Train your custom Linear SVM model on the training data.
5.​ Visualization:
○​ Plot the training data points, color-coded by class.
○​ Visualize the decision boundary (hyperplane) learned by your Linear SVM
model.
○​ Draw the margins, which are the lines parallel to the decision boundary and
pass through the support vectors.
○​ Ensure axes are labeled and the plot has a title.
6.​ Model Testing:
○​ Evaluate your trained Linear SVM model on the testing data.
○​ Calculate and report the accuracy of the model on both training and testing
sets.

Part 2: Non-linear SVM Classifier

Problem Description
In this section, you will extend your SVM implementation to handle non-linearly separable
data using a kernel trick. You will work with a 2D non-linearly separable dataset (circular
boundary example) and visualize the decision boundary after projection.
Instructions
Complete the following steps for implementing and evaluating the Non-linear SVM
classifier:

1.​ Dataset Loading:


○​ Load 2D dataset (Dataset-2) with two classes that are non-linearly
separable.
2.​ Initial Visualization:
○​ Create a scatter plot of the Dataset-2, with data points color-coded
according to their respective classes. This visualization should clearly
illustrate the nonlinear separability of the data.
○​ Ensure the plot is well-labeled with appropriate axis titles (e.g., "Feature 1,"
"Feature 2") and a descriptive title (e.g., "Non-linearly Separable
Dataset-2").
3.​ Data Splitting:
○​ Split the dataset into training and testing sets (e.g., 80% training, 20%
testing).
4.​ Feature Transformation (Kernel Trick Concept):
○​ To handle non-linear separability, project the 2D data into a
higher-dimensional space (e.g., 3D). For circular data, you could use a
transformation like (x, y) -> (x, y, x^2 + y^2).
○​ Implement this feature transformation using function.
5.​ SVM Algorithm Implementation (from scratch - reuse linear SVM core):
○​ Adapt your Linear SVM implementation from Part 1 to work with the
transformed (higher-dimensional) data. The core SVM algorithm (cost
function, gradient descent) remains the same, but it now operates on the
new feature space.
6.​ Model Training:
○​ Train your custom SVM model on the transformed training data.
7.​ Visualization:
○​ 3D Visualization: For the transformed data, create a 3D scatter plot
showing the data points, color-coded by class.
○​ Decision Plane: Visualize the decision plane learned by your SVM in this 3D
space.
○​ Margin in 3D: Draw the margins around the decision plane.
○​ 2D Decision Boundary: Project the 3D decision plane back into the original
2D space to visualize the non-linear decision boundary as a curve.
○​ Ensure axes are labeled and the plot has a title.
8.​ Model Testing and Evaluation:
○​ Evaluate your trained Non-linear SVM model on the testing data (after
applying the same feature transformation to the test set).
○​ Calculate and report the accuracy of the model on both transformed training
and testing sets.

Submission Guidelines
●​ Submit a single Jupyter Notebook (.ipynb) file containing all your code.
●​ Clearly separate Part 1 and Part 2 with markdown headings.
●​ Ensure all code blocks are executed and produce the expected outputs.
●​ Add comments to explain your code, especially for custom SVM implementations
and complex logic.
●​ Ensure your code is well-structured and easy to read.
●​ For all plots, include appropriate titles, axis labels, and legends where necessary.

Evaluation Criteria
Your assignment will be evaluated based on the following:

●​ Correctness: Proper implementation of the Linear and Non-linear SVM algorithms


from scratch.
●​ Functionality: The code runs without errors and produces expected outputs.
●​ Visualization Quality: Clarity and effectiveness of generated plots (decision
boundaries, margins in both 2D and 3D).
●​ Code Quality: Readability, comments, and adherence to Python best practices.
●​ Understanding: Demonstrated understanding of the underlying concepts of SVM,
including the hinge loss, gradient descent, and the kernel trick.

Due Date
Please submit your completed Jupyter Notebook by Nov 5, 2025 11:59 PM .

Common questions

Powered by AI

Splitting a dataset into training and testing subsets ensures that a model can generalize well to new data. Typically, an 80-20% split is used where 80% is for training and 20% for testing. Training subset helps in fitting the model, while testing evaluates its accuracy and robustness, preventing overfitting as the model is validated against unseen data .

Feature transformation is essential in Non-linear SVMs as it maps the original input data into a higher-dimensional space, where a linear decision boundary can be more easily defined. This step enables the application of traditional linear SVM methods to datasets that are not linearly separable in their original space, thus enhancing the algorithm's capacity to deal with complex patterns .

Evaluation criteria include correctness of algorithm implementation, functionality without errors, quality of visualization depicting decision boundaries and margins, code readability adhering to Python best practices, and demonstrated understanding of SVM concepts like hinge loss, gradient descent, and kernel trick. These factors ensure a robust, accurate, and interpretable SVM model .

In a 3D space, decision boundaries are visualized as planes whose orientation and position are determined by the SVM model learned from the transformed data. Margins, represented as parallel planes, illustrate the span where support vectors reside. These visualizations, including a 2D projection of the 3D decision boundary, highlight the effectiveness of feature transformations in handling non-linear separability .

Factors affecting visualization quality include clear labeling of axes, choice of colors for class differentiation, comprehensive titles, and accuracy in depicting data points, decision boundaries, and margins. Managing these ensures clarity and effectiveness in conveying insights and model understanding, which are crucial for accurate interpretation and communication of results .

Implementing a Linear SVM classifier involves several components: initializing hyperparameters like learning rate and number of iterations, initializing weights and bias, defining the cost function through hinge loss, and updating weights and biases using gradient descent. The algorithm iteratively updates these parameters using training data for classification. The fit method uses these updates to build the model, while the predict method leverages the learned weights and bias to classify new data points .

The kernel trick enables SVMs to transform data into higher-dimensional spaces where linear separation might be feasible. This is achieved without explicitly computing new dimensions, allowing the SVM model to find a hyperplane that can separate classes that appear non-linearly separable in the original feature space. The transformation, such as projecting circular data to 3D, allows SVMs to handle complex datasets more effectively .

The hinge loss function is crucial for a Linear SVM as it quantifies the error of predictions. It encourages a margin of separation between classes, penalizing only those predictions that do not meet the minimum margin requirement. This guides the gradient descent process to adjust weights and bias for optimal separation of classes .

Support vectors are critical data points that lie closest to the decision boundary. They define the margin and thus directly influence the position and orientation of the decision boundary. Without them, the algorithm could potentially overfit or underfit the model, making them essential for achieving the optimal separating hyperplane in SVMs .

Visualization helps in illustrating the separation achieved by an SVM model, showing the decision boundary and how well it classifies different classes within a dataset. For a Linear SVM, it includes margin lines and support vectors. For non-linear SVM, it involves projecting higher-dimensional decision planes back into 2D space, demonstrating the efficacy of kernel transformations in achieving classification .

You might also like