Skip to main content
Colin Jaffe/3 min read

Visualizing K-Nearest Neighbors with Simple Data Points

KNN Visualization Essentials

2D Scatter Plot

Plot features on X and Y, color by class — intuition for classification.

Decision Boundary

Use a meshgrid + predict to draw the boundary KNN learns.

Vary k

Plot side-by-side at k=1, 5, 25 — see overfitting vs smoothing.

Highlight Neighbors

Draw lines from a query point to its k nearest training points.

Master Machine Learning at Noble Desktop

Noble Desktop's Python Machine Learning Bootcamp covers scikit-learn, Keras, neural networks, and applied ML.

This lesson is a preview from our Data Science & AI Certificate Online (includes software) and Python Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.

Visualize and plot sample data points to understand k-nearest neighbors classification. Watch this tutorial to learn the key concepts and techniques.

Let's look at some data points. And these are just made-up data points. We'll look at some actual data in the next section.

So, in the first section of part three, we're just looking at some data that we can visualize and see the numbers for and plot and understand how k-nearest neighbors works. So here we have some X and Y values, and these are just coordinates, and again, these could be like weight and height or something else.

So then we have these classes, these categories. If an X is four and Y is 21, the class is zero. When X is five and Y is 19, the class is zero.

When X is 10 and Y is 24, the class is one. So this is the kind of data we'll feature and put in. We'll essentially give our model this data for X_train and this data for y_train, right? Here's the answer to those X and Y points.

What we'll actually give it, and we should visualize this, is tuples of this data, not X and Y separately, but a list where we've zipped up X and Y. Let's take a look at what that looks like. Zip is a Python function that takes the first item from the two arrays and puts them in a tuple. Then it takes the second item from both arrays and puts that into a tuple.

You can imagine a zipper zipping up two halves, and then they interleave. That's what's happening here. So these are the kind of data points.

X is four, Y is 21. X is five, Y is 19 that we'll give to our k-nearest neighbors. Let's plot those points.

We're going to do a little bit of graphing. Not too much, I promise. We can have a scatter plot.

PLT is, of course, pyplot. And we'll scatter X and Y, and we'll set a color for it of the classes. These zero and one, the answers.

And then it will give our color to zero. Any one of these X and Y points that is a zero will get one color. And if it has a one, it will get a different color.

And that's what the C equals argument is. Then we can say, pyplot, show us plot. And here we are.

So this is our training data. And again, it's very sparse. It's very made up.

But this is the kind of graph that k-nearest neighbors is looking at. And that's what we'll be looking at visually, and mathematically.