Support Vector Machine Algorithm

๐Ÿ” Support Vector Machine Algorithm: Explained with Python Example

Support Vector Machine Algorithm

Machine Learning has revolutionized the way we interact with data, and among its most powerful tools is the Support Vector Machine (SVM). A supervised learning algorithm, SVM is mainly used for classification problems but can also be adapted for regression tasks. Its strength lies in its ability to find the optimal boundary between classes โ€” making it one of the most accurate and versatile algorithms in machine learning.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

๐ŸŽฏ What is SVM?

At its core, SVM aims to find the best possible line (or hyperplane) that divides an n-dimensional space into distinct classes. In the future, this decision boundary aids in accurately classifying fresh data points. However, why is this limit ideal? The idea of maximal margin holds the key to the solution.

This hyperplane is determined using the support vectors โ€” the extreme data points that are closest to the boundary. These are the most crucial elements of your dataset because they define the margin and the boundary. Hence the name: Support Vector Machine.

๐Ÿฑ SVM in Action: A Simple Example

Imagine youโ€™re training a model to classify animals โ€” say, cats and dogs. You feed the model numerous images of both animals. Now, you encounter a peculiar creature that looks like both. The SVM model, by looking at the most extreme features (support vectors) of previously learned cats and dogs, classifies this new input with precision.

This elegant mechanism makes SVM ideal for tasks such as:

  • Face Detection
  • Image Classification
  • Text Categorization

๐Ÿง  Types of SVM

SVM can be categorized into two main types:

  1. Linear SVM
    • Used when data is linearly separable
    • Classes can be divided by a single straight line (or hyperplane).
  2. Non-Linear SVM
    • Used when data is not linearly separable
    • Uses kernel techniques to convert data into higher dimensions so that it can be separated.

Download New Real Time Projects :-Click here

๐Ÿ”บ Hyperplane & Support Vectors

  • Hyperplane: The ideal line in the feature space that divides several classes.
  • Support Vectors: The data points that are closest to the hyperplane. These specify the hyperplaneโ€™s orientation and location. Removing these would alter the classifier.

SVM seeks to maximise the margin, or the separation between the closest support vectors from both classes and the hyperplane. The wider the margin, the better the generalization.

โš™๏ธ How Does SVM Work?

โœ… Linear SVM:

Assume that we have two classes (green and blue) and two features (x1 and x2). There could be multiple lines separating these classes, but SVM finds the one with the maximum margin. This line becomes the optimal hyperplane.

โŽ Non-Linear SVM:

Real-world data is often non-linear. In such cases, SVM applies a kernel function to project data into a higher-dimensional space (e.g., from 2D to 3D) where it becomes linearly separable.

Example:
Our data can be mapped into a 3D space using the following if it cannot be separated in 2D:

z = xยฒ + yยฒ

Now a simple plane in 3D can separate the data, which, when projected back into 2D, forms a circular boundary.

๐Ÿ’ป Implementing SVM in Python

Letโ€™s now walk through implementing the SVM classifier using the popular scikit-learn library.

1. ๐Ÿ“ฅ Data Pre-processing

import numpy as np  
import matplotlib.pyplot as plt  
import pandas as pd  
  
# Importing dataset
dataset = pd.read_csv('user_data.csv')  
  
# Extract features and labels
x = dataset.iloc[:, [2, 3]].values  
y = dataset.iloc[:, 4].values  
  
# Split into train and test sets
from sklearn.model_selection import train_test_split  
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)  
  
# Feature scaling
from sklearn.preprocessing import StandardScaler    
sc = StandardScaler()    
x_train = sc.fit_transform(x_train)    
x_test = sc.transform(x_test)

2. ๐Ÿง  Fitting the SVM Classifier

from sklearn.svm import SVC  

# Using a linear kernel
classifier = SVC(kernel='linear', random_state=0)  
classifier.fit(x_train, y_train)

3. ๐Ÿ“Š Making Predictions

# Predict test set results
y_pred = classifier.predict(x_test)

4. ๐Ÿ“‰ Evaluating with Confusion Matrix

from sklearn.metrics import confusion_matrix  

cm = confusion_matrix(y_test, y_pred)
print(cm)

Output: If the confusion matrix shows more correct predictions than incorrect ones, it indicates good model performance.

5. ๐Ÿ“ˆ Visualizing the Results (Training Set)

from matplotlib.colors import ListedColormap  

x_set, y_set = x_train, y_train  
x1, x2 = np.meshgrid(np.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step = 0.01),  
                     np.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))  

plt.contourf(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),  
             alpha = 0.75, cmap = ListedColormap(('red', 'green')))  
plt.xlim(x1.min(), x1.max())  
plt.ylim(x2.min(), x2.max())  

for i, j in enumerate(np.unique(y_set)):  
    plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],  
                c = ListedColormap(('red', 'green'))(i), label = j)  

plt.title('SVM (Training set)')  
plt.xlabel('Age')  
plt.ylabel('Estimated Salary')  
plt.legend()  
plt.show()

6. ๐Ÿ“‰ Visualizing the Results (Test Set)

x_set, y_set = x_test, y_test  
# (Similar visualization code as above)

๐ŸŽฏ Conclusion

Support Vector Machines are powerful, reliable, and highly effective for both linear and non-linear classification problems. By maximizing the margin and focusing on critical boundary cases, SVM ensures robust performance, even in complex datasets.

Whether youโ€™re working with images, text, or structured data, SVM offers a scalable and mathematically sound solution. Try tweaking kernels (linear, rbf, poly) and hyperparameters (C, gamma) to further improve performance.


Support Vector Machine Algorithm example
Support Vector Machine Algorithm machine learning
Support Vector Machine Algorithm pdf
hyperplane in Support Vector Machine Algorithm
svm solved example
support vector regression
linear svm
Support Vector Machine Algorithm
support vector machine algorithm in python
support vector machine algorithm example
support vector machine algorithm geeksforgeeks
support vector machine algorithm example
support vector machine algorithm in python
support vector machine algorithm geeksforgeeks
svm algorithm in machine learning
support vector machine pdf
hyperplane in svm
support vector regression
svm algorithm steps

2 comments

Post Comment