Skip to main content
Colin Jaffe/2 min read

Visualizing Predictions with KNN on Flower Data

ML Best Practices

0/4
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.

Implement and visualize a simple k-nearest neighbors classifier to predict a new data point's class. Watch this tutorial to learn the key concepts and techniques.

Let's wrap up our simple version of a K-Nearest Neighbors before we get to a more complex one in the next section. We've got our classes list. We're using classes_copy, which has the two at the end, but let's append our actual new prediction to our actual class list.

And we'll append the integer version of our prediction at index zero. Again, because our prediction is an array of predictions, but we really just want one. And if we look at classes, there it is with our new prediction.

It's going to be a one. Now we can redo the scatter plot. Remember, this prediction is from our K-Nearest Neighbor, and it should be visualized.

It should be easy to see, like, hey, does it look like it's near, it's the color of its nearest neighbors or not? How did the prediction do? So the only change I'm going to make to this code—like classes have been updated to have the new data point, its prediction, rather, of the new data point—X and Y have the new data point in it. Let's change this to newly classified point and run it. And here's our newly classified point.

And K-Nearest Neighbors took a look at three nearest neighbors, and it said, okay, two of those three are the yellow, the one. So I'm going to make this prediction: this newly classified point is a one—a yellow. That's exactly what happened.

All right, in our next section, we're going to look at a proper and famous dataset of flowers and what species of flower a new flower is. And we're going to run some tests, and we're going to see how we do. All right, folks, I'll see you there.