Unit 6 of 8 · about 4 hours · derive

Support vector machines: margins, hinge loss and slack

The margin turns classification into geometry with a price list: violations cost , dimensions cost kernels, probabilities cost calibration. Every later kernel and calibration discussion assumes this pricing.

You'll be able to

  • Derive the hard-margin width , the hinge loss and the slack budget, and connect to margin width and support-vector count.
  • Classify any training point as support vector or not from its margin position and read dual coefficients on a toy.
  • Defend scaling, choice and LinearSVC-versus-SVC selection from measurements, and state what SVC probabilities mean in sklearn 1.9.

Covers

Hard and soft margins, hinge loss, slack and support vectors
1

Read

In this order, only the parts listed

0 of 6 done

  1. ISLP Chapter 9: maximal margin and support vector classifiersISLP - tree-based methods and support vector machines

    Sections 9.1 and 9.2 (pp. 367 to 376): hyperplanes, the maximal margin classifier, the non-separable case, and the SVC optimization with , slack variables and support vectors

  2. ISLP Chapter 9: multiclass extensions and the hinge lossISLP - tree-based methods and support vector machines

    Sections 9.4 and 9.5 (pp. 383 to 386): one-versus-one against one-versus-all, and the Loss plus Penalty form with hinge versus logistic loss

  3. HoML Appendix C: linear and soft-margin SVMsHands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical models

    Linear SVM Classification, Soft Margin Classification, Under the hood with the hinge plots, and the LinearSVC versus SVC versus SGDClassifier exercise

  4. sklearn SVM: classification, imbalance and probabilitiesscikit-learn - neighbors, SVMs, trees and ensembles

    1.4.1 Classification with 1.4.1.2 Scores and probabilities and 1.4.1.3 Unbalanced problems

    The probability=True path is deprecated in sklearn 1.9 and warns with FutureWarning toward removal in 1.11. A default SVC has no predict_proba; use CalibratedClassifierCV with SVC and ensemble False for probabilities.

  5. sklearn SVM: tips, complexity and mathscikit-learn - neighbors, SVMs, trees and ensembles

    1.4.5 Tips on practical use (scale features, C default 1.0, cache_size) with 1.4.4 Complexity and 1.4.7 Mathematical formulation

  6. ISLP lab: support vector classifier cost demoISLP - tree-based methods and support vector machines

    Support Vector Classifier: 10 versus 0.1 on the 2D example with wider margin and more support vectors at small , then grid search selecting 1 at 0.74 CV accuracy

2

Focus on

  • The gutter constraints fix the margin width at . The toy gives , , width 2.0, support vectors at and with dual coefficients -0.5 and 0.5. Only points on or inside the margin enter the solution.
  • is the violation budget. On negatives at -2, -1 and 2.4 and positives at 0.5, 1 and 2, 0.05 gives width 9.756 with all 6 points as support vectors, while 100 gives width 2.0 with 4. Train accuracy is 5 of 6 at both, because the point at 2.4 breaks linear separability.
  • Hinge loss is exactly 0 past the margin, so safe points vanish from the fit. Logistic loss never reaches 0, so every point keeps pulling, which is why only support vectors matter for the SVC.
  • Scale before fitting. With features on and and the label , a default RBF SVC scores 0.75 unscaled against 0.97 standardized (200 train and 100 test points, seed 2). SVC defaults to RBF with gamma scale and 1.0; LinearSVC defaults to squared_hinge and exposes no support vectors.
Common mistakes · 3
  • Promising SVC probabilities from older tutorials now warns in sklearn 1.9 and disappears in 1.11. Never ship predict_proba without the calibrator wrapper.
  • Reusing a value between SVC and LinearSVC. LinearSVC uses squared hinge and, through liblinear, also regularizes the intercept, so the same gives a different model; compare decisions, not parameter values.
  • Skipping scaling is the classic SVM bug. and the margin both assume comparable units, and the mixed-scale example loses 22 points of accuracy without it.
3

Try it yourself

