Build and train a K-Nearest Neighbors model, then evaluate its performance using actual test data. Learn how predictions align with real-world outcomes and explore methods of measuring model accuracy.
Key Insights
- Use a K-Nearest Neighbors classifier with three neighbors to train a machine learning model.
- Fit the model using provided training data (x-train) along with its known classifications.
- Confirm the accuracy of model predictions by comparing them against the actual results from test data, with further evaluation methods detailed in the subsequent section.
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 create our model, train it, check its predictions. We'll call it k and n model and it's the k neighbors classifier and we pass it n neighbors of 3. Run that code block and now we'll train it or the other term for that is fit it. Fit it to the data and we'll give it the X-train data and the answers for the X-train data.
And our model is trained. What we get back is a model. Now let's actually take a look at these predictions.
We'll say I want you to now do, I want you to now give me some predictions based on some training, sorry, some test data. Hey, okay, given these flowers without the answer, what is your prediction as to where each one fits? Let's print out model predictions and it's those predictions and the correct answers and we'll make a list out of y test to do that. All right, let's check that out.
It seems pretty good. 1,0, 0,0, 2,2, 1. 1,0, 0,0, 2,2, 1. Well, there's 30 of these. We can eyeball it eventually, but let's see how good it actually did.
We'll measure it in many different ways in our next section.