Understand the Python Code
We already built a KNN model. Now let's understand what every important line of the Python code does and how all the lines work together to make a prediction.
Don't memorize the code. Understand what each part is responsible for.
The KNN program has a simple flow: create the data, create the labels, choose K, create the model, train it, give it new data, and make a prediction.
The Complete Code
First, let's look at the complete program before breaking it into individual parts.
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']
Line 1 — Import KNN
from sklearn.neighbors import KNeighborsClassifier
This line imports the KNN classification algorithm from scikit-learn.
Without this import, Python would not know what
KNeighborsClassifier means.
We are telling Python: "Bring me the KNN classifier so I can use it."
Lines 3–10 — Create X
X = [
[2, 60],
[3, 65],
[4, 70],
[5, 80],
[6, 85],
[7, 90]
]
X contains the input features
used by KNN.
Each inner list represents one student. The first value is study hours and the second value is attendance.
Lines 13–20 — Create y
y = [
"Fail",
"Fail",
"Fail",
"Pass",
"Pass",
"Pass"
]
y contains the correct labels
for the training examples.
The positions of X and y
correspond to each other.
Line 23 — Create the Model
model = KNeighborsClassifier(n_neighbors=3)
This creates a KNN classifier and stores it in a
variable called model.
The important part is:
K = 3
This means KNN will look at the 3 nearest training examples when making a prediction.
Creating the model does not make a prediction yet. We are only configuring the KNN algorithm.
Line 26 — Train the Model
model.fit(X, y)
fit() gives the model the training
features and their known labels.
Study hours + attendance
Pass + Fail
Ready to classify new data.
A common beginner mistake is thinking that
fit() means KNN creates a mathematical
equation like linear regression.
KNN is different. It keeps the training examples available so it can compare a new point with them.
Line 29 — Create New Data
new_student = [[5, 82]]
This is a new student whose result is unknown.
5 study hours and 82% attendance.
Notice the double brackets:
[[5, 82]]
The outer list represents the collection of data points, while the inner list represents one data point with two features.
Line 32 — Make the Prediction
prediction = model.predict(new_student)
This asks the trained KNN model to classify the new student.
Behind the scenes, KNN conceptually does this:
Line 34 — Print the Result
print(prediction)
This simply displays whatever prediction was returned by the model.
['Pass']
The result is inside a list because
predict() can predict multiple data points
at the same time.
Why Is predict() So Important?
The entire purpose of building the model is eventually to use it on data it has not been given a label for.
Training examples with known answers.
Uses nearby examples.
No label yet.
Predicted class.
One Complete Example
Let's trace one prediction from beginning to end.
Label: Pass
Label: Pass
Label: Fail
Now we give KNN:
Suppose these three are the nearest neighbors. Their labels are:
2 Pass votes beat 1 Fail vote.
The Code in Plain English
If the Python code feels confusing, translate it into normal language:
What You Should Remember
X
Input features
y
Correct labels
K
Number of neighbors
fit()
Provide training data
predict()
Predict new data
KNN Python code is just the KNN process written in Python.
You prepare X and y, choose K, create the model, call fit(), provide new data, and call predict(). The library handles the distance calculations and neighbor voting for you.