Artificial Intelligence and Machine Learning · Lecture 14 of 17

Classification · Part 4

Support Vector Machines

Recap how classifiers make predictions, then use perceptron geometry to understand the widest separating margin and a linear SVM.

Recap · Classification Parts 1–3
6.3 · Support Vector Machines
Recap · Classification Parts 1–3

How have we predicted a class so far?

Different methods use the same input features to choose a class.

Part 1 · Probability models

Estimated probabilities for two classes, 0.2 and 0.8Class 0Class 10.20.8Choose the more probable class

Naive Bayes and logistic regression estimate class probabilities from the input features.

Part 2 · Nearest neighbors

A new point surrounded by two orange and one blue neighborNearby labels vote

KNN looks at nearby training examples and uses their labels to predict.

Part 3 · Perceptron

A straight line separates two prediction regionsA weighted score chooses a side

Learn weights and a bias, then classify from the sign of their weighted score.

Input features = measurements available for predictionPredicted class = the category returned by the modelTraining labels = known classes used to learn the rule
Recap · Classification Parts 1–3

Probabilities give a label and a degree of confidence

Part 1 used Naive Bayes and logistic regression to estimate how likely each class is for the given input.

Almost a tie

Estimated probabilities for two classesClass 00.45Class 10.55

Choose class 1, with estimated probability 0.55. The model assigns substantial probability to either class.

A clearer preference

Estimated probabilities for two classesClass 00.05Class 10.95

Choose class 1, with estimated probability 0.95. The model is more confident in this prediction.

Both examples predict class 1Parameters = quantities learned from the training dataMaximum likelihood chooses parameters that make the observed labels likely
Recap · Classification Parts 1–3

KNN predicts from similar examples

Part 2 kept the labeled training data. At prediction time, KNN finds the K nearest examples and uses their labels.

Find the nearest labeled examples

Five nearest neighbors contain three class-1 and two class-0 examplesNew inputFeature 1Feature 2

With K = 5, count the votes

Three neighbors have class 1.
Two neighbors have class 0.

Predict class 1.
The local class-1 fraction is 3/5 = 0.6.

K = number of neighbors used in the predictionOrange = class 1, blue = class 0, purple = new inputDistances depend on the feature measurements and their scaling
Recap · Classification Parts 1–3

Perceptron learning changes the rule

Keep the observation’s input fixed during an update. Use its prediction error to change the weights.

Read one example

Its feature values are the input. Its known class is the target.

Predict and compare

Compute the current score, predict a class, then compare it with the target.

Correct the rule

A mistake changes the weights and bias. Revisit the examples until a whole pass makes no mistakes.

wj(t+1)=wj(t)+η(yi−y^i)xij
i = observation index, j = input indexyᵢ = true class, ŷᵢ = predicted classη > 0 = learning ratewⱼ⁽ᵗ⁾ and wⱼ⁽ᵗ⁺¹⁾ = weight before and after the update
t = current training step
xᵢⱼ = input j of observation iFor the bias, j = 0 and xᵢ₀ = 1
Recap · Classification Parts 1–3

Two feature values locate one point

The horizontal coordinate is feature 1. The vertical coordinate is feature 2.

x = (x₁, x₂) = input vector for one observationx₁ = horizontal coordinate, x₂ = vertical coordinateFor training observation i, these are xᵢ₁ and xᵢ₂
Recap · Classification Parts 1–3

Feature weights set the boundary’s orientation

The boundary contains every point satisfying w1x1+w2x2+w0=0.

w = (w₁, w₂) = feature-weight vectorThe normal is perpendicular to the linew₀ = 0 in this plot, so the line passes through the origin
Recap · Classification Parts 1–3

The bias moves the line parallel to itself

Keep the feature weights fixed and change w₀. Watch the intercepts and the two prediction regions.

w₁ = w₂ = 1 throughout this plotw₀ = additive bias, also called the intercept termThe feature-weight vector w excludes the bias
Recap · Classification Parts 1–3

The sign of the score chooses a side

