Have you ever made a decision just by looking around and trusting your instinct! Well, That’s how K-Nearest Neighbors (KNN) works.
In this post, I’ll walk you through a visual, beginner-friendly KNN demo using Python. We’ll plot data, add a new point.
What is KNN
KNN is one of the simplest machine learning algorithms but also one of the most intuitive:
It stores all known data
When asked to make a prediction, it finds the K closest known data points
Then, it "votes" on what the new data point should be
Think of it like this:
If your 5 closest friends love Chocolate Burgers, well, chocolate Burgers! Does anyone like Chocolate Burger's here?
LOL
Chances are you will too, or that's how KNN will assume.
Why it's Super Cool
- No need for the training step, it's Lazy Learning.
- Works really well on small datasets.
- It's super great for visual understanding of AI behaviour
Python Code (Visual Demo)
Let me show some code for KNN, we'll create a synthetic dataset of two classes, plot them and predict a new data point
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.neighbors import KNeighborsClassifier
# let's create a 2D Dataset
data, labels = make_classification(n_samples=100,n_features=2,n_informative=2, n_redundant=0,n_clusters_per_class=1)
# Split the data into two classes
class_0 = data[labels == 0]
class_1 = data[labels == 1]
# Plot the existing data
plt.scatter(class_0[:, 0], class_0[:, 1], color='blue', label='Class 0')
plt.scatter(class_1[:, 0], class_1[:, 1], color='red', label='Class 1')
# Define a new plot
new_point = [[0.5, -0.5]]
plt.scatter(new_point[0][0], new_point[0][1], color='green', label='New Point', s=100, edgecolors='black')
# Fit the model and start predicting
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(data, labels)
predicted = knn.predict(new_point)
# Finally Annotate
plt.title(f"Predicted Class: {predicted[0]}")
plt.legend()
plt.show()
This code works, feel free to run it, tweak the dataset size or n_neighbors, and experiment with different points. It's a great way to see how AI makes decisions.
Output:
So why KNN matter, it's not just acadamic,it's used in:
- Recommendation Systems
- Image Classifier
- Anomaly Detection
The beauty of KNN lies in simplicity which lays the foundation for understanding more complex AI systems.
Want to see more? I build AI agents, storytelling bots, and fun Python experiments that feel alive.
Follow me here on Dev.to or check out SiteEncoders where we turn cool AI into real products.
Let me know your thoughts below or share your favorite "super smart" algorithm!
Top comments (2)
Gr8 content!
Some comments may only be visible to logged-in visitors. Sign in to view all comments.