MACHINE LEARNING • LESSON 10

Choosing K

In KNN, K tells the model how many nearby data points to consider when making a prediction. Choosing K matters because different values of K can produce different predictions.

THE SIMPLEST IDEA

K decides how many neighbors get a vote.

A small K looks at only a few nearby examples. A larger K considers more examples. The right choice depends on the data.

01

What Does K Mean?

Remember that K stands for the number of nearest neighbors KNN uses for a prediction.

K = 1 1 neighbor

Only the closest data point gets to influence the prediction.

K = 3 3 neighbors

The three closest data points can vote.

K = 5 5 neighbors

The five closest data points can vote.

K = number of nearest neighbors used to make the prediction.
02

The Same Data Can Give Different Results

Suppose a new student needs to be classified as Pass or Fail.

Imagine the nearest students have these results:

NEW STUDENT
1st Pass
2nd Pass
3rd Fail
4th Fail
5th Fail

Now watch what happens when we change K.

03

Example 1 — K = 1

If we choose K = 1, KNN looks at only the closest neighbor.

CLOSEST NEIGHBOR Pass
PREDICTION Pass

There is only one vote, so that one neighbor determines the prediction.

K = 1 → very local decision

The prediction depends heavily on the single closest example.

04

Example 2 — K = 3

Now choose K = 3. KNN uses the three closest neighbors.

NEIGHBOR 1 Pass
NEIGHBOR 2 Pass
NEIGHBOR 3 Fail
PASS 2 votes
FAIL 1 vote
PREDICTION Pass

Pass wins the majority vote, so the prediction is Pass.

05

Example 3 — K = 5

Now choose K = 5. KNN considers five neighbors.

1 Pass
2 Pass
3 Fail
4 Fail
5 Fail
PASS 2 votes
FAIL 3 votes
PREDICTION Fail

This time, Fail gets the majority. The prediction changes to Fail.

Changing K can change the prediction because different numbers of neighbors are included in the vote.
06

Small K vs Large K

The choice of K creates a trade-off.

SMALL K More sensitive

A small number of neighbors means individual examples can have a strong influence.

LARGER K More general

More neighbors participate, so the prediction considers a wider group of examples.

07

Why Can a Very Small K Be a Problem?

Suppose K = 1. The prediction depends entirely on one neighbor.

What if that neighbor is unusual or noisy?

NEW DATA
CLOSEST POINT Fail

But this point is an unusual example.

OTHER NEARBY POINTS Pass / Pass / Pass

Most nearby examples suggest Pass.

With K = 1, the unusual point could determine the prediction by itself.

Very small K can make KNN sensitive to noise and unusual data points.
08

Why Can a Very Large K Be a Problem?

A very large K considers many neighbors. This can make the model less sensitive to the local pattern around the new point.

NEW POINT
CLOSE NEIGHBORS Mostly Pass
MANY MORE NEIGHBORS Mostly Fail

If K becomes too large, distant points that are less similar to the new point can influence the prediction.

Very large K can make the model too general and ignore useful local patterns.
09

So What Is a Good K?

There is no single K value that is always best.

A good K depends on the dataset and the problem. In practice, we usually try several values of K and evaluate how well the model performs.

TRY VALUES K = 1, 3, 5, 7...
EVALUATE Compare performance
CHOOSE A suitable K

This is why we don't simply say "K should always be 3" or "K should always be 5."

10

Example: Comparing K Values

Suppose we test three different K values on our validation data.

K VALIDATION ACCURACY OBSERVATION
1 82% Very sensitive
5 91% Best of these choices
15 86% Too general

In this example, K = 5 performs best on the validation data among the values we tested.

We choose K based on evidence from the data, not because one K value is universally correct.
11

An Important Rule

When comparing K values, don't choose the K that simply looks good on the training data.

Use the validation data to compare different K values.

TRAINING DATA Learn from the examples
VALIDATION DATA Compare K values
TEST DATA Final evaluation

This connects directly to the data-splitting concepts from Lesson 4.

REMEMBER THIS

Don't ask "What is the correct K?" Ask "Which K works best for this data?"

Small K values focus strongly on nearby examples, while larger K values consider a broader group. The goal is to find a value that gives good performance on unseen validation data.

Try K Validate Compare Choose K
QUICK CHECK

Check Your Understanding

What does K control? The number of nearest neighbors used for a prediction.
What happens when K = 1? Only the closest neighbor is used.
Why can very small K be sensitive? One unusual or noisy data point can strongly affect the prediction.
Why can very large K be a problem? Distant and less similar examples can have too much influence.
Is there one perfect K for every dataset? No. K should be selected based on how well the model performs on validation data.
What should be used to compare K values? Validation data, rather than simply relying on training performance.
NEXT TOPIC

Distance Between Data Points

We now know that KNN needs to find the nearest points. Next, we'll learn exactly how KNN decides whether one data point is closer to another.