By the end of this lab, you will be able to:
Use NumPy and scikit-learn to implement K-Nearest Neighbour classification
Visualise 2D data points and their classes
Make predictions using KNN and understand how k affects results
Run this only once in your environment.
pip install scikit-learn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
We’ll classify points based on their location in 2D space.
# Create and train model with k=3
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X, y)
# New point to classify
X_new = np.array([[5, 5]])
y_pred = model.predict(X_new)
print(f"Predicted class: {y_pred[0]}")
plt.figure(figsize=(6, 6))
for i in range(len(X)):
colour = 'blue' if y[i] == 0 else 'red'
plt.scatter(X[i][0], X[i][1], color=colour, s=80)
# Plot new point
plt.scatter(X_new[0][0], X_new[0][1], color='green', s=100, marker='X', label='New Point')
plt.title("KNN Prediction")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.grid(True)
plt.legend()
plt.show()
Try Different Values of k
Change n_neighbors to 1, 3, 5, etc.
How does the prediction change?
Which value of k seems most stable?
Add New Training Points
Manually add more points to X and y. Try to:
Add more class 0 and class 1 points in different areas
Make the classes overlap and observe what KNN does
Use Real Categories
Create a KNN classifier for:
“Is this fruit citrus?”
Features: has thick skin, tastes sour, round shape (1 or 0)
Labels: 1 = citrus, 0 = not citrus
Invent 8 fruits with these properties and classify a new one