K-Nearest Neighbors (KNN)

The "Friendly Guesser" - Learning by looking at the closest things around you.

What is KNN?

Imagine you move to a new town. You want to know what kind of street you live on. You look at your closest neighbors. If most of your closest neighbors are quiet, you guess your street is quiet. This is exactly how KNN works!

🛏️

The "Lazy" Learner

KNN is called "lazy" because it does not do any hard work early on. It does not build a complex mathematical brain. It simply memorizes all the data you give it. It waits until you ask a question, and then it searches for the closest answers.

How It Works (Step-by-Step)

1

Choose 'K'

Pick a number, like K=3. This means we will look at the 3 closest friends.

2

Measure

Measure the distance from your new question to every single piece of data we saved.

3

Find Closest

Pick the 'K' (e.g., 3) items that have the shortest distance. These are the neighbors!

4

Look Around

Look at the labels or numbers attached to those closest neighbors.

5

Make Guess

Vote (for categories) or take the average (for numbers) to get your final answer.

Classification

Taking a Majority Vote

If we want to sort things into groups (like "Is this a Cat or a Dog?"), we look at the neighbors and vote. If 2 neighbors are Cats and 1 is a Dog, the winner is Cat!

Regression

Taking the Average

If we want to guess a number (like "How much does this house cost?"), we look at the neighbors' prices and find the average (the middle number).

Measuring Distance: How close is close?

Before we can find the "nearest" neighbors, we need to know how to use a ruler. KNN uses different types of rulers to measure space between points.

✈️ Euclidean Distance

The "Straight-Line" way. Like a bird flying straight from point A to point B.

Example: Point (2,3) to (6,7) = 5.65 steps

🏙️ Manhattan Distance

The "City-Block" way. Like walking on streets around buildings. You can only move up, down, left, or right.

Example: Point (2,3) to (6,7) = 8 steps

* Picking the right ruler changes the answers!

👍 The Good (Pros)

  • Simple to Understand: The idea of checking neighbors makes perfect sense.

  • No Training Wait Time: Since it just memorizes data, it does not need hours to "learn" before you use it.

  • Very Flexible: It can guess both categories (Apple vs Orange) and numbers (Price).

👎 The Bad (Cons)

  • Slow for Big Data: If you have millions of data points, measuring the distance to every single one takes a long time.

  • Needs Lots of Memory: Because it saves everything, it takes up a lot of computer space.

  • Gets Confused Easily: If one detail is measured in millions and another in tiny decimals, the big numbers crush the small ones. It hates messy data.

Where Do We Use KNN?

🛒

Shopping Ideas

"People who bought this shirt also bought these shoes!" It finds neighbors with similar tastes.

🩺

Doctor's Helper

It looks at a sick person's symptoms and finds past patients with similar symptoms to guess the illness.

🖼️

Image Checking

Computers look at pixels (tiny dots) in an image to see if it is close to images of a dog or a cat.