Skip to main content
Colin Jaffe/3 min read

Visualizing and Predicting Classes with Scatterplot and KNN

KNN Visualization Workflow

1

Two Numeric Features

Pick two for scatter plot axes.

2

Color by Class

scatter(x, y, c=labels) — color points by their class.

3

Train KNN

KNeighborsClassifier(n_neighbors=5).fit(X, y).

4

Predict Grid

Predict over a grid of points to draw decision boundaries.

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 classify a new data point using the KNN model. Watch this tutorial to learn the key concepts and techniques.

Okay, we're going to test the model, but first we're going to visualize what it's doing. We have these new X and Y data points. Let's add them to our X and Y. We'll say X.append(new_x) and Y.append(new_y). Make sure to run that.

Now we can reassign, we can redo our scatter plot. And it's already all set up. Here we just need to run this.

The only difference, we're still doing a scatter plot of X and Y, but we're also adding a little bit of text to identify our unclassified new point. And since our new point is in the X and Y, it got plotted like the other ones. Here it is with its little tag.

This is our unclassified new point. Okay. Let's take a look now at giving it a unique class so that it looks a little different from all the others.

So first we'll make a temporary variable for our visualization, so we're going to make a copy of our classes using Python's built-in copy method on lists. And then we'll append a two. Again, this is just a different color to visualize our new item.

And if we could classes_copy, it's got that little two at the end now. That goes to the new data point. All right.

Let's redo our scatter plot now. Same thing, but we're going to scatter it with X and Y, which have had the new data point appended, and with our color argument C being classes_copy so that it has the new two at the end.

We do that, we get, here's the purples, here's the greens, and here's our unclassified new point, yellow. Okay. Now let's actually use our model to predict.

First we need a new tuple. We'll say data point, singular, is a tuple (new_x, new_y). And we see our data point. It's this tuple: (9,19).

All right. Now we're going to try to predict our new class. We'll use KNN's predict.

And it takes in a list, just like X_test here. But we only have one thing we're doing, our data point. Go predict that data point and save that value.

Prediction equals this. And then let's look at prediction. Oh, that's not the last thing here.

Let's put it down here. It's actually an array, which makes sense because what we get back is usually predictions, plural. So it's an array.

If we want to see the new prediction, we would say prediction at zero. All right. We can print out here, not prediction, but prediction at zero.

And again, the first one is like, hey, here's line one. Here's an array of one item, and here's the actual item to compare.

So let's add that prediction to our list and plot it in the next section.