Key points, notes and past questions
k-NN
Distances, k, classification and regression
- Orientation: what you will learn and why
- Big picture: concept map, model comparison and the recipe
- Start here: distance and voting
- Euclidean: $\sqrt{\sum_j(a_j-b_j)^2}$. Manhattan: $\sum_j|a_j-b_j|$.
- Squared distances give the same neighbour order as distances, with no square root.
- Features on big scales dominate the distance, which is why we rescale.
- Classification: majority vote. Regression: average.
- The k-NN algorithm and how to run it
- Always compute the distance to <b>all</b> $n$ points first.
- Pick the $k$ smallest, then vote. Do not vote before sorting.
- Ties in a vote or in distance: follow the rule stated in the question.
- $k$ is the number of neighbours, not the number of features.
- Choosing k: smooth boundary, overfitting and training error
- Small $k$ → complex, wiggly boundary, low bias, high variance (overfit).
- Large $k$ → smooth boundary, high bias, low variance (underfit).
- $k=1$ gives zero training error when no two identical points have different labels.
- Training error is not a fair way to pick $k$. Use validation or LOOCV.
- Properties and cost: lazy, non-parametric, counting distances
- Lazy: no explicit model is built during training.
- Non-parametric: no fixed set of fitted parameters, the data <i>is</i> the model.
- The training set is needed to predict.
- One prediction costs $n$ distances, independent of $k$ (about $n\cdot d$ operations).
- k-NN is non-parametric: it fits no distribution, and the data itself is the model.
- k-NN regression: averaging the neighbours
- $\hat y(x)=\frac1k\sum_{i\in N_k(x)}y_i$.
- The fitted curve is a staircase: it changes only when the set of neighbours changes.
- Classification = vote, regression = average. Everything else is identical.
- Leave-one-out cross-validation with k-NN, and changing labels
- LOOCV error = (number of wrong predictions) ÷ $n$.
- The left-out point is excluded from its own neighbours (unlike training error).
- Do one point at a time, writing its neighbours explicitly.
- With $k=n-1$ and two balanced classes, every left-out point sees the other class as the majority, so LOOCV is about 1.
Notes14 past questions from this week
Decision Trees
Entropy, information gain, splits, depth, decision boundaries
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: the maths you need, from zero
- Proportion = part ÷ whole, always between 0 and 1.
- On a calculator, $\log_2 x=\ln x/\ln 2$. Also $\log_2 1=0$, $\log_2 2=1$, and the $\log_2$ of a fraction below 1 is negative.
- $0\cdot\log_2 0$ is taken as $0$.
- Weighted average: multiply each value by its group size, add, then divide by the total size.
- What a decision tree is
- Node = a group of points. Internal node = a node that holds a question. Leaf = a node that holds a label.
- To predict: follow the answers from the root to a leaf and output that leaf's majority label.
- A question does not always split the data into two non-empty groups (all points may answer the same way).
- Classifiers are judged by the 0-1 loss: count the wrong predictions.
- Entropy: measuring how mixed a node is
- $E(p)=-p\log_2p-(1-p)\log_2(1-p)$, where $p$ is the proportion of label 1.
- Pure node: $E=0$. Half-half node: $E=1$, the maximum for two classes.
- Symmetric: $E(p)=E(1-p)$, so 75/25 and 25/75 both give $0.811$.
- Only proportions matter, not counts: 3 and 1, or 300 and 100, both give $0.811$.
- Always $\log_2$. With $\ln$ the maximum would come out as $0.693$, not 1.
- Calculating entropy in the exam: method, given logs, ranges
- Counts → $p$ → two terms → add.
- If the question prints $\log_2$ values, use those. Round only at the end.
- Quick checks: $E(0.5)=1$, $E(0.25)=0.811$, $E(0.1)=0.469$.
- Write the terms separately before adding. Most lost marks are a dropped minus sign.
- Information gain: scoring a question
- $\text{IG}=E(\text{parent})-\sum_c\frac{n_c}{n}E(\text{child}_c)$.
- Weights are child size ÷ parent size, never a plain average of the child entropies.
- IG is never negative. IG $=E(\text{parent})$ is the largest possible: every child pure.
- A split that is 50/50 in size can still have zero gain. Only label purity matters.
- Comparing two questions at the same node: $E(\text{parent})$ is the same, so the smaller weighted child entropy wins.
- Numeric features: questions of the form x ≤ θ
- Question form: $x_j\le\theta$ goes to the Yes branch, $x_j>\theta$ to the No branch.
- Candidate thresholds lie between consecutive distinct sorted values.
- Compute the gain for every candidate on every feature and take the largest.
- On XOR-style data the first question can have zero gain, yet the tree still needs it.
- Tree structure: depth, regions and decision boundaries
- Depth = number of <b>edges</b> on the longest root-to-leaf path (not the number of nodes).
- At depth $m$ there are at most $2^m$ leaves. Number of questions (internal nodes) = leaves − 1.
- Boundaries are axis-parallel: vertical and horizontal lines in 2D, so rectangles and staircases.
- To isolate one box of one class in $d$ dimensions you need $2d$ questions, one per side of the box.
- Predicting a point = asking which rectangle it falls in.
- Properties: depth, overfitting, stability
- Shallow tree: high bias, underfits. Deep tree: high variance, overfits.
- A fully grown tree reaches zero training error provided no two identical points have different labels.
- Zero training error does not mean a good model.
- A depth limit or pruning controls complexity. Choose the depth with validation data.
- Trees are unstable: changing one point can change the very first question.
- A tree stores only its questions. k-NN must keep all the training data.
Notes24 past questions from this week
Week 6: Probabilistic view, Ridge, Lasso and cross-validationWeek 8: Generative models and Naive BayesAll MLT topics and weeks