Closed-book, before checking anything

Derive the width from the plus-minus-1 gutter constraints. Solve with by hand to , with support vectors at -1 and 1, and confirm with SVC and kernel linear at large .
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.For and , what is the margin width and which toy points are support vectors?Answer

    Width 2.0 with support vectors at and , carrying dual coefficients -0.5 and 0.5.

  2. 2. 0.05 gives width 9.756 with 6 support vectors while 100 gives width 2.0 with 4. Explain both changes.Answer

    Small tolerates violations, so the margin grows wide and every point sits on or inside it. Large punishes violations, so the margin narrows onto 4 points.

  3. 3.A 4-class problem goes to SVC. How many internal classifiers fit, and what does decision_function_shape ovr change?Answer

    Six one-versus-one pairs fit inside libsvm. The ovr shape only reshapes the output scores, not the fits.

  4. 4.A far-correct point contributes what to hinge versus logistic loss, and what follows for sparsity?Answer

    Exactly 0 to hinge and a small positive amount to logistic. Hinge lets safe points drop out of the solution, leaving support vectors only.

5

Practice

  • Deep-ML 21: Pegasos Kernel SVM Implementation

    Implement deterministic pegasos_kernel_svm; the visible tests check ([2.0, 2.0, 6.0, 1.0], 36.2027) for linear and ([1.0, 1.0, 1.0, 1.0], 0.0) for RBF with sigma 0.5

    Labels are plus-minus 1. The update rule is in the problem's learn section: follow it literally, updating and in place sample by sample within each iteration (a batch update fails test one), with the RBF kernel . Note that is 1 at , so that step resets every .

You're done when

You derive the width, reproduce the toy coefficients, predict the effect before running, and problem 21 passes.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 5 more in the section
  • Hands-On Machine Learning with Scikit-Learn and PyTorch - nonlinear classical modelsAurelien Geron (O'Reilly, 2025) · primary

    Read Chapters 3, 5 and 6 and the online Appendix C notebook as the first pass through performance measures, trees, ensembles and SVMs, running notebooks 03, 05 and 06 alongside. Vary depth, estimators, learning rate, k, C and RBF width under fixed splits.

  • scikit-learn - neighbors, SVMs, trees and ensemblesscikit-learn · reference

    Check official estimator constraints, complexity, preprocessing needs, missing-value behavior and probability caveats. Do not use library calls as the mechanism implementation.

  • ISLP - tree-based methods and support vector machinesJames, Witten, Hastie, Tibshirani and Taylor · required

    Use Chapters 8 and 9 with their Python labs for the statistical account of trees, bagging, random forests, boosting and SVMs that the notebooks leave implicit, and for model-choice and feature-importance arguments.

Also in this section

  • Deep-ML - tree, ensemble and kernel problemsDeep-ML · support

    Use 138 Best Gini-Based Split, 20 Decision Tree Learning, 307 Bagging Classifier, 21 Pegasos Kernel SVM and 280 RBF Kernel as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including tie and update-order conventions.

  • Probabilistic Machine Learning, Chapter 16: exemplar-based methodsKevin Murphy · support

    Read Sections 16.1 to 16.1.3 for the KNN decision rule, the curse of dimensionality with the growing-hypercube example, and the speed section on k-d trees, hashing and approximate search.

  • XGBoost parameters and sklearn estimator interfaceXGBoost developers · reference

    Read the tree-booster parameter list for eta, depth, sampling and regularization names and defaults, and the sklearn page for early stopping with eval_set.

  • LightGBM parametersLightGBM developers · reference

    Read learning_rate, num_leaves, max_depth, min_data_in_leaf, feature_fraction, bagging_fraction with bagging_freq, lambda_l2 and early stopping defaults.

  • CatBoost common parameters and categorical featuresCatBoost developers · reference

    Read iterations, learning_rate, l2_leaf_reg, depth, grow_policy, subsample, rsm, boosting_type, use_best_model and one_hot_max_size defaults, plus native categorical handling.