⚡ SVM from Scratch

These are Noah's notes on how to implement a linear support vector machine (SVM) from scratch, following the soft-margin formulation of Martha's notes. The notebook starts with the example of the book and then returns to the Physiotherapy no-shows.

The interactive notebook includes:

  • The 14 points of the book's linear SVM figure, and the conditions that define the margin.
  • The hinge loss, compared with the log loss of logistic regression.
  • Training with gradient descent, which recovers the line of the book and its two support vectors.
  • Soft margin: how $\lambda$ trades margin width for training errors when a suspicious point appears.
  • k-fold cross-validation implemented by hand to choose $\lambda$ on the no-show data.
  • Evaluation on the test set, comparison with logistic regression, and the effect of moving the threshold on the SVM score.

Want to modify the code and experiment on your own?
Open the notebook in Google Colab

Bibliography