Sub topic 17
Training algorithms and how cost grows with data
This chapter assumes you know the dual SVM problem, the kernel trick, and the role of support vectors from earlier chapters. It explains why kernel SVMs slow down on large datasets and what to use instead.
What the solver does
Training a kernel SVM means solving the dual problem. This is a quadratic program (QP) with one variable alpha_i per training point, and its coefficients come from the kernel matrix K, which is n x n.
General-purpose QP solvers scale poorly. scikit-learn's SVC wraps libsvm, which uses a decomposition method called SMO (Sequential Minimal Optimization). SMO repeatedly picks two alphas, solves that two-variable problem in closed form, and updates. It never builds the full matrix. It computes kernel rows on demand and keeps recent rows in a cache.
How cost grows
In practice, SVC training time grows between roughly n^2 and n^3 in the number of samples. The exact rate depends on C, the kernel, and how many points end up as support vectors. Memory depends on the kernel cache. If the cache is too small, rows are recomputed and training slows down.
Prediction cost is separate. Each prediction evaluates the kernel against every support vector, so it scales with the number of support vectors. A noisy problem with a large C or gamma can leave most training points as support vectors, which makes prediction slow.
| Method | Training cost | Nonlinear boundary |
|---|---|---|
| `SVC` (libsvm, SMO) | about `n^2` to `n^3` | yes, via kernels |
| `LinearSVC` (liblinear) | about linear in `n` | no |
| `SGDClassifier(loss="hinge")` | linear in `n` per epoch | no |
| Kernel approximation + linear model | linear in `n` | approximate |
Measuring it
The script below times both solvers as n doubles. Expect SVC time to grow much faster than LinearSVC time.
import time
from sklearn.datasets import make_classification
from sklearn.svm import SVC, LinearSVC
for n in [1000, 2000, 4000, 8000]:
X, y = make_classification(n_samples=n, n_features=20, random_state=0)
for name, model in [("SVC rbf", SVC(kernel="rbf")),
("LinearSVC", LinearSVC(dual=False))]:
start = time.perf_counter()
model.fit(X, y)
elapsed = time.perf_counter() - start
print(f"n={n:5d} {name:9s} {elapsed:.3f}s")Choosing a path when data grows
Nystroem and RBFSampler map inputs to an explicit approximate feature space. A fast linear SVM then runs on those features, so you keep most of the nonlinearity at linear cost.
Exercise
You double the training set from 20,000 to 40,000 samples. Roughly how much longer should RBF SVC training take, and what two changes could keep training time manageable?
Show answer
Between about 4 times (n^2) and 8 times (n^3) longer. You could switch to LinearSVC or SGDClassifier if a linear boundary is enough. Otherwise, use Nystroem or RBFSampler with a linear model to approximate the kernel at linear cost.