Understanding the k-Nearest Neighbors Model

Evaluate the trained model's predictions using various metrics.

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.

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.

We'll measure it in many different ways in our next section.

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