0% found this document useful (0 votes)
4 views1 page

K-Nearest Neighbors Algorithm in Python

The document outlines the implementation of the K-Nearest Neighbors (KNN) algorithm for classification in Python. It details the steps involved in the algorithm, including calculating distances, sorting, and performing majority voting among neighbors. The provided Python code demonstrates how to classify a test data point, resulting in a predicted class of 'A'.

Uploaded by

singhsaurabh4039
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

K-Nearest Neighbors Algorithm in Python

The document outlines the implementation of the K-Nearest Neighbors (KNN) algorithm for classification in Python. It details the steps involved in the algorithm, including calculating distances, sorting, and performing majority voting among neighbors. The provided Python code demonstrates how to classify a test data point, resulting in a predicted class of 'A'.

Uploaded by

singhsaurabh4039
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like