Logistic regression turns a learned score into a probability. Fisher LDA finds a projection that separates labelled groups. An SVM learns a boundary with a wide margin. K-means uses distances to discover groups when class labels are unavailable.
Start by drawing a linear boundary and marking the two sides. Then learn what each method optimises and work through one example. For CT2 Set B 2026, prioritise the eight-point K-means calculation. For the semester, practise the SVM formulation and the K-means and linkage numericals.
1. Linear classification and logistic regression
A linear classifier starts with a score . The features are , the weights are and the bias is . The boundary is a line in two dimensions, a plane in three, and a hyperplane in higher dimensions. The vector points perpendicular to it. The score’s sign tells which side a point is on. The bias shifts the boundary, and changing the direction of tilts it.
Linear regression uses a linear score to predict a continuous target such as a house price. Binary logistic regression maps that score to a probability and uses a classification loss.
The odds are . The log-odds are linear in the features, even though the probability is curved. At the usual threshold, is the boundary because . With a threshold , the boundary becomes . Changing the threshold trades off different error types without refitting the weights.
How logistic regression learns
Binary cross-entropy measures how much probability the model assigned to the actual label. Minimising it over the training examples is equivalent to maximising the likelihood of their labels in the unregularised model.
Differentiating cross-entropy through the sigmoid cancels its factor. When the label is 1 and is too small, . A gradient step raises the score on that example when its features are nonzero and the other parameters are held fixed.
Worked example — probability, loss, and the boundary
Let and . For the score is , giving . At threshold , predict class 1. The boundary is the line .
- If the actual label is 1, the loss is .
- If the actual label is 0, the loss is . The model was confident in the wrong class.
- With the second label, the weight gradient is . Subtracting a small multiple of it lowers the score for .
For mutually exclusive classes, multinomial logistic regression scores each class and uses softmax.
2. Linear discriminant analysis
Fisher’s idea
Fisher LDA projects labelled data onto a line. A good projection places the two class means far apart relative to the spread within each class. If the points vary greatly along a direction, a difference between the two means along that direction may still give poor separation.
For class , let be its mean and its scatter matrix. Each outer product records how a point’s feature deviations vary together. For two classes and , let and . The projected mean gap is and the total projected within-class scatter is .
Scaling changes both squared terms by the same factor, so fix and maximise . Differentiating the Lagrangian gives . Since points along , the best separating direction is
assuming invertible and different means. With classes, the corresponding multiclass projection has at most discriminant directions. When the scatter is singular, a regularised estimate can make the calculation usable.
Worked example — find and use the Fisher direction
Take and .
- Find means. and , so .
- Find scatter. The deviations within are , and . Summing their outer products gives . Class is shifted by , so it has the same scatter.
- Find the direction. . Solving gives . Use the equivalent direction for simpler scores.
- Project and classify. With , the projected scores are and . The midpoint of the projected class means is . Under this midpoint rule, scores 6, so it is assigned to .
The midpoint is a classification choice made after projection. Class priors and the projected distributions matter when choosing a threshold.
Gaussian LDA as a classifier
Gaussian LDA models each class with its own mean and prior , and assumes a common covariance . By Bayes’ rule, compare the class scores
The quadratic term in is common to all classes and cancels in the comparison, producing linear boundaries. In the two-class case, the boundary has normal , related to the Fisher direction. A larger prior for one class changes the score offset and can shift the boundary toward the other class.
3. Support vector machines and the margin
A support vector machine (SVM) tries to separate labelled points while leaving a wide gap around the decision boundary. For a linear SVM, use and labels . Predict by the sign of .
The decision line is . Choose the scale of so the two margin planes are and . An example with lies on the correct margin or beyond it. The perpendicular distance from a point to the decision plane is . The distance from either margin plane to the middle is , so the full margin is .
PYQ — CT I 2022, Q6 · Hard-margin formulation (6 marks)
A support vector machine (SVM) finds a separating hyperplane with the maximum margin. For examples with , write the decision function as . The decision plane is . In canonical scaling, the two margin planes are and . Their perpendicular separation is , so maximising the margin is equivalent to minimising .
For linearly separable data the hard-margin problem is
Introduce a multiplier for each constraint. Setting the Lagrangian derivatives with respect to and to zero gives and . Therefore the dual is
After solving the dual quadratic programme, the points with are the support vectors. They lie on the margin and determine the classifier. Compute from the multiplier sum and obtain from a support vector. Classify a new point using the sign of .
For overlapping data, use nonnegative slack variables with constraints and add to the objective. This allows violations while still penalising them.
Worked example — find the widest separating strip
Label and negative and positive. The closest point on the negative-class line segment to is its midpoint . The direction joining those closest class-hull points is , so write . Set on the negative margin and on the positive margin. Subtracting gives , hence and .
The three lines are , , and . The middle line is the decision boundary. In a sketch, draw these as three parallel lines, put the two negatives on the line at 3 and the positive on the line at 8, and draw the margin width perpendicular to the lines. The full margin is .
Soft margin for overlapping data
A hard-margin solution requires perfect separation. The soft-margin formulation introduces slacks to allow margin violations.
At the optimum for fixed , . This is the hinge loss. A signed score of is on the correct side but inside the margin, so the slack is . A signed score of is on the wrong side and has slack . Larger places more weight on avoiding violations. Smaller allows more violations in exchange for a smaller weight norm.
Kernels for nonlinear boundaries
A feature map can make a curved boundary in the original space into a linear boundary in the mapped space. A kernel computes directly. Replace the dot products in the dual with kernel evaluations and predict using
For binary XOR, add the interaction feature . The score gives signs on . It is linear in the three mapped features and separates the XOR labels. Example kernels include the polynomial kernel and the RBF kernel with . Feature scaling affects dot products and distances. A kernel changes the feature geometry, and a soft margin allows violations. You can use them together.
4. K-means clustering
K-means partitions unlabelled points into groups. Each group is represented by a centroid, the coordinate-wise mean of its members. The algorithm seeks to reduce the sum of squared distances from points to their assigned centroids.
- Choose and initialise centroids. Use the starting points given in an exam question.
- Assign each point to its nearest current centroid. Squared Euclidean distances give the same ordering as Euclidean distances.
- Recompute each centroid as the mean of its newly assigned points.
- Repeat the assignment and update until assignments stabilise, or stop after the requested number of iterations.
Each assignment and update reduces or preserves the objective. Initial centres can change the result because the method can settle at a local optimum. If distances tie, state a consistent rule. If a cluster becomes empty, choose a replacement centre under a specified procedure.
PYQ — CT2 Set B 2026, Q2(b) · Eight points, two clusters (7 + 1 marks)
K-means assigns each point to its nearest centroid, then replaces each centroid with the mean of the points assigned to it. I take the initial centroids as and . Squared Euclidean distance gives the same assignments as Euclidean distance.
First assignment. For example, has squared distances and , so it joins cluster 1.
| Point | Cluster | ||
|---|---|---|---|
| 0 | 85 | 1 | |
| 1 | 72 | 1 | |
| 2 | 61 | 1 | |
| 4 | 61 | 1 | |
| 52 | 5 | 2 | |
| 74 | 1 | 2 | |
| 72 | 1 | 2 | |
| 85 | 0 | 2 |
Update the centroids. Taking coordinate-wise means gives
Second assignment. The squared distances to the updated centroids are as follows.
| Point | Cluster | ||
|---|---|---|---|
| 1.0625 | 69.8125 | 1 | |
| 0.0625 | 57.8125 | 1 | |
| 0.5625 | 48.3125 | 1 | |
| 1.0625 | 47.8125 | 1 | |
| 39.0625 | 1.8125 | 2 | |
| 58.5625 | 0.3125 | 2 | |
| 58.0625 | 0.8125 | 2 | |
| 69.0625 | 0.8125 | 2 |
No point changes cluster, so the centroids stay at and . The final partition is and . This is unsupervised learning because the algorithm discovers the groups from the coordinates without given class labels.
PYQ — 2021–22 semester, Q6 · Three clusters for two epochs (10 marks)
Let the initial centroids be , and , as specified. In each epoch, I assign all points to their nearest current centroid using squared Euclidean distance and then calculate the coordinate-wise mean for each cluster.
| Point | Epoch 1 squared distances to | Cluster | Epoch 2 squared distances to | Cluster |
|---|---|---|---|---|
| 1 | 1 | |||
| 3 | 3 | |||
| 2 | 2 | |||
| 2 | 2 | |||
| 2 | 2 | |||
| 2 | 2 | |||
| 3 | 3 | |||
| 2 | 1 |
After epoch 1, cluster 2 contains , so its mean is . The other two means are and . Epoch 2 uses those three means for the second assignment. In that assignment, moves from cluster 2 to cluster 1.
| Epoch | Cluster 1 and new centroid | Cluster 2 and new centroid | Cluster 3 and new centroid |
|---|---|---|---|
| 1 | , | , | , |
| 2 | , | , | , |
The final grid shows the eight points and the three second-epoch centroids. A cross marks each centroid.
5. Agglomerative clustering and dendrograms
Start with each point in its own cluster and repeatedly merge the pair of clusters with the smallest linkage distance. Single linkage uses the smallest distance between a pair of points across the clusters. Complete linkage uses the largest. Single linkage can join long chains through a sequence of close pairs. Complete linkage tends to form more compact clusters. A dendrogram displays the merge order and heights. Cutting across it at a chosen height gives a flat clustering.
PYQ — Semester paper printed 2023 · Single and complete linkage
Agglomerative clustering initially places each point in a separate cluster. At every step it merges the two clusters with the smallest linkage distance. Single linkage defines the distance between two clusters as the minimum Euclidean distance between their points. Complete linkage uses the maximum.
For example, the distance between and is . The initial distances are
| Point | ||||
|---|---|---|---|---|
| 1.0414 | 0.5930 | 0.4610 | 0.8184 | |
| 0.7737 | 0.6161 | 0.3239 | ||
| 0.3023 | 0.4522 | |||
| 0.3585 |
Both rules first join with at , then join with at . With single linkage, the nearest link between those two pairs is –, so they merge at . The nearest link from to that four-point cluster is – at .
With complete linkage, the distance from to is . Its distance to is , and the distance between the two pairs is . The smallest is . This produces . The final merge joins it to at the largest cross-cluster distance, .
6. K-nearest neighbours and decision trees
K-nearest neighbours (KNN)
KNN is supervised. At prediction time, calculate the distance from a query to the training points, take the nearest labelled points, and vote for their most frequent label. For labels apple, apple and orange among the three nearest points, predict apple. Distance and feature scaling matter. In KNN, counts nearby labelled examples. In K-means, counts clusters and the nearest-centroid rule is used to fit an unlabelled partition.
PYQ — Semester 2023, Q3 · KNN versus K-means (4 marks)
K-nearest neighbours (KNN) is a supervised algorithm. It stores labelled training examples. To classify a new point, it finds the closest training examples and predicts their majority class. Here is the number of neighbours. For example, two apple labels and one orange label among the three closest examples give an apple prediction.
K-means is an unsupervised clustering algorithm. It starts with centroids, assigns unlabelled points to the closest centroid, and repeatedly updates the centroids to the means of their assigned groups. Here is the number of clusters. For example, it can group customers by similar purchasing behaviour when customer categories are unavailable.
Decision trees and information gain
A classification tree makes feature tests at internal nodes and predicts a class at a leaf. ID3 chooses a split that maximises the reduction in label entropy. If is a child subset of , its contribution to the post-split entropy is weighted by its size.
For four training cases with labels “yes, yes, no, no”, the parent entropy is 1 bit. If a feature separates the two yes cases from the two no cases, both child entropies are 0. Its information gain is 1 bit. A tree repeats this calculation where further splits are needed. Depth limits or pruning help reduce overfitting.
7. Closed-book revision
| Task | What to reproduce without looking |
|---|---|
| Logistic regression | Score → sigmoid → threshold, then binary cross-entropy |
| Fisher LDA | Means → within-class scatter → projection → threshold |
| SVM | Draw , derive the margin and write both objectives |
| K-means | State the seeds, calculate distances, assign, then update the means |
| Agglomerative clustering | Show merge order and heights for each linkage rule |
Redo CT2 Set B Q2(b), then switch to the three-cluster semester question so you have to recompute assignments after the first epoch. Finish by writing one hard-margin SVM answer with the objective, constraint and interpretation of the support vectors.







