English:K-nearest neighbours and distance-based classification - AI Year 8
Introduction

Can nearby examples help you predict a label? Rank exact distances, compare k=1 with k=3 and investigate what happens when neighbours disagree. You will use fictional cards, two held-out reference labels and an executed Python model. The labels cat and dog are category names on abstract cards; no animal is measured or classified.
Features, neighbours and a reproducible prediction
A classifier predicts a category. A k-nearest neighbours classifier compares a new case with labelled training examples. Choose the features, a distance measure, a positive integer k and a voting rule. The letter k states how many neighbours take part. It does not mean the number of categories, coordinates or correct answers. Here every selected neighbour has one equal vote; we do not use inverse-distance weighting.
Our original cards have two abstract coordinates, x and y. The target is a supplied category name. The two axes use the same declared unit for this teaching example. These are not real cat or dog measurements, personality scores or pupil profiles. Keeping the task artificial lets you inspect every assumption without collecting private data.
| Training card | x | y | Supplied label |
|---|---|---|---|
| A | 1 | 0 | cat |
| B | 0 | 2 | dog |
| C | 2 | 1 | dog |
| D | 3 | 0 | cat |
| E | 0 | 4 | cat |
The query Q is at x=0, y=0. Calculate the horizontal difference, square it, calculate the vertical difference, square it and add the two results. We rank squared Euclidean distances:
All squared distances are nonnegative, and taking square roots would preserve their order. Squared distances are not the same numerical quantities as distances. For A the squared distance is one; for B it is four; for C it is five. Continuing gives D nine and E sixteen. The order is A, B, C, D, E. With k=1 the unique nearest card A gives cat. With k=3 the selected cards are A, B and C: dog has two votes and cat has one. The prediction is dog. The single nearest card therefore does not always decide.
Try before checking: use k=5 for Q. Write the five labels, count each category and predict the result. First hint: include every card, not just the closest three. Partial step: A, D and E share a label. Check after attempting: cat has three votes, dog two, so the result is cat. This change is caused by k; the query and training cards have not changed.
StatQuest with Josh Starmer explains the neighbouring-example idea. The recording uses a different example; its biological illustrations do not validate our abstract cards. Pause to ask which features and voting rule are being used. You can complete every calculation here without playing the video.
The prepared source outline also gives an ordered list of ordinary distances: 1.2, 1.8, 2.1, 4.5 and 5.0. Its first three labels are cat, cat and dog, so k=3 predicts cat. The source does not specify the last two labels. You cannot deduce its k=5 result until those labels are supplied. This is a separate example, not the squared-distance table above. Different data can legitimately produce different answers.
Tie policies, implementation and boundary cases
An algorithm needs a defined response when evidence does not produce a unique choice. Distinguish a tie at the neighbour cutoff from a tie in the votes. For this original classroom implementation, either unresolved situation returns ASK. ASK means defer this prediction for review. It is not an extra animal category or proof of safety. Other libraries may resolve ties differently; our conservative teaching policy is a declared design decision, not a universal rule of kNN.
Query R has coordinates x=0, y=3. The squared distances are B one, E one, C eight, A ten and D eighteen. At k=1, B and E are equally near but have different labels. Selecting whichever identifier comes first would silently turn a display convention into a prediction rule. Our cutoff policy returns ASK when the kth and next distances are equal. At k=3 both tied nearest cards are included, followed by C, so there is no tie at the cutoff. Dog has two votes and cat one: predict dog.
For Q with k=2, A and B are selected without a cutoff tie. Their labels receive one vote each. This is a vote tie, so the result is ASK for a different reason. Do not merge these two failure explanations. Odd k can avoid an equal vote between two categories in an unambiguous equal-vote selection, but it does not solve every tie. With three categories, three selected neighbours can give bird, cat and dog one vote each. Odd k=3 still has no unique winner. Equal distances can also straddle a cutoff when k is odd.
Predict before reading the code: should k=0, k larger than the training count or an empty training list be accepted? Hint: k counts actual examples. Partial step: zero selects no evidence. The program rejects these inputs with ValueError rather than silently changing k. It also rejects duplicate card identities, missing coordinates, empty labels and the reserved label ASK. It accepts exact integers and Fraction values; floats and Boolean values are deliberately outside this small program's input contract. These choices simplify reproducible arithmetic rather than defining every valid production implementation.
The following complete standard-library program was actually executed during course preparation. It stores the labelled cards and performs the declared distance and voting procedure. It does not train neural weights, download a model or invoke generative AI. An identifier orders equal-distance rows for display; the explicit cutoff check prevents that order from secretly deciding tied membership.
"""Original equal-vote two-feature kNN with explicit conservative tie policies."""
from collections import Counter
from fractions import Fraction as F
import json
def number(value):
if type(value) not in [int, F]:
raise ValueError("Use exact integers or fractions")
return F(value)
def classify(query, training, k):
if type(query) not in [list, tuple] or len(query) != 2:
raise ValueError("Supply two query coordinates")
qx, qy = map(number, query)
if type(training) not in [list, tuple] or not training:
raise ValueError("Supply nonempty labelled training cards")
if type(k) is not int or not 1 <= k <= len(training):
raise ValueError("Choose an integer k within the training count")
ranked, ids = [], set()
for row in training:
if type(row) not in [list, tuple] or len(row) != 4:
raise ValueError("Each card needs id two coordinates and label")
identity, x, y, label = row
if type(identity) is not str or not identity or identity in ids:
raise ValueError("Use distinct nonempty card identities")
if type(label) is not str or not label or label == "ASK":
raise ValueError("Use a nonempty label distinct from the deferral marker")
ids.add(identity)
distance = pow(qx - number(x), 2) + pow(qy - number(y), 2)
ranked.append(dict(id=identity, label=label, squared_distance=distance))
ranked.sort(key=lambda r: (r["squared_distance"], r["id"]))
if len(ranked) > k and ranked[k - 1]["squared_distance"] == ranked[k]["squared_distance"]:
return dict(prediction="ASK", reason="cutoff_tie", ranked=ranked,
selected_ids=[], votes=dict())
selected = ranked[:k]
votes = Counter(r["label"] for r in selected)
best = max(votes.values())
winners = sorted(label for label, count in votes.items() if count == best)
return dict(prediction=winners[0] if len(winners) == 1 else "ASK",
reason="unique_vote_winner" if len(winners) == 1 else "vote_tie",
ranked=ranked, selected_ids=[r["id"] for r in selected], votes=dict(votes))
def demonstrate():
training = [("A", 1, 0, "cat"), ("B", 0, 2, "dog"), ("C", 2, 1, "dog"),
("D", 3, 0, "cat"), ("E", 0, 4, "cat")]
q = [classify((0, 0), training, k) for k in [1, 3, 5]]
r = [classify((0, 3), training, k) for k in [1, 3]]
three_classes = [("X", -1, 0, "bird"), ("Y", 0, -1, "cat"), ("Z", 1, 0, "dog")]
return dict(training=training, query_q=q, query_r=r,
q_even_k=classify((0, 0), training, 2),
odd_k_three_class_tie=classify((0, 0), three_classes, 3),
original_reference_labels=dict(Q="cat", R="dog"),
real_animals_classified=False, local_generative_model_called=False)
if __name__ == "__main__":
print(json.dumps(demonstrate(), indent=2, default=str))The printed output retains the ranking, selected identifiers, vote counts, prediction and reason. Trace Q and R by hand first, then compare with the executed output. If you run the program in a school-approved local Python environment, retain your actual output and any changes. A copied code listing alone is not evidence that you executed it. For larger datasets this simple full sort is not the only search implementation; different search structures concern efficiency, while the stated prediction rule must still be checked.
Evaluation, scaling and a responsible transfer
A changing prediction is not automatically an improvement. To judge predictions, you need reference labels that were not used as training examples, a defined test and an appropriate comparison. For our original fictional test fixture, the held-out reference for Q is cat and for R is dog. These reference labels were authored for the teaching exercise; they were not discovered by observing real animals. Neither Q nor R is inserted into the training cards.
| Case | Held-out reference | k=1 result | k=3 result | Constant-cat baseline |
|---|---|---|---|---|
| Q at zero zero | cat | cat | dog | cat |
| R at zero three | dog | ASK | dog | cat |
For k=1 there is one matching label, no wrong category prediction and one deferral. It covers one of the two cases. Among that single answered case, one of one is correct. For k=3 there is one matching label and one wrong category prediction, with both cases answered. It covers two of two; one of two answered cases is correct. The constant-cat baseline also matches one of two, answers both and fails on R. Recording only the number of matching labels would hide the difference between an error and a deferral. Recording only conditional correctness would hide how much the model refused to answer. A deferral still needs a workable response and may create delay or workload.

