Support Vector Machines and Support Vector Regression › Practical Implications
Machine Learning from ScratchAll chapters Support Vector Machines and Support Vector Regression › Practical ImplicationsSection view · Sections in this chapter8 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
This section covers when to use SVMs, recommended practices, common pitfalls, and computational and deployment considerations.
When to Use SVMs
SVMs are useful candidates for moderate-sized nonlinear problems and for high-dimensional applications such as sparse text classification or bioinformatics. A kernel is especially attractive when it expresses a meaningful similarity that would be awkward to construct as explicit features. Dedicated linear SVM solvers can also handle much larger sparse datasets. The convex objective gives every exact optimum the same objective value, but bias terms or dual coefficients can be nonunique, and finite solver tolerances can produce different fitted scores. Compare models using validation and record the loss, normalization, preprocessing, and solver settings when reproducing a result.
For tabular data with mixed numerical and categorical inputs, compare the SVM with tree ensembles or gradient boosting using suitable preprocessing and validation. Runtime differences depend on sample size, representation, and tuning budget. A nonlinear kernel sum does not automatically provide a decomposition into per-feature contributions, whereas a linear SVM has the additive score . If probabilities are required, include calibration and its validation in the comparison.
Best Practices
Use C=1 and gamma="scale" as a baseline for an RBF classifier, then compare relevant kernels and parameter ranges with cross-validation, following Hyperparameter Search and Nested Cross-Validation. A logarithmic range such as through is a starting search range, not a universal one. For large linear problems, consider LinearSVC or SGDClassifier(loss="hinge"), which avoid the kernel Gram matrix. Compare their validation results rather than expecting identical boundaries. SVC(kernel="linear") uses hinge loss with an unpenalized intercept. LinearSVC defaults to squared hinge loss and penalizes the intercept. SGDClassifier(loss="hinge") also leaves its intercept unpenalized, while using a mean-loss objective with alpha regularization. LinearSVC and SGDClassifier use different solvers and stopping rules from SVC.
Set random_state where the chosen solver or calibration procedure uses randomness. For imbalanced classes, compare class weights or resampling performed only within the training fold; oversampling before splitting can place copies of the same example in training and validation. When probabilities are required, compare SVC(probability=True) with an explicit procedure such as CalibratedClassifierCV using held-out probability metrics and calibration plots. An explicit wrapper can make the calibration protocol easier to control, but it is not guaranteed to improve probability estimates.
Data Requirements and Preprocessing
Feature scales affect the SVM's geometry and regularization. Changing the units of one input changes its contribution to an RBF distance and the penalty associated with an equivalent linear coefficient. This can distort a fit when the units are arbitrary, although a high-variance feature does not automatically make the model ignore every other feature. Standardization is a useful default for many numerical datasets; other representations, such as normalized sparse text features or a domain-specific kernel, may call for a different choice. For sparse inputs, StandardScaler(with_mean=False) scales features without centering; its default centering raises an error on sparse matrices. Fit any learned scaling statistics on the training fold only, and reuse them on validation and test inputs. Estimating them from held-out data would leak information into the fitted procedure.
Handle missing values using preprocessing supported by the chosen estimator. Fit the imputer, scaler, and SVM together within each training fold so that prediction uses the training statistics. For a custom kernel, symmetry and positive semidefiniteness must hold on every finite input collection. Negative training-Gram eigenvalues beyond numerical error reveal a failure of that matrix's PSD condition; nonnegative eigenvalues on one dataset do not prove global kernel validity. An invalid kernel can make the dual nonconcave, depending on its behavior on the feasible directions.
Common Pitfalls
Do not interpret SVM decision scores as probabilities. In the canonical binary formulation, a point with score lies at distance from the hyperplane when . This is twice the canonical one-sided margin distance . Signed geometric distance is . It does not imply odds or a fixed probability. A second issue is using an arbitrary similarity function as a kernel. The function must be symmetric and produce a positive-semidefinite Gram matrix on every finite input set. Logistic-style similarities do not satisfy this requirement in general and can destroy the dual's concavity. Checking one training matrix can reveal a failure, but cannot prove that a function is a valid kernel on all inputs.
Oversampling before splitting can leak copies of observations across the training-validation boundary. Perform any resampling within training folds. Likewise, fit learned preprocessing within each training fold: changing measurement units can alter the regularization and kernel geometry, while sensible pre-existing scales may already suit the task. Defaults such as C=1 are baselines whose validation performance must be checked. Compare alternatives with comparable tuning budgets and use an untouched test set for the final performance estimate.
Computational Considerations
A full kernel Gram matrix contains entries, so explicitly storing it requires quadratic memory. Decomposition solvers such as libsvm can compute kernel entries as needed and retain a cache instead. Exact nonlinear kernel fitting can still become expensive as grows: feature dimension, kernel-evaluation cost, cache reuse, data geometry, and solver tolerance all affect runtime. Dense quadratic-programming steps can involve cubic-cost linear algebra, but that is not a universal bound on total SVM training time. Measure fitting time and memory for the intended data size; dedicated linear solvers are often preferable when a linear boundary is adequate.
For larger linear problems, consider LinearSVC or SGDClassifier(loss="hinge"), which avoid materializing the kernel matrix. Linear passes through a fixed feature representation can scale with the number of nonzero input entries; total training time also depends on convergence. For a nonlinear approximation, RBFSampler constructs random Fourier features for the RBF kernel, while Nystroem constructs a feature map using a subset of training points as landmarks. Fit a linear model on the transformed inputs and validate the approximation dimension as well as the model settings. For libsvm models, cache_size controls the kernel cache in megabytes, with a default of 200 MB in scikit-learn 1.8; it does not cap all training memory.
Performance and Deployment Considerations
Use cross-validation on the training data to select the model and evaluate the selected procedure on an untouched test split when one is available. Choose accuracy, F1, ROC AUC, or another metric to match class balance and the cost of errors. For a kernel model, the support-vector count helps estimate storage and prediction cost. A fraction near one indicates a dense support representation; it does not by itself identify a poor kernel or an excessive penalty. Inspect validation performance alongside the count. For regression, validation RMSE and MAE measure prediction error, while the support count describes the representation's sparsity.
Persist the complete prediction procedure: preprocessing, kernel choice and parameters, fitted coefficients and bias, support inputs for a nonlinear kernel expansion, and the class-label mapping for classification. A nonlinear kernel model typically evaluates a sum over its support vectors, so prediction cost grows with their number and the cost of each kernel evaluation. A linear model can instead store the combined vector and predict without visiting the support set. Monitor input and score distributions and assess whether changes warrant retraining; batch SVC and SVR fits do not update themselves. If the downstream system uses calibrated probabilities, persist the fitted calibration procedure as well and reassess calibration on fresh data.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!