Chapter 4

Neighbours and naive Bayes

Nearest-neighbour prediction compares a query with stored examples. Naive Bayes instead compares class likelihoods. Both can be calculated by hand, but distances and conditional probabilities answer different questions.

4.1 Distances and nearest-neighbour prediction

Past papers · DA 2024–2026 (3 papers): 1 question, 2 marks

Let x,v∈Rd\mathbf x,\mathbf v\in\mathbb R^d be feature vectors. Use a meaningful common scale before adding distances across coordinates. Estimate scaling parameters using training data only.

Distance-weighted prediction replaces each vote by a nonnegative weight aia_i; regression becomes ∑aiyi/∑ai\sum a_i y_i/\sum a_i. For weights inverse to distance, an exact match must be handled separately. Sorting squared Euclidean distances gives the same neighbour order as sorting Euclidean distances.

Small kk usually gives low bias and high variance; large kk smooths the rule. At k=nk=n, unweighted classification always predicts a global majority class. One-neighbour training error is zero for distinct feature vectors with consistent labels when self-neighbours are allowed. It is not a test-error guarantee. An odd kk prevents binary vote ties, but not equal-distance ties or multiclass vote ties. In high dimension, many observations may be needed to populate local neighbourhoods.

4.2 Class scores and posterior error

Past papers · DA 2024–2026 (3 papers): 1 question, 1 mark

Let there be CC classes, prior πc=P(y=c)\pi_c=P(y=c) and conditional feature model pj(xj∣c)p_j(x_j\mid c). Naive Bayes assumes mutual conditional independence of features given the class. It does not assume that features are marginally independent.

4.3 Fitting likelihoods and counting parameters

Past papers · DA 2024–2026 (3 papers): 1 question, 1 mark

Let ncn_c be the number of observations of class cc, and ncjvn_{cjv} the number with feature jj equal to category vv in that class. Feature jj has rjr_j possible categories. The prior estimate is π^c=nc/n\hat\pi_c=n_c/n when no prior smoothing is requested.

Without conditional independence, a full categorical joint model requires (C−1)+C(∏jrj−1)(C-1)+C(\prod_jr_j-1) free parameters. If priors are supplied rather than learned, omit the C−1C-1 prior parameters. A zero conditional probability makes an entire unsmoothed class score zero.

For a count vector with a fixed total count, multinomial naive Bayes uses class score ln⁡πc+∑jxjln⁡θcj\ln\pi_c+\sum_jx_j\ln\theta_{cj}, omitting the class-independent multinomial coefficient. Here ∑jθcj=1\sum_j\theta_{cj}=1 and θ^cj=(Ncj+α)/(∑jNcj+αd)\hat\theta_{cj}=(N_{cj}+\alpha)/(\sum_jN_{cj}+\alpha d), where NcjN_{cj} is the total count of category jj in class cc. This differs from Bernoulli modelling, which also counts absent binary features.

Last-minute recall

  • Choose the distance metric before finding neighbours.
  • Scale features using the training subset only.
  • Naive Bayes independence is conditional on the class.
  • Add log likelihoods to avoid multiplying very small values.
  • Normalise scores before reporting a posterior error probability.
  • Count free probabilities, not all probability-table entries.
  • Gaussian naive Bayes allows a different variance in each class and feature.

Revise offline: the whole Machine Learning handbook as one PDF

42 pages · 1.8 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections