Linear Support Vector Machine (SVM)
A Linear Support Vector Machine (SVM) is a supervised classification algorithm that finds the linear hyperplane maximizing the geometric margin between classes. For data that is not strictly linearly separable, a soft-margin Linear SVM introduces slack variables and a regularization parameter C to balance margin width against margin violations and misclassifications.
Study Card: Linear Support Vector Machine (SVM)
Direct Answer
A Linear Support Vector Machine (SVM) is a supervised classification algorithm that separates classes in the input feature space using an optimal linear hyperplane that maximizes the geometric margin between the closest samples of opposite classes.
When datasets are noisy or not linearly separable, a soft-margin Linear SVM solves the primal convex optimization problem:
where and slack variables permit margin violations controlled by . (Unlike non-linear SVMs, a Linear SVM does not apply non-linear kernel mappings to transform the feature space; it works directly with linear dot products, making it fast and well suited for high-dimensional sparse data such as text.)
Key Terms
- Hyperplane: A linear decision boundary separating classes (a line in 2D, a plane in 3D).
- Margin: The distance between the positive and negative margin boundaries .
- Support Vectors: The training instances that lie on or inside the margin (), which uniquely determine and .
- Soft Margin: Allows margin violations and misclassifications via slack variables so noisy or overlapping classes can still be modeled robustly.
- Regularization Parameter (): Controls the penalty on slack variables—large enforces fewer training errors (narrower margin, higher variance), while small favors a wider margin with more tolerance for violations (higher bias).
Example
Consider classifying emails as spam or not spam based on TF-IDF word frequencies. A Linear SVM learns a hyperplane in this high-dimensional feature space separating spam from legitimate email. Only the borderline emails closest to the boundary (or on the wrong side of the margin) act as support vectors. If a few legitimate newsletters share words common in spam, a soft margin with tuned allows those violations rather than overfitting a razor-thin boundary to noise.
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs # Sample data
# Generate sample data
X, y = make_blobs(n_samples=100, centers=2, random_state=6)
# Fit a linear SVM with different C values (regularization parameter)
for c_value in [0.1, 1, 10, 1000]:
clf = svm.SVC(kernel='linear', C=c_value)
clf.fit(X, y)
# Plot data points first so axes limits match the actual data range
plt.figure(figsize=(6, 4))
ax = plt.gca()
ax.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# Create grid over the data range to evaluate decision function
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 = clf.decision_function(xy).reshape(XX.shape)
# Plot decision boundary (0) and margins (-1, +1)
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
# Highlight support vectors
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
linewidth=1, facecolors='none', edgecolors='k', label='Support Vectors')
ax.set_title(f"Linear SVM (C={c_value})")
ax.legend()
plt.show()
# In real-world applications, standardize features first and use cross-validation to tune C.
Related Concepts
- Kernel Methods (Non-linear SVM): When a linear boundary in input space is insufficient, non-linear SVMs apply the kernel trick (e.g., RBF or polynomial kernels) to compute dot products implicitly in a higher-dimensional feature space.
- Soft Margin Classification & Hinge Loss: Equivalently written in unconstrained form as .
- Primal vs. Dual Formulation: Linear SVMs with are efficiently solved in the primal (e.g.,
sklearn.svm.LinearSVCscales ), whereas kernel SVMs solve the -variable dual problem using Lagrange multipliers . - Feature Scaling: Because penalizes weights equally across coordinates, standardizing features beforehand is essential so large-scale features do not distort the margin.
- Bias-Variance Tradeoff: Tuning directly navigates underfitting (small , wide margin) vs. overfitting (large , tight margin).