Visualizing Predictions with KNN on Flower Data

Implement and visualize a simple k-nearest neighbors classifier to predict a new data point's class.

Understand how k-nearest neighbors classifies data points by analyzing their nearest neighbors. Gain insights into visualization techniques that highlight prediction accuracy.

Key Insights

  • Demonstrates how k-nearest neighbors (k-NN) classifies a new data point by analyzing the three closest data points and selecting the majority class, resulting in a prediction of class "1" (yellow).
  • Shows the process of appending the new predicted classification into an existing classes list, updating it to reflect the newly predicted point.
  • Highlights the importance of visualizing the prediction results with a scatter plot to verify the accuracy of predictions by illustrating the proximity of the newly classified data point to its neighbors.

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.

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.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Machine Learning

Master Machine Learning with Hands-on Training. Use Python to Make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram