Sub topic 18
When to Use an SVM and When Not To
This chapter assumes you know the soft-margin SVM, the role of C and gamma, the kernel trick, feature scaling, and cross-validated grid search from earlier chapters. Here you will use them to decide whether an SVM suits a problem.
Where SVMs do well
- Small to medium datasets. With hundreds to tens of thousands of rows, an RBF SVM is often competitive with much more complex models.
- Many features relative to samples. A linear SVM handles text data, such as TF-IDF vectors with tens of thousands of columns, because the margin penalty limits overfitting.
- Compact models. Only support vectors are stored, so a well-separated problem gives a small model.
- Few knobs. For an RBF SVM you tune only C and gamma.
Where SVMs struggle
- Training cost. Kernel SVM training grows between roughly quadratic and cubic in the number of samples. Beyond about 100,000 rows it is usually too slow.
- Scale sensitivity. Distances and dot products drive everything, so unscaled features can dominate the kernel.
- Probabilities. An SVM outputs a score, not a probability. In scikit-learn,
probability=Trueadds an internal cross-validated calibration step. That makes training slower, andpredict_probacan disagree withpredict. - Noisy, overlapping classes. Most points become support vectors. Prediction slows down, and the model gains little over simpler ones.
- Mixed or messy tabular data. Categorical columns and missing values fit tree ensembles more naturally.
- Interpretability. A kernel model has no per-feature coefficients. Only the linear kernel does.
A decision guide
| Situation | Suggested choice |
|---|---|
| Under about 50k rows, dense numeric features | `SVC` with RBF, tuned C and gamma |
| Very many rows or sparse text features | `LinearSVC` or SGD with hinge loss |
| You need calibrated probabilities | Logistic regression, or calibrate the SVM scores |
| Mixed types, missing values, large tables | Gradient-boosted trees |
| Need to explain each feature's effect | Linear model |
A practical default
The function below picks a model by data size. The 20,000-row threshold is a rule of thumb, not a fixed limit.
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC, SVC
def choose_svm(n_samples):
if n_samples > 20_000:
return make_pipeline(StandardScaler(), LinearSVC(C=1.0, dual=False))
return make_pipeline(StandardScaler(), SVC(kernel="rbf", C=1.0, gamma="scale"))
X, y = make_classification(n_samples=1000, n_features=20, random_state=0)
model = choose_svm(len(X))
print(cross_val_score(model, X, y, cv=5).mean())Diagnosing a poor fit
- Check that scaling happens inside the pipeline.
- Compare training and validation scores. A big gap means C or gamma is too high.
- Count support vectors with
n_support_. If nearly every row is one, the problem is probably noisy or gamma is too large.
Exercise
You have 500,000 rows of sparse TF-IDF text features and need a fast classifier with an accuracy score. Which SVM variant would you try first, and why not an RBF SVC?
Show answer
Try LinearSVC (or SGDClassifier with hinge loss). Text features are high-dimensional and often close to linearly separable, and a linear solver scales roughly linearly with the number of rows. An RBF SVC would take roughly quadratic to cubic time in 500,000 samples and would be impractically slow.