Classification · Part 4
Recap how classifiers make predictions, then use perceptron geometry to understand the widest separating margin and a linear SVM.
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
Naive Bayes and logistic regression estimate class probabilities from the input features.
Part 2 · Nearest neighbors
KNN looks at nearby training examples and uses their labels to predict.
Part 3 · Perceptron
Learn weights and a bias, then classify from the sign of their weighted score.
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
Choose class 1, with estimated probability 0.55. The model assigns substantial probability to either class.
A clearer preference
Choose class 1, with estimated probability 0.95. The model is more confident in this prediction.
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
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.
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.
t = current training stepxᵢⱼ = 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.
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 .
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.
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 . Predict 1 when s ≥ 0 and 0 when s < 0.
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 .
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.
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.
Static preview. Open handout.html to use the interactive example.