Visualize and classify new data points using Python's K-Nearest Neighbors (KNN) model. See step-by-step how to add, plot, and predict the classification of previously unclassified data.
Key Insights
- Visualize new data points by appending them to existing x and y lists, updating the scatter plot, and labeling the points clearly to distinguish them from existing data.
- Create a temporary copy of classification data in Python to assign a unique class and visually differentiate the unclassified data point in the scatter plot.
- Use the KNN model's predict method to classify new data points, converting individual data points into tuples and interpreting the resulting array-based predictions accurately.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
Okay, we're going to test the model, but first we're going to visualize what it's doing. We got this x and y new data points. Let's add them to our x and y. We'll say x dot append new x and y dot append new y. Make sure to run that.
Now we can reassign, we can redo our scatterplot. And it's already all set up. Here we just need to run this.
The only difference, we're still doing a scatterplot 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 scattered 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, this is just a temporary 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 class this copy, 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 little, same thing, but we're going to scatter it with x and y, which have had the new data point appended. And with our 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, 919.
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 an x test here. But we only have one thing we're doing, our data point. Go predict that data point and set that value.
Prediction equals this. And then let's look at prediction. Oh, it'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 it's like, okay, well, here's the actual item to compare it.
So let's add that prediction to our list and plot it in the next section.