Implementation of K-Nearest Neighbors (KNN)
Algorithm
Aim
To implement the K-Nearest Neighbors (KNN) algorithm for classification using Python.
Algorithm
- Choose the value of K (number of nearest neighbors).
- Calculate the distance between the test data point and all training data points.
- Sort the distances in ascending order.
- Select the K closest data points.
- Perform majority voting among the K neighbors.
- Assign the class label with maximum votes to the test data point.
Python Implementation
import math
from collections import Counter
def euclidean_distance(p1, p2):
dist = 0
for i in range(len(p1)):
dist += (p1[i] - p2[i]) ** 2
return [Link](dist)
def knn(train_data, train_labels, test_point, k):
distances = []
for i in range(len(train_data)):
d = euclidean_distance(train_data[i], test_point)
[Link]((d, train_labels[i]))
[Link](key=lambda x: x[0])
k_neighbors = distances[:k]
labels = [label for _, label in k_neighbors]
return Counter(labels).most_common(1)[0][0]
train_data = [[1,2],[2,3],[3,4],[6,7],[7,8]]
train_labels = ['A','A','A','B','B']
test_point = [4,5]
k = 3
print("Predicted Class:", knn(train_data, train_labels, test_point, k))
Output
Predicted Class: A
Conclusion
The KNN algorithm successfully classifies the test data point based on the majority class of its
nearest neighbors.