shapesinfinity.
Learn AI · Beginner · 3 min

K-Nearest Neighbours: Find Your Tribe

What if you learned by copying your closest friends?

How KNN Finds Your Neighbours
New person to guessDistance to all stored peopleK closest neighbours selectedCount which group winsPrediction (group guess)K must be small enough to matter

Measure distance to all stored examples. Pick K closest. Count their groups. Pick the winner.

goes inthe AI workscomes outwatch out

In simple words

Imagine you want to know if someone is kind. You look at their three closest friends. If most are kind, they probably are too. KNN copies this idea — it looks at neighbours to guess.

The real definition

K-Nearest Neighbours (KNN) is a simple machine learning algorithm that guesses what something is by looking at its nearest examples in the data. If most neighbours belong to group A, the new item probably does too.

Like… Finding Your Restaurant Style

You want to pick a new restaurant. You ask your three closest friends where they eat. Two say Italian, one says Thai. You probably go Italian. KNN does exactly this with data.

But: Friends give opinions. KNN only measures distance, not quality.

You see it every day

on your phone

Spam detector on phone

Phone checks if an email is spam by finding similar emails you marked spam before.

out in the world

House price guesser

A real estate website guesses your house price by looking at the three closest sold houses.

at work

Employee skill prediction

HR software guesses if you will be good at a job by checking similar employees' success.

Step by step

  1. 1

    Gather training data

    Collect stored examples with their correct labels (known answers).

  2. 2

    Measure distance

    Calculate how far away the new example is from every stored example.

  3. 3

    Pick K neighbours

    Sort by distance. Take the K closest examples.

  4. 4

    Vote and decide

    Count which label appears most. That becomes your guess.

Remember

KNN looks at nearby examples to make guessesK is the number of neighbours you checkDistance means how different two things areVote with neighbours; majority winsSimple but needs good training data

Memory trick

K = how many friends you ask. More friends = safer guess, but slower. Ask too many and strangers vote.

Words

Algorithm AL-go-rith-um
A step-by-step rule that a computer follows.
Machine learning muh-SHEEN LER-ning
Teaching a computer to learn from examples.
Data DAY-tuh
Information or facts stored in a computer.
Label LAY-bul
The correct answer or group name for an example.
Distance DIS-tuns
How different or far apart two examples are.
Neighbour NAY-bor
A stored example that is close to the new one.
Training data TRAY-ning DAY-tuh
Examples used to teach the AI to work.
Prediction pruh-DIK-shun
The guess the AI makes about something new.
Model MAH-dul
A learned program that makes predictions.

Check yourself

quick check

What does K stand for in K-Nearest Neighbours?

Hint

Think: you ask K friends for advice.

quick check

A doctor wants to guess if a patient has a disease. The doctor checks five similar past patients. Three had the disease; two did not. What does KNN predict?

Hint

Count the winners in the K neighbours.

think

Why might KNN work poorly if you only have five stored examples?

Show a good answer

With only five examples, each new guess uses very few neighbours. One wrong example ruins the vote. You need many examples for safe predictions.

Tell a friend

“KNN is lazy — it just copies the opinion of your closest friends. Simple, but it works.”

People also ask

Why is it called 'lazy' learning?

KNN waits until you ask it a question. Then it searches for neighbours. It does not build a model in advance.

What if all K neighbours belong to different groups?

KNN picks the group that appears most often. If it is a tie, different software handles it differently.

Does KNN work with numbers and words?

KNN works best with numbers because distance is easy to measure. With words, you must convert them to numbers first.