Concepts / Mastering Machine Learning Models

Mastering Machine Learning Models

The Nearest Neighbors model looks at the stored examples closest to a new data point to reason about it.

  • Programming

Choosing a Model by Looking at Neighbors

When you start a machine learning project, one of the first questions is which kind of model to use. There are many approaches to choose from, and each one reasons about data in a different way. This article focuses on one of them, the Nearest Neighbors model, which is a useful first model to understand because its core idea can be stated in a single sentence.

What do you think happens?

Suppose you have five stored examples, each a single number with a label, and a new number arrives. How would you decide which label to give it?

  • Use the label that appears most often in the whole dataset

  • Look at the stored examples closest to the new number and use their labels

  • Average all the stored numbers and pick the label of the average

Reveal answer
Answer:

Look at the stored examples closest to the new number and use their labels

This is the core idea of the Nearest Neighbors model. The name describes the approach: to make a decision about a new data point, the model looks at the existing data points that are nearest to it.

What the Name Refers To

The Nearest Neighbors model is one of the models you can choose from when you are deciding which machine learning approach fits a problem. Its name describes its core idea. To reason about a new data point, the model does not build a complicated set of rules. Instead, it compares the new point with the stored examples and focuses on the ones that are nearest to it. The stored examples that are close to the new point are the ones that matter for the decision.

Definition

The Nearest Neighbors model looks at the stored examples closest to a new data point to reason about it.

The word neighbors in the name is the key. A neighbor here is a stored example that sits close to the new point. The model's reasoning is therefore local: it cares about what is near the new point rather than about the whole dataset taken as one block. This is why the model is described in terms of nearby existing examples rather than in terms of a formula learned from all the data.

Tracing a Lookup Step by Step

The best way to see the model's idea is to follow one lookup from start to finish. The example below is an original illustration, not taken from the source. It uses a tiny dataset of single numbers, each paired with a label. The stored examples are the pairs 1.0 red, 2.5 blue, 4.0 red, 7.5 green, and 9.0 green. A new query value of 3.2 arrives, and the model must find the stored example closest to it. Real problems usually involve many measurements per example, but the logic of comparing and picking the closest one is the same.

Query 3.2Distance to 1.0 is2.2Distance to 2.5 is0.7Distance to 4.0 is0.8Distance to 7.5 is4.3Distance to 9.0 is5.8Nearest: 2.5 (blue)comparecomparecomparecomparecompareselect smallest
What happens, in order, when the new value 3.2 arrives: the distance from the query to each stored value is computed, and the single closest stored example is selected.

Read the diagram from top to bottom. The query is compared with every stored value, one at a time. Each comparison produces a distance, which here is simply how far apart the two numbers are on the number line. The distances are 2.2, 0.7, 0.8, 4.3, and 5.8. The smallest distance is 0.7, which belongs to the stored value 2.5. That stored example is the nearest neighbor, and its label blue is the one the model relies on for this query. Notice that 4.0 is the second closest with distance 0.8, but the model only needs the single nearest example in this version of the lookup.

python
Output (expected)
2.5 blue 0.7
Notes on the code
  • examples is the stored dataset: each entry pairs a value with its label
  • query is the new data point that needs a decision
  • the first stored example is used as the starting candidate
  • abs(value - query) measures how far a stored value is from the query
  • the loop replaces the candidate whenever it finds a smaller distance
  • the final candidate is the nearest stored example
Code walkthrough
Walk through the lookup in the order the code runs. The data is stored once as a list of pairs, and nothing else is built from it. The query is set to 3.2. The starting candidate is the first stored example, so its value and label, and its distance to the query, are recorded. The loop then visits every stored example. For each one, it computes the distance, and if that distance is smaller than the best distance found so far, it replaces the candidate. When the loop finishes, the candidate holds the stored example closest to the query. The print statement reports the value 2.5, the label blue, and the distance 0.7, rounded to two places.
  1. 1
    Store the examples as a list of value and label pairs
  2. 2
    Set the new query value to 3.2
  3. 3
    Record the first stored example as the current best candidate
  4. 4
    Visit each stored example and compute its distance to the query
  5. 5
    Replace the candidate whenever a smaller distance is found
  6. 6
    Print the nearest stored value, its label, and its distance

Where the Model Keeps Its Knowledge