Read the axes before using a confusion matrix. This illustration puts actual values across columns and predictions down rows; other sources may reverse them. If you designate dog as positive for k=3, R is a true positive and Q is a false positive; there are no cat predictions in these two cases. For k=1, Q is a true negative and R is a deferral, which must not be silently recorded as a predicted negative. Two artificial cases are not a reliable estimate of real-world performance. If you use them to choose k, they have become model-selection evidence; evaluate the chosen method on fresh independent cases before making a new performance claim.
Distance depends on feature choice and scale. Changing centimetres to millimetres in just one feature can change which differences dominate, even when the underlying objects have not changed. Scaling can therefore change neighbours. It does not guarantee better accuracy. Define transformations using training data only, keep them fixed for validation and test cases, and avoid target leakage. The prerequisite course Feature engineering, scaling and data leakage - AI Year 8 supplies a separate exact worked scaling example. Do not normalise each query using its hidden reference label or refit preprocessing on the final test data.
CodeAI discusses training data and bias. Relate it to the choice of examples and features, without claiming that five invented cards establish fairness for a population. A minority category may be outvoted; simply increasing k can worsen local decisions. A numerical similarity is not evidence of biological identity, personal ability or equal treatment.
Transfer workshop: imagine sorting fictional craft kits into paper and wood categories from width and mass. Write the target, units and permitted features before drawing training cards. First hint: millimetres and grams have different meanings. Partial step: compare a declared scaled distance rule with a constant-label baseline, using the same unused cases. After your attempt, check that your plan separates training, model selection and final evaluation; includes ties, missing values and a reviewed ASK route; and makes no unsupported claim about safe handling or real material identification. State which error would matter to the user and what further evidence would be required.
Interactive Tasks
Quiz: Test Your Knowledge
For Q the ordered labels are cat dog dog cat cat. What does equal-vote k=3 predict? (dog) (!cat) (!ASK because k is odd) (!The label of the last card)
What does k represent in this classifier? (The number of selected neighbours) (!The number of coordinates in every model) (!The number of mistakes allowed) (!The number of categories required)
Why can squared Euclidean distances rank the neighbours here? (Taking square roots preserves their nonnegative order) (!Squaring always preserves any signed list order) (!Squared distance equals ordinary distance in every case) (!Distances are irrelevant to kNN)
Why does R return ASK at k=1 in our program? (Two equally close cards straddle the neighbour cutoff) (!There are no training cards) (!All five labels are identical) (!Every odd k must defer)
What happens for Q at k=2 under our policy? (A vote tie causes ASK) (!The second card alone decides) (!The query becomes training data) (!A missing coordinate is invented)
Does odd k=3 guarantee a unique vote winner across three classes? (No because three labels may receive one vote each) (!Yes because odd numbers remove all ties) (!Yes if card identifiers are alphabetical) (!No because kNN cannot use three classes)
Which summary is correct for k=1 on Q and R? (One matching label and one deferral) (!Two matching labels) (!Two wrong labels) (!One wrong label and no deferrals)
What should follow choosing k using evaluation cases? (A fresh independent evaluation for a new performance claim) (!Calling those same cases untouched final tests) (!Adding all final labels as input features) (!Publishing only the better looking metric)
Which preprocessing choice helps avoid leakage? (Derive scaling from training data and keep it fixed) (!Use hidden test labels to choose feature scales) (!Refit scaling on every final test answer) (!Treat a label as an allowed earlier measurement)
What does the executed card program demonstrate? (The declared calculation on fictional examples) (!Reliable identification of real animals) (!Guaranteed fairness for all pupils) (!Safe deployment for any decision)
Memory Game
| Neighbour | A training example selected for proximity |
| Feature | An allowed input representation |
| Target | The category intended as the output |
| Deferral | A case left for a declared review route |
| Baseline | A simple comparison under the same evaluation |
| Leakage | Using information unavailable for the intended prediction |
Drag and Drop
| Choose the permitted measurements and units | Feature definition |
| Order training cards by the declared distance | Neighbour search |
| Count equal votes from the selected examples | Label prediction |
| Keep an unresolved case for a documented response | Tie handling |
| Compare frozen methods on unused labelled cases | Independent evaluation |
Crossword Puzzle
| FEATURE | What allowed input quantity represents a case? |
| TARGET | What category is the classifier intended to predict? |
| DISTANCE | What numerical measure orders nearby examples? |
| VOTES | What equal contributions are counted for selected labels? |
| BASELINE | What simple comparison method tests the value of complexity? |
| LEAKAGE | What problem uses unavailable information for prediction? |
LearningApps
Optional extra practice. Choose an appropriate activity with your teacher. If the external collection is unavailable, use the native activities and independent tasks in this course.
Cloze Text
Open Tasks
Easy
- Draw the five feature cards: Plot A to E on labelled equal-unit axes and add Q without its reference label. Hint: label x before y. Partial step: B lies at zero two. Check against the supplied coordinate table only after plotting.
- Trace a neighbour ranking: Calculate all squared distances from Q and rank the cards. Hint: square each coordinate difference separately. Partial step: A contributes one plus zero. Compare with the worked ranking after your attempt.
- Explain a changing vote: Use the same ranking to compare k one three and five. Hint: count selected labels rather than copying the nearest one. Retain the votes and explain why the answer changes.
- Read an evaluation diagram: Explain which axis shows actual classes in the supplied matrix and where true positive lies. Hint: read the labels first. Do not invent counts or hide a deferral inside a binary prediction cell.
Standard
- Separate two kinds of tie: Trace R with k one and Q with k two. Hint: ask whether selection or voting is unresolved. Name each reason and propose a visible review message without silently changing k.
- Compare coverage and correctness: Rebuild the two-case evaluation table for both methods and the baseline. Hint: keep ASK separate. Record matching labels out of all cases, coverage and correctness among answered cases with explicit denominators.
- Test the input contract: Write expected outcomes for empty training data invalid k and duplicate card identities before optionally executing the retained program. Hint: invalid data should be rejected rather than guessed. Preserve actual output separately from predictions.
- Investigate feature scaling: Use the prerequisite scaling course to explain how a unit change can alter neighbours. Hint: inspect which coordinate difference dominates. State the training-only fit rule and avoid claiming improvement without reference labels.
Advanced
- Challenge the odd-k shortcut: Construct three fictional categories with one vote each at k three. Hint: include all three neighbours so there is no cutoff issue. Explain why this is a vote tie and distinguish the two-category special case.
- Design a fresh evaluation: After selecting k using Q and R propose unused cases and reference labels fixed before testing. Hint: include an ordinary case a tie and a deliberately different region. Explain the limits of a tiny authored sample.
- Transfer to fictional craft kits: Define a paper-versus-wood card task using width and mass. Hint: declare units scale and target before collecting examples. Include a baseline consequences of errors and a reviewed deferral route without real pupil or product-safety data.
- Audit an overconfident report: Critique a report claiming that one correct answered case proves universal fairness. Hint: request the unanswered cases and all denominators. Produce a corrected report that distinguishes executed arithmetic from operational suitability.
Learning Check
- Predict with two values of k: Use Q to compare k one and k three and explain the misconception that one nearest example always decides.
- Trace the held-out boundary: Rank R and state the outcomes for k one and k three under our policies.
- Distinguish the evaluation denominators: Compare k one k three and constant cat on the two supplied references.
- Analyse a three-class tie: Why can bird cat and dog at k three still produce ASK?
- Check a scaling and testing plan: A team fits feature scaling using final test cases then chooses k from final scores. Explain two problems and a better sequence.
- Transfer without overclaiming: Describe evidence needed before using the fictional craft-kit classifier for a new task.
Worked checks and assessment guidance
- The squared-distance order is A one B four C five D nine E sixteen. k one selects A and predicts cat. k three selects A B C with one cat and two dog votes and predicts dog. The same dataset therefore supplies a concrete counterexample to the single-nearest claim.
- R has B one E one C eight A ten D eighteen. k one cuts through the equal nearest distance and returns ASK with cutoff_tie. k three includes both tied nearest cards and C then predicts dog by two votes to one. Identifier order is display only and is not allowed to invent unique membership.
- k one matches Q and defers R: one matching label out of two cases coverage one of two and correctness one of one among answered cases. k three misclassifies Q and matches R: one of two matching coverage two of two correctness one of two among answered cases. Constant cat also matches one of two and covers both. No universal winner or fairness conclusion follows.
- Each selected label has one vote so there is no unique highest count. All three examples are included and there is no cutoff tie in that fixture. Odd k only prevents a fifty-fifty equal-vote division between two categories when selection is unambiguous; it does not remove three-class ties or equal-distance cutoff ties.
- Test information has entered preprocessing and repeated k selection has consumed the final set as model-selection evidence. Fit transformations on training evidence keep them fixed choose k with separate validation or suitable training-only cross-validation then obtain a fresh final evaluation. Appropriate scaling and that separation still do not guarantee future accuracy.
- State target units permitted features distance and tie policy. Keep labelled training and model-selection examples apart from a fresh final set compare the same cases with a baseline record errors and deferrals separately and document their consequences. A school card simulation is not material identification or product-safety approval; real use would need appropriate evidence governance and a workable human response.
Learning evidence
Retain a labelled feature plot, all five exact distances for Q, votes for two or more values of k, both distinct tie traces, the two-case table with honest denominators and a separate transfer plan. Preserve the original training and held-out fixture, declared preprocessing and baseline, expected boundary outcomes and actual Python output if executed. Explain the three-class counterexample and remaining uncertainties. These artefacts show reasoning on synthetic data; they do not certify animal identification, deployment competence or complete curriculum coverage.
OER and Wikipedia
scikit-learn: Nearest Neighbors is the primary technical reference for classification by nearby labelled examples, distance choices and equal or distance-weighted voting. The course uses its own exact standard-library implementation with separately declared conservative tie policies; it does not claim to reproduce scikit-learn tie handling. DfE Computing, Key Stage 3 supplies algorithm and abstraction context. This Year 8 AI route is editorial enrichment, not a separate statutory AI subject. Relevant source sections inspected 15 November 2026.
2D Cartesian Coordinates.svg, H Padleckas, public domain as recorded on the file page. ConfusionMatrix.png, Jackverr, CC BY-SA 3.0. Original illustrations unchanged; neither contains our original card data. The matrix has actual classes in columns and predictions in rows.
https://www.youtube.com/watch?v=HVXime0nQeI — StatQuest with Josh Starmer: StatQuest: K-nearest neighbors, Clearly Explained. https://www.youtube.com/watch?v=x2mRoFNm22g — CodeAI: AI: Training Data & Bias. Actual metadata, playability, embedding and preview bytes verified; previews visually inspected. Original videos embedded unchanged without transcript copying, endorsement, redistribution or relicensing implied. The complete teaching examples and activities work independently of the recordings.
All five cards, Q and R, their held-out labels and the three-class tie fixture are original fictional examples. The exact Python model was actually executed and independently cross-checked. No real animals or pupils were classified, no practical deployment took place and no local generative LLM or paid API was used.
Linked learning areas
aiMOOC Projects
Schulfach+


aiMOOCs


aiMOOC Projekte


MOOCwiki · Deutsch
Nach dem Lernen ist vor dem Lernen
Entdecke direkt den nächsten Lernkurs. Weitere Inhalte erscheinen, wenn Du weiter nach unten scrollst.
Zur MOOCwiki-HauptseiteMediathek
Mediathek wird aus dem Wiki geladen ...
Keine passenden Inhalte gefunden. Bitte ändere Suche oder Filter.
NEWSLernweltNOAH fragen