Sub topic 18
When to Use an SVM and When Not To
This chapter assumes you know how the soft-margin SVM, the kernel trick, and the C and gamma parameters work, and that you can run a cross-validated grid search in scikit-learn. Here we turn that knowledge into a decision. We cover what SVMs do well, where they fail, and how to check a candidate dataset before committing to one.
Where SVMs are strong
- Small to medium datasets. With a few hundred to a few tens of thousands of rows, a tuned RBF SVM is often competitive with much more complex models.
- High-dimensional data with few samples. Text features, gene expression, and similar data can have more columns than rows. A linear SVM handles this well because the margin penalty (the 0.5·||w||² term) regularizes the weights, and the dual problem depends on the number of samples, not the number of features.
- Clear class separation. When classes are separated by a wide margin, few support vectors define the boundary, and the model is compact and stable.
- Nonlinear structure without feature engineering. The RBF kernel captures curved boundaries with only two hyperparameters, C and gamma.
- A convex training problem. Training has one global optimum, so results do not depend on random initialization the way neural network training does.
Where SVMs are weak
Cost grows faster than linearly with data
Kernel SVM training with libsvm (the solver behind SVC) typically scales between O(n²) and O(n³) in the number of samples. It also stores kernel values for the working set. In practice, fitting on 10,000 rows is comfortable, 100,000 rows can take minutes to hours, and a million rows is usually impractical. Prediction cost grows with the number of support vectors, which tends to grow with n on noisy data.
Probabilities are an add-on
An SVM outputs a signed distance to the boundary, not a probability. SVC(probability=True) fits a Platt-scaling sigmoid using internal 5-fold cross-validation. This multiplies training time, and the probabilities can disagree with predict on borderline points. If you need well-calibrated probabilities, logistic regression or a calibrated gradient-boosted model is a more direct choice.
Sensitivity to feature scale and hyperparameters
Distances drive the RBF kernel, so an unscaled feature with a large range dominates. You must scale inside a pipeline. Poor C and gamma choices give either underfitting or a model that memorizes the training set, so tuning is not optional.
Limited interpretability
A linear SVM exposes the weight vector coef_, which you can inspect. A kernel SVM is described by its support vectors and dual coefficients, which do not map to per-feature effects.
Weak fit for some data types
SVMs do not handle missing values natively. They do not learn representations, so on images, audio, and raw text, deep networks usually win. On tabular data with mixed types, categorical columns, and missing values, gradient-boosted trees are usually the stronger default.
Choosing the SVM variant
The practical question is often which SVM to use, not whether to use one.
| Situation | Suggested choice |
|---|---|
| Many features, text-like, sparse | `LinearSVC` |
| Rows above roughly 50,000, linear boundary acceptable | `LinearSVC` or `SGDClassifier(loss="hinge")` |
| Under roughly 20,000 rows, curved boundary | `SVC(kernel="rbf")` with tuned C and gamma |
| Need approximate nonlinearity on large data | `Nystroem` or `RBFSampler` features feeding a linear SVM |
| Need calibrated probabilities | Logistic regression, or `CalibratedClassifierCV` around an SVM |
| Regression on a small dataset | `SVR` |
LinearSVC uses liblinear, which scales roughly linearly with the number of samples. It also differs slightly from SVC(kernel="linear"): by default it uses squared hinge loss and also regularizes the intercept. Do not expect identical coefficients.
Measuring the cost on your own data
The following script compares a kernel SVM and a linear SVM as the sample count grows. Run it to see the scaling gap on your machine. Exact times will vary.
import time
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC, LinearSVC
for n in [1000, 4000, 16000]:
X, y = make_classification(
n_samples=n, n_features=20, n_informative=10, random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
models = {
"SVC rbf": make_pipeline(StandardScaler(), SVC(kernel="rbf")),
"LinearSVC": make_pipeline(StandardScaler(), LinearSVC(dual=False)),
}
for name, model in models.items():
start = time.perf_counter()
model.fit(X_train, y_train)
elapsed = time.perf_counter() - start
acc = model.score(X_test, y_test)
print(f"n={n:6d} {name:10s} fit={elapsed:6.2f}s acc={acc:.3f}")Expect the SVC fit time to grow much faster than the sample count, while LinearSVC grows close to linearly. If the accuracy gap is small, the linear model is the better engineering choice.
A decision procedure
Treat the thresholds as rough guides. They depend on hardware, feature count, and how noisy the labels are, so measure on your own data.
Common errors
- Forgetting to scale. Fitting the scaler on the full dataset before cross-validation leaks information. Put
StandardScalerinside the pipeline, as in the tuning chapter. - Tuning on too coarse a grid. C and gamma interact: a large gamma needs a smaller C to avoid overfitting. Search both on logarithmic grids.
- Using accuracy on imbalanced data. A majority-class predictor can score highly. Use
class_weight="balanced", and evaluate with F1, precision-recall, or ROC AUC. - Reading
predict_probaas ground truth. Withprobability=True, the probabilities come from a separate fitted sigmoid and can be poorly calibrated on small data. - Using a kernel SVM by default on large data. Try
LinearSVCfirst, then move to a kernel model only if the validation score justifies the cost.
Summary of trade-offs
Use an SVM when the dataset is small or medium, features are numeric and scaled, a margin-based boundary suits the problem, and you do not need probabilities. Avoid it when the data is very large, when you need calibrated probabilities or easy explanations, or when the input is raw perceptual data. For large linear problems, the linear SVM keeps most of the benefits at a fraction of the cost.
Exercise
You have 400,000 rows of sparse text features (50,000 columns) and need a fast classifier. A colleague suggests SVC(kernel="rbf"). What would you use instead, and why?
Show answer
Use LinearSVC (or SGDClassifier with hinge loss). Kernel SVM training scales between O(n²) and O(n³) in the number of samples, so 400,000 rows is impractical. Sparse, high-dimensional text is usually close to linearly separable, so a linear SVM scales roughly linearly with rows and gives similar accuracy.
Exercise
A model built with SVC(probability=True) gives a predicted probability of 0.48 for a point, yet predict returns the positive class. Why can this happen, and what should you do if you need reliable probabilities?
Show answer
predict uses the sign of the decision function, while predict_proba comes from a separate Platt-scaling sigmoid fitted with internal cross-validation. The two can disagree near the boundary. For reliable probabilities, use logistic regression, or wrap the SVM in CalibratedClassifierCV and check calibration on held-out data.