Skip to content
E
Educatum

Linear Support Vector Machine (SVM)

Curated by Educatum AI teamLast updated:
Basic Concepts
Direct Answer

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 wTx+b=0w^T x + b = 0 that maximizes the geometric margin 2∥w∥2\frac{2}{\|w\|_2} 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:

min⁡w,b,ξ12∥w∥22+C∑i=1nξisubject toyi(wTxi+b)≥1−ξi,ξi≥0\min_{w, b, \xi} \frac{1}{2}\|w\|_2^2 + C \sum_{i=1}^{n} \xi_i \qquad \text{subject to} \quad y_i(w^T x_i + b) \ge 1 - \xi_i,\quad \xi_i \ge 0

where yi∈{−1,+1}y_i \in \{-1, +1\} and slack variables ξi\xi_i permit margin violations controlled by CC. (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 wTx+b=0w^T x + b = 0 separating classes (a line in 2D, a plane in 3D).
  • Margin: The distance 2∥w∥2\frac{2}{\|w\|_2} between the positive and negative margin boundaries wTx+b=±1w^T x + b = \pm 1.
  • Support Vectors: The training instances that lie on or inside the margin (yi(wTxi+b)≤1y_i(w^T x_i + b) \le 1), which uniquely determine ww and bb.
  • Soft Margin: Allows margin violations and misclassifications via slack variables ξi\xi_i so noisy or overlapping classes can still be modeled robustly.
  • Regularization Parameter (CC): Controls the penalty on slack variables—large CC enforces fewer training errors (narrower margin, higher variance), while small CC 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 CC 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.
  • 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 12∥w∥22+C∑imax⁡(0,1−yi(wTxi+b))\frac{1}{2}\|w\|_2^2 + C\sum_i \max(0, 1 - y_i(w^T x_i + b)).
  • Primal vs. Dual Formulation: Linear SVMs with n≫dn \gg d are efficiently solved in the primal (e.g., sklearn.svm.LinearSVC scales O(n⋅d)O(n \cdot d)), whereas kernel SVMs solve the nn-variable dual problem using Lagrange multipliers αi\alpha_i.
  • Feature Scaling: Because ∥w∥22\|w\|_2^2 penalizes weights equally across coordinates, standardizing features beforehand is essential so large-scale features do not distort the margin.
  • Bias-Variance Tradeoff: Tuning CC directly navigates underfitting (small CC, wide margin) vs. overfitting (large CC, tight margin).
View all →

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.