0% found this document useful (0 votes)
9 views

Assignment No 2 AI

The document discusses implementing a K-Nearest Neighbors algorithm in Python. It includes functions for calculating Euclidean distance, finding the K nearest neighbors of a data point, and predicting the class by majority vote of neighbors. It then applies the KNN to the Iris dataset, compares results to scikit-learn's KNN, and investigates the effect of different values of K.

Uploaded by

Usama Mushtaq
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)
9 views

Assignment No 2 AI

The document discusses implementing a K-Nearest Neighbors algorithm in Python. It includes functions for calculating Euclidean distance, finding the K nearest neighbors of a data point, and predicting the class by majority vote of neighbors. It then applies the KNN to the Iris dataset, compares results to scikit-learn's KNN, and investigates the effect of different values of K.

Uploaded by

Usama Mushtaq
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
You are on page 1/ 4

ARTIFICIAL

INTELLIGENCE
CS307/AI202

ASSIGNMENT-02
Task: Implementing K-Nearest Neighbors (KNN)
Q 1: Implement the KNN algorithm in python. This should include functions for
calculating Euclidean distance between data points, getting the K nearest neighbors
of a data point, and predicting the class of a data point based on the majority class of
its K nearest neighbors.

import numpy as np

from collections import Counter

def euclidean_distance(x1, x2):

return np.sqrt(np.sum((x1 - x2) ** 2))

def get_neighbors(data, labels, point, k):

distances = np.array([euclidean_distance(point, x) for x in data])

sorted_indices = np.argsort(distances)

neighbors = data[sorted_indices[:k]]

labels_neighbors = labels[sorted_indices[:k]]

return neighbors, labels_neighbors


def predict_class(neighbors, labels_neighbors):

class_counts = Counter(labels_neighbors)

most_frequent_class, _ = class_counts.most_common(1)[0]

return most_frequent_class

# Example usage

data = np.array([[1, 2, 1], [1.5, 1.8, 1], [5, 8, 0], [8, 8, 0]])

labels = np.array([0, 0, 1, 1])

point = np.array([3, 5, 1])

k=2

neighbors, labels_neighbors = get_neighbors(data, labels, point, k)

predicted_class = predict_class(neighbors, labels_neighbors)

print("Predicted class:", predicted_class)

Q 2: Apply your KNN implementation on the Iris dataset. The Iris dataset includes 50
samples from each of three species of Iris flowers with four features measured from
each sample: the lengths and the widths of the sepals and petals.

• from sklearn import datasets


• iris = datasets.load_iris()
• X = iris.data
• y = iris.target

from sklearn import datasets


from sklearn.model_selection import train_test_split
iris = datasets.load_iris()
X = iris.data # Features
y = iris.target # Target labels

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


k=3
def predict(X_train, y_train, X_test, k):
predictions = []
for point in X_test:
neighbors, labels_neighbors = get_neighbors(X_train, y_train, point, k)
predicted_class = predict_class(neighbors, labels_neighbors)

2
predictions.append(predicted_class)
return np.array(predictions)

predicted_labels = predict(X_train, y_train, X_test, k)

# Calculate accuracy
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, predicted_labels)
print("KNN Accuracy:", accuracy)

Q 3: Compare the results of your KNN implementation with the KNN implemen-
tation from sklearn.

Q 4: Investigate the effect of the hyperparameter K (number of neighbors) on the


predictions. Try different values of K and observe how the predictions change.

• from sklearn.neighbors import KNeighborsClassifier


• knn = KNeighborsClassifier(n_neighbors=3)
• knn.fit(X, y)
• predictions = knn.predict(X)

3
4

You might also like