Support Vector Machine Explained With a Worked Example

Two groups of wooden disks separated by a diagonal line and dotted margin guides.

Suppose a factory wants to classify two materials from their sensor readings. Several lines might separate the examples on a plot. A support vector machine asks which line leaves the widest gap between the closest examples from opposite classes.

A support vector machine (SVM) classifier learns a decision boundary with a margin. A linear SVM draws a line in two dimensions or a hyperplane in more dimensions. A kernel SVM can produce a curved boundary in the original feature space.

Find the widest gap by hand

Start with one feature and four labeled readings. Class A has readings of 1 and 2. Class B has readings of 6 and 7. Any cutoff strictly between 2 and 6 separates these training examples. A cutoff of 3 leaves one unit of room on the A side and three on the B side. Moving the cutoff to 4 leaves two units on each side.

The widest margin separator is at 4. The observations at 2 and 6 sit at the margin edges, so they are the support vectors in this example. If the reading at 2 stays fixed, moving the reading at 1 farther away leaves this boundary unchanged. Moving the nearest B point from 6 to 5 shifts the midpoint to 3.5.

Scatter plot with a separating line, two dashed margin edges, and three circled support vectors.
Maximum-margin separator on the Iris dataset. scikit-learn developers

In higher dimensions the same idea uses perpendicular distance to the boundary. A signed decision score tells you which side a new example falls on. The score can rank examples by how far they fall toward one class, but it needs a calibration step if you plan to interpret it as a probability.

Let the model trade off margin width and violations

Real datasets often overlap. One example can land on the wrong side of the boundary, or on the correct side but inside the margin. A soft-margin SVM allows these violations and penalizes them during fitting. This gives the model a way to balance margin width with the cost of fitting training points closely.

The setting C controls the penalty. A smaller C allows more violations in exchange for stronger regularization. A larger C penalizes violations more heavily. If a sensor occasionally produces an unusual value, a large penalty can make the boundary change to accommodate that one reading. Compare several settings on validation data before choosing.

Choose a linear or nonlinear boundary

Start with a linear model if features already separate the classes reasonably well, or if you need quick training on a large or sparse dataset. A linear classifier is easier to inspect because its weights show how inputs contribute to the decision score. Scaling matters when the feature units differ, since the geometry of the margin depends on the representation.

An RBF kernel lets the boundary bend around groups of examples. It measures how similar pairs of observations are, with similarity declining as the distance between them grows. Gamma controls the rate of that decline. Lower gamma lets examples influence a larger neighborhood. Higher gamma concentrates influence near individual examples and can produce a more intricate boundary.

Nine RBF decision boundary plots comparing three values of C and three values of gamma.
How C and gamma change the shape of an RBF decision boundary. scikit-learn developers

Tune C and gamma together because both affect the fit. If a high-gamma setting wins on training data but loses on validation data, the boundary may be following local quirks. Try smoother settings and inspect where the errors occur. Scikit-learn example of RBF parameter effects.

A workflow for testing your first SVM

Choose a split that reflects future use. If several rows come from the same physical device and the model will be used on new devices, hold out whole devices. Fit preprocessing inside each training fold and apply it to that fold’s validation rows. Unlimited AI guide to choosing a cross-validation split.

Compare a scaled linear SVM with a simple baseline, then add an RBF candidate if the validation errors suggest a nonlinear pattern. Use the same splits and metrics across candidates. Record training time as well as accuracy because kernel fitting can become costly on large datasets.

Report the confusion matrix at the chosen operating rule and inspect examples near the boundary. If the application needs a probability estimate, validate calibration as a separate property. Unlimited AI guide to reading a confusion matrix.

Common questions

Can an SVM handle more than two classes?

Yes. A multiclass system can combine several binary classifiers. For example, a one-versus-one strategy fits a classifier between each pair of classes. See the implementation guide for the strategy used by your estimator. Scikit-learn support vector machines guide.

How does a linear SVM differ from logistic regression?

Both can produce a linear boundary, but they optimize different losses. Logistic regression estimates class probabilities through its link function. The SVM prioritizes a margin and penalizes violations, and its decision scores have a different interpretation. Compare both on your task when you need a strong baseline. Unlimited AI worked example of logistic regression.

Before tuning, record the metric you want to improve and the cost of each type of mistake. A simpler boundary that performs similarly on unseen examples can save training time and make errors easier to examine.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top