Sub topic 3
Scaling and Using SVMs in Practice
Assume you know features, labels, train/test splits, cross-validation, and basic vectors. SVMs are especially sensitive to feature scale because distances and dot products affect their margins and kernels.
Scaling features before fitting
Scale numeric features inside a pipeline. Standardization subtracts each feature's training mean and divides by its training standard deviation. Without scaling, a feature measured in thousands can dominate a feature measured between 0 and 1.
Tuning C and gamma with grid search
For an RBF SVM, C controls the penalty for margin violations. Larger values fit training data more strictly. gamma controls how local each example's influence is. Larger values create more detailed boundaries.
from sklearn.datasets import make_moons
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = make_moons(n_samples=400, noise=0.25, random_state=0)
model = make_pipeline(StandardScaler(), SVC(kernel="rbf"))
grid = GridSearchCV(model, {
"svc__C": [0.1, 1, 10, 100],
"svc__gamma": [0.01, 0.1, 1, 10],
}, cv=5)
grid.fit(X, y)
print(grid.best_params_)| Parameter | Smaller value | Larger value |
|---|---|---|
| `C` | Wider margin, more violations | Fewer violations, possible overfitting |
| `gamma` | Smoother boundary | More local, detailed boundary |
Extending SVMs to more than two classes
SVC handles multiclass classification by default with one-versus-one classifiers. It trains one binary classifier for each pair of classes and combines their votes. One-versus-rest trains one classifier per class instead. Both strategies increase training work as the number of classes grows.
Decision scores and probability estimates
decision_function(X) returns signed scores. The sign gives the predicted side of the boundary, while the magnitude indicates distance from the boundary in the model's scoring space. These scores are not probabilities. Use probability=True when probability estimates are required, but expect extra calibration work during fitting.
Support vector regression
SVR predicts continuous values using an epsilon-insensitive tube. Errors inside the tube do not contribute to the loss. The parameters C, gamma, and epsilon control fit, locality, and tube width.
Training algorithms and data size
Kernel SVM training can become slow and memory-heavy as the number of examples grows because it uses pairwise similarities. Linear SVMs scale better for very large, sparse datasets. Kernel choice, feature count, and the number of support vectors also affect prediction cost.
When to use an SVM and when not to
Use an SVM when the dataset is small or medium-sized, features are meaningful after scaling, and a strong linear or nonlinear boundary is useful. Prefer other models when training data is huge, probabilities are central, or extensive categorical and missing-value handling is needed.
Exercise
Why should a scaler be placed inside the cross-validation pipeline?
Show answer
So each validation fold is transformed using statistics learned only from its corresponding training fold.