---
title: "Building a nearest-neighbor classifier"
course: "AI101 · AI, explained"
unit: "Learning from data with math"
lesson: 25 of 36
source: https://open.maestrocollege.edu/ai-explained/building-a-nearest-neighbor-classifier
publisher: Maestro College
---

# Building a nearest-neighbor classifier

Lesson 25 of 36 in **AI, explained** (AI101), unit "Learning from data with math". An open lesson from Maestro College.

## What the learner will be able to do
- Build a classifier that labels a new item with the category of its most similar labeled example.
- Extend it to vote among the k most similar labeled examples, breaking ties with the single nearest one, and report the vote share behind each prediction as its confidence.
- Understand how a learned classifier's behavior comes from its labeled examples rather than written rules, by tracing a prediction to the examples that caused it.

## Teaching notes
STARTING STATE: vectors.py through cosine(). IN THIS LESSON: the learner creates classifier.py with knn_vote(text, k, examples), which ranks the examples by cosine similarity of bag-of-words vectors over the training vocabulary and returns (majority category of the top k, its vote share), the nearest example breaking ties, and knn_classify(text, k, examples) returning only the category; k=1 first. They classify three tickets they write themselves at k=1 and k=5 with examples=load_tickets('train'), printing the nearest tickets and vote shares for two. No test tickets: L10 measures once. END STATE: committed as 'Add nearest-neighbor classifier'.

## Course teaching approach
- Start from a real-world problem, then introduce the concept as the solution.
- Keep it true beginner level, hands-on, a little gamified. The learner should feel they are winning.
- Balance explanation, demonstration and practice. No copy-paste exercises: give small story-based tasks.
- End with one fun closed question about the topic.

## How to use this document
You are acting as a tutor for a learner on Maestro Open. Teach from this document, one small step at a time. Ask the learner to try things, wait for their answer, then give feedback. Keep it beginner-friendly. If the lesson involves code or spreadsheets and you cannot run them, ask the learner to try it on their own computer and paste the result back. Do not award grades, credit or certificates.

## When the lesson is done
The lesson is complete only when the learner has shown they can do every outcome listed above. Check each one with a small task or question; if an answer is wrong, explain and let them try again before moving on.
When every outcome is covered:
1. Say clearly: "Lesson 25 complete: Building a nearest-neighbor classifier."
2. Give a two-line recap of what they learned.
3. Name the next lesson, "Estimating probabilities from counts" (lesson 26 of 36), and give the learner its link: https://open.maestrocollege.edu/ai-explained/estimating-probabilities-from-counts.md. Tell them to paste that page into this chat, or fetch it yourself if you can open links, then start it the same way. Do not start teaching the next lesson's topic until the learner has it.

Previous lesson: Cosine similarity: same words, any length
Next lesson: Estimating probabilities from counts
