AIML · Lecture 14 · Classification · Part 4Course homeOpen slide deck  
Artificial Intelligence and Machine Learning · Lecture 14 of 17

Classification · Part 4

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

23 reading sections · 10 interactive examples · Lecture 14 of 17. Use the contents to navigate. Printing retains the worked steps and lab previews.

Recap · Classification Parts 1–3How 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

Part 1 introduced the optimal Bayes rule, which chooses the class with greatest true conditional probability. In practice Naive Bayes and logistic regression estimate probabilities from data. Their parameters are fitted from labeled examples, including maximum likelihood estimation. Part 2 introduced KNN, a nonparametric rule that keeps training examples and counts nearby labels. Part 3 introduced the perceptron. Its two-feature rule draws a straight boundary in the input plane.

Open the corresponding slide ↗

Recap · Classification Parts 1–3Probabilities 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

The optimal Bayes classifier would choose the class with greatest true conditional probability. Naive Bayes and logistic regression fit probability models from labeled data. Naive Bayes combines a class prior with class-conditional feature likelihoods, using its conditional-independence assumption. Logistic regression models the class probability directly through a sigmoid of a weighted score. Maximum likelihood fits model parameters by favoring the observed labels. Estimated class probabilities express the fitted model’s preference among the possible labels.

Open the corresponding slide ↗

Recap · Classification Parts 1–3KNN 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

For classification, KNN predicts the most frequent class among the selected neighbors. In this illustration, three of five labels are 1, giving the local estimate 3/5 = 0.6 and a class-1 prediction. For a numerical regression target, KNN averages the neighbor targets. The choice of K affects how local the rule is and is selected using validation data. Feature scaling changes distances and can therefore change which examples are nearest. This makes the geometry of the input space important even before we introduce the perceptron boundary.

Open the corresponding slide ↗

Recap · Classification Parts 1–3Perceptron 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

For a correct prediction the error is zero, so the update keeps the weights. For a missed class 1 the error is positive and the weighted score of that example increases. For a false class 1 the score decreases. One correction may need to be followed by more corrections. A complete pass without mistakes establishes convergence on the training examples. The finite-update guarantee requires linearly separable training classes.

Open the corresponding slide ↗

Recap · Classification Parts 1–3Two 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ᵢ₂

A row of the data table becomes one point in the two-dimensional feature plane. A feature is a measurement associated with the example. Its value is the coordinate along that feature’s axis. The arrow from the origin to the point is another way to draw the same vector. Moving an input slider changes the example we are asking the model to classify.

Open the corresponding slide ↗

Recap · Classification Parts 1–3Feature 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

A point on the boundary satisfies w₁x₁ + w₂x₂ + w₀ = 0. The vector w = (w₁,w₂) is normal, or perpendicular, to that line. Its direction points toward increasing score. The line’s direction is perpendicular to the weight direction. When w₂ is nonzero, rearranging gives x₂ = −(w₁/w₂)x₁ − w₀/w₂. The ratio of the feature weights therefore determines the slope. When w₂ = 0 and w₁ is nonzero, the boundary is vertical. When w₁ = 0 and w₂ is nonzero, it is horizontal. Multiplying every coefficient, including the bias, by one positive number preserves the predictions and boundary. If both feature weights are zero, the score is constant and there is no separating line.

Open the corresponding slide ↗

Recap · Classification Parts 1–3The 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

With weights (1,1), the boundary equation becomes x₁ + x₂ = −w₀. The bias changes the right-hand side while the normal direction stays fixed. Thus the boundary shifts without rotating. At w₀ = 0 it passes through the origin. For example, w₀ = −2 gives x₁ + x₂ = 2, with intercepts (2,0) and (0,2). Raising the bias raises every score by the same amount, expanding the region predicted as class 1. In the following SVM geometry, the notation w includes only the feature weights, with w₀ written separately.

Open the corresponding slide ↗

Recap · Classification Parts 1–3The 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

For the fixed rule s = x₁ + x₂ − 2, (0,0) has score −2 and predicts class 0. The point (2,1) has score +1 and predicts class 1. The point (1,1) has score 0 and is on the boundary. Our perceptron’s greater-than-or-equal rule assigns that tie to class 1. The score grows when moving in the direction of the feature-weight vector. Its sign identifies a prediction region, while its scale depends on the chosen coefficients.

Open the corresponding slide ↗

Recap · Classification Parts 1–3Turn 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.

