Support Vector Machine (SVM)
Support Vector Machine (SVM)
example
Support Vector Machine (SVM) is a supervised machine learning algorithm used for
classification and regression tasks. It is particularly effective for high-dimensional datasets
and works well for both linear and non-linear data.
1. Objective of SVM
The main goal of SVM is to find the optimal hyperplane that maximally separates data
points of different classes. The hyperplane is chosen such that it has the largest margin
between the closest points (support vectors) of the two classes.
2. Key Concepts
1. Input Data: The algorithm takes labeled data points (features and corresponding
class labels).
2. Choose Kernel Function: Based on the data, select a kernel function to transform it
into a higher-dimensional space (if necessary) to make it linearly separable.
○ Common kernels: Linear, Polynomial, Radial Basis Function (RBF), Sigmoid.
3. Construct Hyperplane: Find the hyperplane that separates the classes with the
maximum margin.
4. Classify New Data: Use the hyperplane to predict the class of new data points.
● Robustness: Focuses only on support vectors, ignoring the influence of other data
points.
● Flexibility: Works for both linearly and nonlinearly separable data using kernel tricks.
● High-dimensional data: Handles datasets with many features effectively.
By maximizing the margin, SVM ensures that the model is not only accurate but also
generalizes well to unseen data, reducing overfitting.
Applications of SVM
Problem: Classify emails as "Spam" or "Not Spam" based on features like word frequency,
presence of links, etc.
1. Input Data: Feature set includes attributes like:
○ Frequency of the word "offer."
○ Number of links in the email.
○ Use of capitalized words.
2. Visualization: Plot the data in a 2D/3D space using two or more features.
3. Training the SVM:
○ If the data is linearly separable, SVM identifies the hyperplane with maximum
margin.
○ If not, the kernel function (e.g., RBF) maps data into a higher-dimensional
space where it becomes linearly separable.
4. Hyperplane and Margin:
○ SVM constructs a decision boundary separating "Spam" and "Not Spam."
○ The closest emails to the boundary are the support vectors.
5. Classification:
○ For a new email, calculate its position relative to the hyperplane.
○ Assign "Spam" or "Not Spam" based on which side of the hyperplane it lies.
Python Code: Email Spam Classification with SVM
Here’s a Python example of using SVM with the scikit-learn library for email spam
classification:
Key Notes