0% found this document useful (0 votes)
36 views4 pages

AI Lab10

This lab report describes two activities using K-Nearest Neighbors (KNN) classification. The first activity implements a KNN classifier from scratch to classify an unknown point. The second activity uses Scikit-Learn's KNN classifier on the Iris dataset, making a prediction on new data and reporting the test score. Overall, the report details experiments with KNN classification algorithms on sample datasets.

Uploaded by

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

AI Lab10

This lab report describes two activities using K-Nearest Neighbors (KNN) classification. The first activity implements a KNN classifier from scratch to classify an unknown point. The second activity uses Scikit-Learn's KNN classifier on the Iris dataset, making a prediction on new data and reporting the test score. Overall, the report details experiments with KNN classification algorithms on sample datasets.

Uploaded by

Maryam Khansa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CSC462-Artificial Intelligence

Lab Report 09-Linear Regression

Name MUSKA IJAZ

Registration FA19-BEE-163
Number
Class/Section
BCE-7B

Instructor’s Name Sir ALI RAZA SHAHID

Lab Assessment

Pre-Lab /1

In Lab /5

Critical Analysis /4 /10


In-Lab Activities:
Activity 1:

Code:
import math

def classifyAPoint(points,p,k=3):
'''
This function finds the classification of p using
k nearest neighbor algorithm. It assumes only two
groups and returns 0 if p belongs to group 0, else
1 (belongs to group 1).

Parameters -
points: Dictionary of training points having two keys - 0 and 1
Each key have a list of training data points belong to that

p : A tuple, test data point of the form (x,y)

k : number of nearest neighbour to consider, default is 3


'''

distance=[]
for group in points:
for feature in points[group]:

#calculate the euclidean distance of p from training points


euclidean_distance = math.sqrt((feature[0]-p[0])**2 +(feature[1]-p[1])**2)

# Add a tuple of form (distance,group) in the distance list


distance.append((euclidean_distance,group))

# sort the distance list in ascending order


# and select first k distances
distance = sorted(distance)[:k]

freq1 = 0 #frequency of group 0


freq2 = 0 #frequency og group 1

for d in distance:
if d[1] == 0:
freq1 += 1
if d[1] == 1:
freq2 += 1
return 0 if freq1>freq2 else 1

# driver function
def main():

# Dictionary of training points having two keys - 0 and 1


# key 0 have points belong to class 0
# key 1 have points belong to class 1

points = {0:[(1,12),(2,5),(3,6),(3,10),(3.5,8),(2,11),(2,9),(1,7)],
1:[(5,3),(3,2),(1.5,9),(7,2),(6,1),(3.8,1),(5.6,4),(4,2),(2,5)]}

# testing point p(x,y)


p = (2.5,7)

# Number of neighbours
k=3

print("The value classified to unknown point is: {}".\


format(classifyAPoint(points,p,k)))

if __name__ == '__main__':
main()

Activity 2:

Code:

# Python program to demonstrate


# KNN classification algorithm
# on IRIS dataset

from sklearn.datasets import load_iris


from sklearn.neighbors import KNeighborsClassifier
import numpy as np
from sklearn.model_selection import train_test_split

iris_dataset=load_iris()

X_train, X_test, y_train, y_test = train_test_split(iris_dataset["data"], iris_dataset["target"],


random_state=0)
kn = KNeighborsClassifier(n_neighbors=1)
kn.fit(X_train, y_train)

x_new = np.array([[5, 2.9, 1, 0.2]])


prediction = kn.predict(x_new)

print("Predicted target value: {}\n".format(prediction))


print("Predicted feature name: {}\n".format
(iris_dataset["target_names"][prediction]))
print("Test score: {:.2f}".format(kn.score(X_test, y_test)))

Critical Analysis:

You might also like