A useful way to understand the model is to compare what it does before a query with what it does during a query. Before any new point arrives, the model holds only the stored examples. Nothing has been measured against the new point yet. When a query comes in, the model computes its distance to the stored examples and uses the nearest ones. In this sense, the stored examples are not just input to a separate learning step; they are the model's working knowledge at the moment of prediction.

5 stored examplesno query yet5 distances computedquery 3.2 arrivedquery arrives
Before the query, the model holds five stored examples. When the query 3.2 arrives, distances are computed against all five stored examples.

Because the stored examples are consulted directly when a prediction is made, the set of examples is central to everything the model does. The source describes the model as a fit for small datasets, and the next section explains why that matters.

Why Small Datasets and Baselines Fit

The source identifies small datasets as the setting where this model is a good fit. When a dataset has relatively few examples, a model that works directly from those examples is practical to try, and it gives you a clear starting point before you commit to more complicated approaches. The source states this suitability without giving a size threshold, so you should judge what counts as small for your own problem.

A baseline is a simple reference model. Its job is not necessarily to be the final choice, but to give you a point of comparison. If a more complex model cannot clearly beat a simple baseline, that is useful information about the problem and about the extra complexity. The source describes the Nearest Neighbors model as good to use as a baseline, which means it is a sensible first model to build and measure before you move on to others.

Why the Reasoning Is Easy to Explain

The idea behind the name is simple to describe to someone who has never studied machine learning. A new point is compared with the stored examples, and the ones closest to it are the ones that matter. Because the reasoning can be stated in one sentence, the model is easy to explain to colleagues, stakeholders, or anyone who needs to understand why a prediction was made. This explainability is one of the reasons the source recommends it as an early model.

In the lookup from the previous section, the prediction blue can be traced back to one specific stored example, the value 2.5 with label blue. A person who wants to know why the model made that call can point to that exact row in the stored data, see how close it was to the query, and check the reasoning themselves. The explanation is the example, not a hidden set of learned weights.

Mistakes Beginners Make With Nearest Neighbors

  • Common mistake

    Assuming the model learns a formula from the data before a query arrives

    Why it fails

    The model works from the stored examples at prediction time. The stored examples are the model's knowledge, so there is no separate formula to inspect in the same way.

    Do this instead

    Think of the model as the stored examples plus the rule of picking the nearest ones.

  • Common mistake

    Treating the nearest example as always correct because it is closest

    Why it fails

    Closeness is only one piece of information. The source describes the model as a simple reference point, not as a guarantee of the right answer.

    Do this instead

    Use the model as a baseline and measure how well it performs before trusting it.

  • Common mistake

    Choosing the Nearest Neighbors model for a very large dataset because it is simple

    Why it fails

    The source identifies small datasets as the setting where this model is a good fit, so its suitability is tied to the size of the data.

    Do this instead

    Use it as a first model on small data, and judge the size of your own dataset before relying on it.

  • Common mistake

    Forgetting that the baseline exists to be compared against, so a more complex model is judged only in isolation

Practice: Find the Nearest Stored Example

EASY

Using the same stored examples from the lookup, the pairs 1.0 red, 2.5 blue, 4.0 red, 7.5 green, and 9.0 green, find the nearest stored example for a new query value of 6.0. List the distance to each stored value, then name the nearest example and its label.

Hints
  • Distance here is the absolute difference between a stored value and the query
  • Compute the distance to all five stored values before deciding
  • The nearest stored value is the one with the smallest distance, and its label is the answer

What do you think happens?

For the query 6.0, which stored example is nearest?

Reveal answer
Answer:

7.5 green

The distances are 5.0 to 1.0, 3.5 to 2.5, 2.0 to 4.0, 1.5 to 7.5, and 3.0 to 9.0. The smallest distance is 1.5, which belongs to 7.5 with label green.

Key Takeaways

  • The Nearest Neighbors model reasons about a new data point by looking at the stored examples closest to it, and its name describes that core idea.
  • A lookup computes the distance from the query to each stored example and selects the nearest one, as the Python trace showed for the value 3.2.
  • The source describes the model as suitable for small datasets, judged for your own problem since no size threshold is given.
  • The model is useful as a baseline, a simple reference point against which more complex models can be compared.
  • Because its reasoning points to specific stored examples, the model is easy to explain to colleagues and stakeholders.

Related concepts

More concepts starting with M