MACHINE LEARNING • LESSON 10

Build KNN With Python

Now let's build a real KNN classification model in Python. We will use scikit-learn to train the model, give it a new data point, and make a prediction.

THE SIMPLEST IDEA

Give KNN examples, choose K, then ask it to classify a new example.

Python and scikit-learn handle the distance calculations and neighbor selection for us. We mainly need to prepare the data, choose K, train the model, and make a prediction.

01

What Are We Building?

We will build a simple model that predicts whether a student will Pass or Fail.

We will use two features:

FEATURE 1 Study Hours

Number of hours the student studies.

FEATURE 2 Attendance

Student attendance percentage.

MODEL GOAL Predict Pass or Fail
02

Step 1 — Import KNN

First, we import the KNN classifier from scikit-learn.

from sklearn.neighbors import KNeighborsClassifier
What does this do?

It imports KNeighborsClassifier, the scikit-learn class we will use to create our KNN classification model.

You do not need to implement the distance formula yourself. Scikit-learn provides the KNN algorithm for us.
03

Step 2 — Create the Training Data

Next, we create some example students. Each row contains the student's features.

X = [
    [2, 60],
    [3, 65],
    [4, 70],
    [5, 80],
    [6, 85],
    [7, 90]
]

Each row represents one student.

STUDENT STUDY HOURS ATTENDANCE
1 2 60%
2 3 65%
3 4 70%
4 5 80%
5 6 85%
6 7 90%

Notice that X contains only the features. It does not contain the Pass/Fail answer.

04

Step 3 — Create the Labels

Now we tell the model the correct class for each training example.

y = [
    "Fail",
    "Fail",
    "Fail",
    "Pass",
    "Pass",
    "Pass"
]

The first value belongs to the first row in X, the second value belongs to the second row, and so on.

FEATURES [5, 80]
LABEL Pass
X = input features. y = the correct output label.
05

Step 4 — Choose K

Now we decide how many neighbors KNN should look at.

For this simple example, let's choose:

K 3

Use the 3 nearest students.

model = KNeighborsClassifier(n_neighbors=3)
What does n_neighbors=3 mean?

It tells KNN to use the three closest training examples when making a classification.

06

Step 5 — Train the Model

Now we give the training features and labels to the model.

model.fit(X, y)

This connects the feature values in X with their known labels in y.

X Features

Study hours + attendance

+
y Labels

Pass + Fail

model.fit() KNN Model

Ready to classify new data.

With KNN, this does not mean that the algorithm creates a complicated equation. It keeps the training examples available so it can compare new points with them.

07

Step 6 — Give the Model a New Student

Now suppose a new student has:

NEW STUDENT [5, 82]

5 study hours and 82% attendance.

We don't know whether this student will Pass or Fail. That is what the model needs to predict.

new_student = [[5, 82]]
08

Step 7 — Make the Prediction

We use predict() to ask the model for the class of the new student.

prediction = model.predict(new_student)

print(prediction)

The output will be:

['Pass']
MODEL PREDICTION Pass

The three nearest training examples produce a majority Pass vote.

09

What Happened Behind predict()?

Although Python gives us the answer with one line, KNN is conceptually doing the same process you learned on the previous pages.

NEW STUDENT [5, 82]
DISTANCE Compare with X
K = 3 Closest 3
VOTE Majority class
RESULT Pass
10

Complete Python Code

Now put all the pieces together.

from sklearn.neighbors import KNeighborsClassifier

# Training features
X = [
    [2, 60],
    [3, 65],
    [4, 70],
    [5, 80],
    [6, 85],
    [7, 90]
]

# Training labels
y = [
    "Fail",
    "Fail",
    "Fail",
    "Pass",
    "Pass",
    "Pass"
]

# Create the KNN model
model = KNeighborsClassifier(n_neighbors=3)

# Train the model
model.fit(X, y)

# New student
new_student = [[5, 82]]

# Make prediction
prediction = model.predict(new_student)

print(prediction)
['Pass']
11

Understand the Five Important Lines

You don't need to memorize the entire program. Understand these five lines first.

KNeighborsClassifier(...) Creates the KNN classifier.
n_neighbors=3 Sets K to 3.
model.fit(X, y) Gives the model the training data.
model.predict(...) Asks the model to classify new data.
print(prediction) Displays the prediction.
12

A Second Small Example

The same idea can be used for something completely different.

Suppose we want to classify fruits as Apple or Orange using weight and size.

from sklearn.neighbors import KNeighborsClassifier

X = [
    [150, 7],
    [160, 7],
    [170, 8],
    [180, 8],
    [190, 9]
]

y = [
    "Apple",
    "Apple",
    "Apple",
    "Orange",
    "Orange"
]

model = KNeighborsClassifier(n_neighbors=3)

model.fit(X, y)

new_fruit = [[175, 8]]

prediction = model.predict(new_fruit)

print(prediction)
SAME KNN IDEA Find neighbors → Vote → Predict

Only the features and labels changed.

REMEMBER THIS

Building KNN in Python is mostly about preparing the data and using the KNN classifier correctly.

Put your features in X, your labels in y, choose K, create the classifier, fit it with the training data, and use predict() for a new data point.

X + y KNN Model New Data Prediction
QUICK CHECK

Check Your Understanding

Which Python library are we using? Scikit-learn.
What does X contain? The input features used by the model.
What does y contain? The correct class labels for the training examples.
What does n_neighbors=3 mean? KNN uses the three nearest neighbors for a prediction.
What does model.fit(X, y) do? It provides the training examples and their labels to the KNN model.
What does model.predict() do? It predicts the class of new data using the nearest neighbors.
NEXT TOPIC

Understand the Python Code

Now that the complete KNN program works, let's break the Python code down line by line and understand exactly what each part is doing.