Professional Documents
Culture Documents
ML KNN Fruit Assignment
ML KNN Fruit Assignment
#Hamza Ashfaque
#Section 6D
#Machine Learning Assignment
import math
fruit = []
#calculates Euclidean Distance and puts them in a tuple with Fruit Type
for i in range(len(fruit_type)):
e_length = fruit_length[i] - l
e_width = fruit_width[i] - w
e_red = fruit_color_red[i] - r
e_green = fruit_color_green[i] - g
e_blue = fruit_color_blue[i] - b
e_distance = math.sqrt(e_length**2 + e_width**2 + e_red**2 + e_green**2 + e_blue**2)
fruit.append((fruit_type[i], e_distance))
#Sorts the tuple based on euclidean distance and counts the number of fruits based on range
of k
euclidean_distances = lambda fruit: fruit[1]
fruit.sort(key=euclidean_distances)
mango = 0
banana = 0
for i in range(k):
x = fruit[i][0]
if x == 'banana':
banana = banana + 1
elif x == 'mango':
mango = mango + 1