My K Nearest Neighbor Algorithm...without using SKLearn...

I created a KNN Algorithm by making my own data set with tuples storing the x-value, y-value, and color of 9 points, asking the user for the x and y-values of their data point and how many dots are being taken into account, then determining the distance between the point and the ones in the data set. I stored these distances in a dictionary, with the points being the keys and the distances being the values, and sorted it from the lowest distance to the highest. After, I totaled up the number of points with each color out of the inputted number of nearest points. Finally, I classified the inputted point based on which of the colors was the most prominent out of those points.


#Importing import numpy as np import math as math import operator #Data data = [(1, 2, "red"), (10, 8, "blue"), (5, 6, "green"), (4, 1, "red"), (8, 8, "blue"), (3, 1, "green"), (1, 2, "red"), (8, 10, "blue"), (6, 5, "green")] #Store Points(X/Y Values) x = int(input("What's your x-value?")) y = int(input("What's your y-value?")) #Store # of points being taken into account(K Value) k= int(input("How many closest dots do you want to take into account?")) point = (x, y) print(point) #Distance Formula def distance(x2, y2): return math.sqrt((x2-x)**2+(y2-y)**2) #Find/Store Distances of All Points(Loop/Dictionary) knndict = {} for n in data: z = distance(n[0],n[1]) print(z) knndict[n] = z #Ordering The Dictionary(Least Distance) knndicts = sorted(knndict.items(), key=operator.itemgetter(1)) print(knndicts) #Voting Process green = 0 blue = 0 red = 0 for n in range(0, k, 1): if knndicts[n][0][2] == "green": green += 1 if knndicts[n][0][2] == "blue": blue += 1 if knndicts[n][0][2] == "red": red += 1 print(green) print(blue) print(red) #Determining the Color if green > blue and green > red: print("Your dot's color has been classified as green!") elif blue > green and blue > red: print("Your dot's color has been classified as blue!") elif red > green and red > blue: print("Your dot's color has been classified as red!") else: print("Unfortunately, your dot could not be classified. Try picking a different number of closest dots next time.")

My K Nearest Neighbor Algorithm so far...without SKLearn...

Nowhere near done but I'll get there eventually...

#Importing import numpy as np import matplotlib.pyplot as plt import math as math #Data data = [(1, 2, "red"), (10, 8, "blue"), (5, 6, "green"), (4, 1, "red"), (8, 8, "blue"), (3, 1, "green"), (1, 2, "red"), (8, 10, "blue"), (6, 5, "green")] #Points x = int(input("What's your x-value?")) y = int(input("What's your y-value?")) point = (x, y) print(point) #Distance for i in range(0, 9, 1) def distance(x, y, x2, y2): return sqrt(x2-x)**2+(y2-y)**2
1