You are on page 1of 2

#70066795

#Hamza Ashfaque
#Section 6D
#Machine Learning Assignment

import math

#Dataset of fruits mango and banana


fruit_type = ['mango', 'mango', 'banana', 'banana', 'mango']
fruit_length = [22, 23, 17, 15, 20]
fruit_width = [17, 16, 4.3, 4.6, 13]
fruit_color_red = [255, 244, 227, 211, 240]
fruit_color_green = [130, 132, 207, 199, 123]
fruit_color_blue = [67, 64, 87, 71, 54]

#input entry on which KNN will perform


print("Enter data about fruit")
l = float(input("Enter length: "))
w = float(input("Enter width: "))
print("Enter rgb color values")
r = int(input("red: "))
g = int(input("green: "))
b = int(input("blue: "))
k = int(input("Enter number of k: "))

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

if banana > mango:


fruit_type = 'banana'
else:
fruit_type = 'mango'

print("Your fruit is " + fruit_type)

You might also like