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.
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.