University of Sydney · FACULTY OF MACHINE LEARNING

COMP4318 · Machine Learning and Data Mining

- one subject, every graph, every model, every mark
Machine Learning14 Chapters10-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 11 · COMP4318

Nearest Neighbour & Rule-Based Algorithms

Week 2 introduces instance-based learning — k-nearest-neighbours with its distance metrics and the effect of k — and rule-based learners (1R and PRISM) that build IF–THEN rules. These are direct, hand-computable methods, so they are prime exam material: a typical question gives you a handful of points and asks you to classify a new one with a stated k and distance, or to trace a rule. The weekly homework quiz (h2) drills the same calculations.

In this chapter

What this chapter covers

  • 01k-NN: store all training examples, classify a new point by majority vote of its k nearest neighbours
  • 02Distance metrics: Euclidean (L2, √ of sum of squares), Manhattan (L1, sum of absolute differences), Minkowski
  • 03Feature scaling / min-max normalisation so large-scale features don't dominate the distance
  • 04Choosing k: rule of thumb k ≤ √(#training examples); larger k = more robust to noise, k=1 fits the training set perfectly
  • 05Weighted k-NN (weight ∝ 1/d²) and regression k-NN (average the neighbours' values)
  • 06Complexity: training O(1) (just storing), classification O(mn); speed up with KD-trees / ball trees
  • 071R: one rule on the single best attribute (a decision stump), leaf = majority class, chosen by smallest error rate
  • 08PRISM covering algorithm: add tests to maximise accuracy p/t for a class until p/t = 1
Worked example · free

Classifying a point with k-NN and Manhattan distance

Q [4 marks]. Training set (feature vector, label): (2,2)→−1, (2,8)→+1, (4,4)→−1, (6,5)→−1, (3,6)→+1. Classify the query point (4,7) using k = 3 and Manhattan distance. (4 marks)
  • +1Manhattan distance from (4,7) to each training point = |4−x1| + |7−x2|: (2,2)→2+5=7; (2,8)→2+1=3; (4,4)→0+3=3; (6,5)→2+2=4; (3,6)→1+1=2.
  • +1Sort ascending: (3,6)=2, (2,8)=3, (4,4)=3, (6,5)=4, (2,2)=7. The k=3 nearest are (3,6), (2,8) and (4,4).
  • +1Read off their labels: (3,6)=+1, (2,8)=+1, (4,4)=−1.
  • +1Majority vote of {+1, +1, −1} = +1.
The query (4,7) is classified as +1: two of its three nearest neighbours are +1.
Sia tip — List every distance before you pick the nearest — that working earns a mark and prevents mis-sorting a tie. If the two features were on very different scales you would min-max normalise first, otherwise the large-scale feature would dominate the Manhattan (or Euclidean) distance and swamp the other.
Glossary

Key terms

k-nearest neighbours (k-NN)
An instance-based classifier: store all training examples, and classify a new point by the majority vote of its k closest examples (average their values for regression).
Euclidean distance
The L2 distance D(A,B) = √((a1−b1)² + … + (an−bn)²); the straight-line distance and the default for k-NN.
Manhattan distance
The L1 distance D(A,B) = |a1−b1| + … + |an−bn|; the sum of absolute coordinate differences.
Min-max normalisation
Rescaling each attribute by x' = (x − min)/(max − min) to [0,1] so features on large scales don't dominate distance-based methods.
1R (decision stump)
A one-rule learner: a one-level decision tree testing a single attribute, with each leaf labelled by the majority class; pick the attribute with the smallest training error.
PRISM
A covering (rule-based) algorithm: for each class it adds tests to a rule to maximise accuracy p/t (t covered, p of the target class) until p/t = 1, then adds a default rule.
FAQ

Nearest Neighbour & Rule-Based Algorithms FAQ

How do I choose k in k-NN?

A common rule of thumb is k ≤ √(number of training examples), tuned with a validation set. Small k (down to k=1) fits the training data tightly but is sensitive to noise; larger k is smoother and more robust but can blur genuine class boundaries. In the exam, use the k the question states and show the votes; if asked to choose, pick the k that minimises validation error.

Euclidean or Manhattan — which distance should I use?

Use whichever the question specifies. Euclidean (L2) is the straight-line distance and the usual default; Manhattan (L1) sums absolute differences and is less sensitive to a single large coordinate gap. Both require the features to be on comparable scales, so normalise first.

Why normalise before k-NN?

Because distance sums over all features, an attribute measured on a large scale (say income in thousands) would dominate one on a small scale (say age), so the small-scale feature is effectively ignored. Min-max normalisation puts every attribute on [0,1] so each contributes fairly to the distance.

Where can I find practice for the k-NN and rule questions?

The Week 2 tutorial and homework-quiz (h2) solutions on Canvas are the closest match; work them by hand. This guide's practice exam includes fresh k-NN and rule-tracing questions, and you can ask Sia to generate more at the difficulty you need and explain each step.

Study strategy

Exam move

Drill the mechanical routine until it is automatic: compute the distance from the query to every training point, sort, take the k smallest, then majority-vote (or average for regression). Always write out all the distances — that's where the method marks live. Practise both Euclidean and Manhattan, and rehearse min-max normalisation so you can apply it before a distance calculation when scales differ. For the rule-based side, trace one 1R error table and one PRISM p/t sequence by hand. When a step trips you up, ask Sia to re-run the vote with a fresh point and check your sorting.

Working through Nearest Neighbour & Rule-Based Algorithms in COMP4318? Sia is AskSia’s AI Machine Learning tutor — ask any COMP4318 Nearest Neighbour & Rule-Based Algorithms question and get a clear, step-by-step explanation grounded in how COMP4318 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 12 of your University of Sydney subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMP4318 tutor, unlimited, worked the way the exam marks it
The full 10-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$25/ month
30-day money-back · cancel in one tap · how it works
Unlock the full COMP4318 Bible + 12 University of Sydney subjects解锁完整 COMP4318 Bible + University of Sydney 12 门科目
$25/mo