Implementing a linear, binary SVM support vector machine
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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
Related reading
- Implementing a many-to-many LSTM in TensorFlow?
- Implementing Bag-of-Words Naive-Bayes classifier in NLTK
- Implementing Binary Cross Entropy loss gives different answer than Tensorflow's
- Implementing contrastive loss and triplet loss in Tensorflow
- Implementing custom loss function in keras with condition
- Implementing custom loss function in keras with different sizes for y_true and y_pred
- Implementing custom loss function in scikit learn
- Implementing dropout from scratch
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.