Data that can be separated by a line or hyperplane is know as linearly separable data. The hyperplane acts as a linear classifier.

A Support Vector Machine (SVM) is supervised machine learning algorithm used primarily for classification tasks. The core idea behind an SVM is to find the best boundary (or hyperplane in higher dimensions) that separates classes of data points with the maximum margin. The "margin" is defined as the distance between the separating hyperplane (decision boundary) and the closest data points from each class, which are called "support vectors." This concept can be extended to non-linear boundaries using something called the kernel trick.

Linear SVM Classification:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
from matplotlib import colors

# Create a synthetic dataset
X, y = make_blobs(n_samples=1000, centers=2, random_state=6)

# Initialize and train the SVM
svm = SVC(kernel='linear')
svm.fit(X, y)

# Plotting
fig, ax = plt.subplots()

# Plot the decision boundary
ax.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap=colors.ListedColormap(['blue', 'red']))
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# Create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = svm.decision_function(xy).reshape(XX.shape)

# Plot decision boundary and margins
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
           linestyles=['--', '-', '--'])

# Plot support vectors
ax.scatter(svm.support_vectors_[:, 0], svm.support_vectors_[:, 1], s=100,
           linewidth=1, facecolors='none', edgecolors='k')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('SVM Decision Boundary with Support Vectors')
plt.show()

Untitled