Wednesday, February 25, 2026

Support Vector Machines in Machine Learning

Support Vector Machines in Machine Learning

Introduction

Support Vector Machines (SVMs) are powerful supervised learning algorithms used for classification, regression, and even outlier detection. They are particularly effective in high-dimensional spaces and are widely applied in fields like image recognition, text classification, and bioinformatics.

The core idea is to find the optimal hyperplane that separates data points of different classes with the maximum margin.


Key Concepts

  • Hyperplane: The decision boundary separating classes. In 2D it’s a line, in 3D a plane, and in higher dimensions a hyperplane.
  • Support Vectors: Data points closest to the hyperplane. They directly influence its position and orientation.
  • Margin: The distance between the hyperplane and the nearest support vectors. SVM maximizes this margin for robustness.
  • Kernel Trick: A mathematical technique that allows SVMs to classify non-linear data by mapping it into higher-dimensional space.

The SVM Algorithm

  1. Input: Training dataset ((x_i, y_i)) where (x_i) are feature vectors and (y_i \in {-1, +1}).
  2. Objective: Find a hyperplane defined as:
    [ w \cdot x + b = 0 ]
    that maximizes the margin between classes.
  3. Optimization Problem:
    [ \min_{w, b} \frac{1}{2} |w|^2 ]
    subject to:
    [ y_i(w \cdot x_i + b) \geq 1 \quad \forall i ]
  4. Kernel Extension: Replace dot products with kernel functions (K(x_i, x_j)) to handle non-linear data.
  5. Output: A decision function that classifies new data points based on which side of the hyperplane they fall.

Python Implementation (Scikit-learn)

Here’s a simple example using scikit-learn:

# Import libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

# Load dataset (Iris dataset)
iris = datasets.load_iris()
X = iris.data[:, :2]  # Using first two features for visualization
y = iris.target

# Binary classification (class 0 vs class 1)
X = X[y != 2]
y = y[y != 2]

# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Train SVM model with linear kernel
model = SVC(kernel='linear', C=1.0)
model.fit(X_train, y_train)

# Evaluate
accuracy = model.score(X_test, y_test)
print("Test Accuracy:", accuracy)

# Plot decision boundary
w = model.coef_[0]
b = model.intercept_[0]
x_points = np.linspace(min(X[:,0]), max(X[:,0]), 100)
y_points = -(w[0]/w[1]) * x_points - b/w[1]

plt.scatter(X[:,0], X[:,1], c=y, cmap='coolwarm')
plt.plot(x_points, y_points, color='black')
plt.title("SVM Decision Boundary")
plt.show()

This code:

  • Loads the Iris dataset
  • Trains a linear SVM classifier
  • Evaluates accuracy
  • Plots the decision boundary

Advantages and Limitations

AspectStrengthLimitation
AccuracyHigh accuracy in classification tasksSensitive to choice of kernel and parameters
VersatilityWorks well in high-dimensional spacesComputationally expensive for large datasets
GeneralizationMaximizes margin for robustnessLess effective when classes overlap significantly

Conclusion

Support Vector Machines remain one of the most reliable and versatile algorithms in machine learning. Their ability to handle both linear and non-linear data makes them indispensable in real-world applications ranging from spam detection to medical diagnosis.

No comments:

Post a Comment

Support Vector Machines in Machine Learning

Support Vector Machines in Machine Learning Introduction Support Vector Machines (SVMs) are powerful supervised learning algorithms used ...