Use s=w1x1+w2x2+w0. Predict 1 when s ≥ 0 and 0 when s < 0.

s = weighted score for the current inputBlue region = predict 0, orange region = predict 1The score-zero line belongs to class 1 under our perceptron rule
Recap · Classification Parts 1–3

Turn the score into a distance

The signed perpendicular distance is dsigned=s‖w‖=sw12+w22.

Why set the score to 1? For a hard-margin SVM, scale both weights and the bias so the closest training point has score magnitude 1 (+1 on the positive side, −1 on the negative side). Try it on the purple point.
‖w‖ > 0. The bias is excluded from this length.The sign identifies the side of the lineThe shortest distance is the absolute value of dsigned.
6.3 · Support Vector Machines

How should we choose a separating line?

For the noisy AND problem, several lines classify every training point correctly. A linear Support Vector Machine (lSVM) chooses a separator by maximizing its distance from the nearest observations of both classes.

Compare separators

First inspect two lines with the same training accuracy.

Measure the margin

Use the weight vector to measure the gap to the nearest points.

Learn the separator

Maximize that gap while respecting the class constraints.

6.3 · Support Vector Machines

Two separators get every training label right

Compare the two lines and read the nearest-point clearance for each. A gold ring marks the selected point.

6.3 · Support Vector Machines

The separator is the zero-score hyperplane

The score is the weighted sum of the input’s feature values plus the bias.

s(x)=∑j=0mwjxj=𝐰⊤x+w0h:s(x)=0

The sign tells us which side of the separator the point is on. The separator h contains exactly the points where s(x) = 0.

Orientation

The vector 𝐰 is perpendicular to the separator. Moving along the separator leaves the score unchanged.

Position

The bias w₀ moves the separator without changing its orientation.

s(x) = score for input x𝐰 = feature weights, w₀ = biasx = input vector, x₀ = 1j = feature index (j = 0 is the bias term)m = number of input features
6.3 · Support Vector Machines

Read the separator, margin edges, and normal

The solid line is the separator. The dashed lines bound the margin and the arrow is perpendicular to them. A gold ring marks the selected point.

6.3 · Support Vector Machines

Convert a raw score into a geometric distance

Signed distance=𝐰⊤x+w0‖𝐰‖
Distance=|𝐰⊤x+w0|‖𝐰‖
x = point whose distance we measure𝐰 = feature weights, w₀ = bias‖𝐰‖ = length of the nonzero feature-weight vector
6.3 · Support Vector Machines

Why are the margin edges at scores −1 and +1?

The separator stays at score 0. Scale the weights and bias together so the closest training point has a score of −1 or +1. Then draw a parallel line at each of those scores.

Score −1Score 0Score +1Negative edgeSeparatorPositive edgex₁ = 1.5x₁ = 2x₁ = 2.5Full margin width = 1 plot unit
Example score s(x) = 2x₁ − 4

Scores at the two edges

Each line joins points with the same score.

∑j=0mwjxj=−1∑j=0mwjxj=+1

Distance across the strip

Full width=2‖𝐰‖

Each edge is 1/‖𝐰‖ plot units from the separator.

Class −1 stays at or beyond the blue edge

∑j=0mwjxij≤−1

Class +1 stays at or beyond the orange edge

∑j=0mwjxij≥+1

𝐰 = feature weights, w₀ = biasx = general input, xᵢ = training inputx₀ = xᵢ₀ = 1j = feature index, m = number of features
6.3 · Support Vector Machines

See what scores −1 and +1 mean on the plot

Move the solid line. When it separates both classes, the dashed lines mark scores −1 and +1. Select Fit max-margin to see the widest strip.

6.3 · Support Vector Machines

Why does one inequality work for both classes?

Negative class · yᵢ = −1

s(xi)≤−1

Positive class · yᵢ = +1

s(xi)≥+1

Multiply each score by its true class label to merge the two inequalities into one.

yis(xi)=yi(∑j=0mwjxij)≥1(i=1,…,n)
yᵢ = true class label of training point is(xᵢ) = score for training point in = number of training points
6.3 · Support Vector Machines

