Implementing a linear, binary SVM support vector machine
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A linear binary SVM (Support Vector Machine) finds the hyperplane that maximizes the margin between two classes. The "support vectors" are the data points closest to the decision boundary — they alone determine the hyperplane's position. SVMs are effective for high-dimensional data and work well when classes are linearly separable. scikit-learn's LinearSVC and SVC(kernel='linear') provide production-ready implementations, while understanding the math behind the margin and the hinge loss helps you tune the model and interpret results.
SVM with scikit-learn
SVC(kernel='linear') trains a linear SVM using the SMO (Sequential Minimal Optimization) algorithm. The C parameter controls the trade-off between maximizing the margin and minimizing classification errors.
Using LinearSVC (Faster for Large Datasets)
LinearSVC uses the liblinear library and scales to millions of samples. It is significantly faster than SVC(kernel='linear') for large datasets because it uses a different optimization algorithm that avoids computing the kernel matrix.
Understanding the Math
The SVM finds w and b that maximize the margin 2/||w|| while correctly classifying training points. Points with |w·x + b| = 1 lie on the margin boundary and are the support vectors.
Visualizing the Decision Boundary
The solid line is the decision boundary (w·x + b = 0). The dashed lines are the margin boundaries (w·x + b = +/-1). Green circles mark the support vectors.
Effect of C Parameter
| C value | Margin | Tolerance for errors | Risk |
| Small (0.01) | Wide | High (soft margin) | Underfitting |
| Medium (1.0) | Balanced | Moderate | Good default |
| Large (100) | Narrow | Low (hard margin) | Overfitting |
Small C allows more margin violations for a wider margin. Large C penalizes violations heavily, fitting the training data more tightly.
SVM from Scratch (Gradient Descent)
This implements the hinge loss SVM using stochastic gradient descent. The loss function is max(0, 1 - y*(w·x + b)) plus L2 regularization ||w||^2. Production code should use scikit-learn, but this shows the core algorithm.
Common Pitfalls
- Not scaling features: SVMs are sensitive to feature scales because the margin depends on distances. A feature with range [0, 100000] dominates one with range [0, 1]. Always standardize with
StandardScalerbefore fitting. - Using SVC for large datasets:
SVChas O(n^2) to O(n^3) time complexity. For datasets over 10,000 samples, useLinearSVCorSGDClassifier(loss='hinge')which scale linearly. - Ignoring the C parameter: The default
C=1.0may not be optimal. UseGridSearchCVorRandomizedSearchCVto find the best C value for your data, typically searching over[0.001, 0.01, 0.1, 1, 10, 100]. - Expecting probability outputs:
SVCdoes not compute probabilities by default. Setprobability=Trueto enablepredict_proba(), but this adds computation time (Platt scaling) and may slow training. - Using linear SVM for non-linearly separable data: If classes overlap or have non-linear boundaries, a linear SVM will underfit. Use
SVC(kernel='rbf')for non-linear decision boundaries, or engineer polynomial features.
Summary
SVC(kernel='linear')andLinearSVCimplement linear binary SVMs in scikit-learn- The SVM finds the hyperplane that maximizes the margin between two classes
Ccontrols the margin width: small C = wide margin (may underfit), large C = narrow margin (may overfit)- Always scale features with
StandardScalerbefore fitting an SVM - Use
LinearSVCfor datasets over 10,000 samples (faster thanSVC) - Support vectors are the training points closest to the decision boundary — they define the model