For w = (2,1), w₀ = −2 and x = (2,2), the score is 2×2 + 1×2 − 2 = 4. The normal length is √5, so the signed distance is 4/√5, approximately 1.789. The projection onto the line is (0.4,1.2), where 2×0.4 + 1.2 − 2 = 0. The white segment from the input to that projection is perpendicular to the boundary and is the shortest path to it. A positive signed distance lies on the class-1 side and a negative signed distance lies on the class-0 side. The absolute distance is nonnegative. With the starting point, the button multiplies both weights and the bias by 1/4, changing its score from +4 to +1. The dashed segment still measures about 1.789 units because the score and weight vector length shrink by the same factor. Euclidean distance is measured in the plotted feature units, so feature scaling matters. This normalized distance prepares the maximum-margin SVM criterion.

Open the corresponding slide ↗

6.3 · Support Vector MachinesHow 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.

Open the corresponding slide ↗

6.3 · Support Vector MachinesTwo separators get every training label right

Compare the two lines and read the nearest-point clearance for each.

Both separators classify the 360 noisy AND training points correctly. Separator 2 passes much closer to the nearest points. Small changes in measured inputs can therefore cross that line sooner. A linear SVM selects a separator by maximizing clearance from the nearest points, giving a centered margin in the separable case.

Interactive example · Two separators get every training label right
Initial state of the interactive example: Equal training accuracy can hide different geometry
Change the controls and inspect the result. The example pauses when you move away.
"
Open the corresponding slide ↗

6.3 · Support Vector MachinesThe 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

In two dimensions the hyperplane is a line. For the SVM, 𝐰 contains only the feature weights. The separate bias w₀ is included in the indexed sum by setting x₀ = 1. Among separating lines we will choose the one with the widest margin.

Open the corresponding slide ↗

6.3 · Support Vector MachinesRead 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.

The shaded strip is the margin. The separator runs halfway between its parallel edges. The normal vector is perpendicular to all three lines. The training points touching the dashed edges are the points that delimit the strip.

Interactive example · Read the separator, margin edges, and normal
Initial state of the interactive example: Inspect the source’s margin construction
Change the controls and inspect the result. The example pauses when you move away.
"
Open the corresponding slide ↗

6.3 · Support Vector MachinesConvert 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

The signed distance measures motion along the unit normal 𝐰/‖𝐰‖ from the separator to x. The sign says which side x lies on. Multiplying all coefficients by a positive number leaves the line and the distances unchanged, so we can choose a convenient score scale.

Open the corresponding slide ↗

6.3 · Support Vector MachinesWhy 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

The dashed planes are parallel to the separator at score zero. The +1 and −1 on those lines are scores. The class labels also use +1 and −1. A point of class +1 can have a score larger than +1, and a point of class −1 can have a score smaller than −1. Each is 1/‖𝐰‖ away from the separator, giving a full width of 2/‖𝐰‖. A training point that satisfies its inequality with equality lies on its class’s margin plane.

Open the corresponding slide ↗

6.3 · Support Vector MachinesSee 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.

The angle sets the direction of the unit normal u. The offset w₀ moves the line parallel to itself. For a separating line, divide the raw score by the distance from the center line to the closest training point. That sets the closest point’s score to −1 or +1 while keeping the line fixed. The dashed lines show where this rescaled score reaches −1 and +1. A negative or zero minimum signed clearance means the plot does not show a hard-margin strip. Fit max-margin restores the widest separating strip for these ten points.

Interactive example · See what scores −1 and +1 mean on the plot
Initial state of the interactive example: Rotate a separator and measure its clearance
Change the controls and inspect the result. The example pauses when you move away.
"
Open the corresponding slide ↗

6.3 · Support Vector MachinesWhy 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
Open the corresponding slide ↗

6.3 · Support Vector MachinesChoose 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

Relabel the two classes as −1 and +1 so one inequality handles both classes. A negative label reverses the score inequality. Among all feasible separators, maximizing the full width is equivalent to minimizing half the squared feature-weight norm. This objective is convenient to differentiate. The hard-margin formulation assumes separable data.

Open the corresponding slide ↗

6.3 · Support Vector MachinesFrom 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
Open the corresponding slide ↗

6.3 · Support Vector MachinesWhich 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

The optimization is anchored by training points at the margin. The dual derivation later shows how the fitted weights are built from points with positive multipliers. In degenerate solutions a point can lie on a margin plane with a zero multiplier.

Open the corresponding slide ↗

6.3 · Support Vector MachinesSoft-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

A soft margin is a practical extension. Slack lets some observations enter or cross the margin. A larger C charges more for a given total violation. A smaller C gives more weight to keeping the norm small. Choose C using validation.

Open the corresponding slide ↗

Further readingBishop on classification

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

Open the corresponding slide ↗

Lecture recapFrom 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.
Open the corresponding slide ↗
Continue learning