Choose the widest separating margin

Combine the two class constraints

yi(∑j=0mwjxij)≥1(i=1,…,n)

For yᵢ = +1, the score must reach +1. For yᵢ = −1, it must be at most −1.

Maximize the full width

(𝐰^,w^0)=arg max(𝐰,w0)∈ℝm+12‖𝐰‖
subject toyi(∑j=0mwjxij)≥1(i=1,…,n)

A wider empty strip puts the separator farther from the nearest training points on each side.

Rewrite as a minimization

(𝐰^,w^0)=arg min(𝐰,w0)∈ℝm+112‖𝐰‖2
subject toyi(∑j=0mwjxij)≥1(i=1,…,n)

A smaller nonzero weight norm gives a wider margin. Squaring and multiplying by ½ keeps the minimizing coefficients the same.

𝐰 = feature weights, w₀ = biasyᵢ = signed class (−1 or +1)n = number of training observationsxᵢⱼ = training feature j of case i, xᵢ₀ = 1
6.3 · Support Vector Machines

From a point to an SVM prediction

First compute the fitted score for the input. Its sign tells you which class to predict.

y^i=sgn(∑j=0mw^jxij)={+1ifs(xi)>0−1ifs(xi)<0

Try a point. Fix the fitted weights at ŵ₁ = 2 and ŵ₂ = 1, with bias ŵ₀ = −2.

1. Compute the score

2. Take its sign

3. Predict a label

ŵⱼ = fitted weight (j = 0 is the bias)xᵢⱼ = input feature j, xᵢ₀ = 1s(xᵢ) = fitted score for input isgn = sign functionŷᵢ = predicted signed class
6.3 · Support Vector Machines

Which points are the support vectors?

At the hard-margin SVM fit, the closest training points touch the dashed edges. Support vectors are the points on those edges that determine the fitted separator.

yi(𝐰⊤xi+w0)=1

On the edge

With the score rule s(x) = 2x₁ − 4, a blue point at x₁ = 1.5 scores −1. Its true label is −1, so (−1) × (−1) = 1. It touches the blue edge.

Beyond the edge

A blue point at x₁ = 1 scores −2, so (−1) × (−2) = 2. It is farther from the separator and does not limit the width.

In the dual formulation, a hard-margin support vector has a positive multiplier aᵢ and lies on its class’s edge. A point beyond the edge has aᵢ = 0.

𝐰 = feature weights, w₀ = biasyᵢ = signed class (−1 or +1)xᵢ = training point, aᵢ = its multiplier in the dual
6.3 · Support Vector Machines

Soft-margin SVM allows margin violations

Hard margin asks every point to stay on or beyond its own margin edge. If the classes overlap, that may be impossible. A soft-margin SVM allows points inside the margin or on the wrong side, and charges for those violations.

minimize𝐰,w0,ξ12‖𝐰‖2+C∑i=1nξi
subject toyi(𝐰⊤xi+w0)≥1−ξi,ξi≥0

What slack measures

In the hard-margin rule the signed score must reach 1. A point with signed score 0.6 falls short by 0.4, so its smallest allowed slack is 0.4.

What C controls

The first term favors a wider margin. The second charges C for each unit of slack. A larger C puts more weight on avoiding violations.

𝐰 = feature weights, w₀ = biasyᵢ = signed class (−1 or +1)ξᵢ = nonnegative slack for point iC > 0 = penalty per unit of slackn = number of training points
Further reading

Bishop on classification

Read chapters 4 and 7 for linear discriminants, perceptron learning, and support vector machines.

Lecture recap

From a separating line to a maximum-margin SVM

Geometry

The separator has score 0. Its margin edges have scores −1 and +1.

Hard-margin SVM

Choose the widest strip with every training point on or beyond its class’s edge. Support vectors anchor the strip.

Soft-margin SVM

Allow violations and use C to set their cost.

Next, Nonlinear SVM asks how changing the features can make a curved class boundary possible.
Continue learning
AIML · Lecture 14 · Classification · Part 4
Course home