Loading W Code...
5
Topics
sklearn
Implementation
Indian Context
Bollywood Movie Recommendations, Restaurant Finder, Similar Product Discovery on Flipkart
Concept Level: Beginner
# KNN: THE SIMPLEST CLASSIFIER
import numpy as np
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import make_classification
# Create sample data - like food preferences
np.random.seed(42)
X, y = make_classification(
n_samples=100, n_features=2, n_redundant=0,
n_informative=2, n_clusters_per_class=1, random_state=42
)
# Train KNN classifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X, y) # "Training" = just storing the data!
# New point to classify
new_point = np.array([[0, 0]])
# Find the 5 nearest neighbors
distances, indices = knn.kneighbors(new_point)
print("5 Nearest Neighbors:")
print(f"Distances: {distances[0]}")
print(f"Their labels: {y[indices[0]]}")
print(f"Prediction: Class {knn.predict(new_point)[0]}")
# Visualize
plt.figure(figsize=(10, 8))
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='RdYlBu', edgecolor='black', s=50)
plt.scatter(new_point[0, 0], new_point[0, 1], c='green', marker='*', s=300,
label='New Point', edgecolor='black', linewidth=2)
# Highlight nearest neighbors
neighbors = X[indices[0]]
plt.scatter(neighbors[:, 0], neighbors[:, 1], facecolors='none',
edgecolors='green', s=200, linewidth=2, label='5 Nearest Neighbors')
# Draw lines to neighbors
for neighbor in neighbors:
plt.plot([new_point[0, 0], neighbor[0]],
[new_point[0, 1], neighbor[1]],
'g--', alpha=0.5)
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('KNN: Finding Nearest Neighbors')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()