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.

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.

Let's create our model, train it, and check its predictions. We'll call it `knn_model`, and it's the K Neighbors Classifier, and we pass it `n_neighbors=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 by giving it the `X_train` data and the corresponding 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 give me some predictions based on some test data. Hey, okay, given these flowers without the answers, what is your prediction as to where each one fits? Let's print out the model predictions, the predicted values, 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 well it actually performed.

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

Build practical, career-focused machine learning skills through hands-on training designed for beginners and professionals alike. Learn fundamental tools and workflows that prepare you for real-world projects or industry certification.