Finding closest data type using "n" number of nearest neighbors

Given a set of data (traindata) which assigns certain coordinates to a binary system of colors, this program uses this data to find the color of best fit for a coordinate (testdata) given by the user. Ultimately, any coordinate entered by the user should produce either "black" or "blue." Solved by appending the distances from each training coordinate to the testing coordinate to a list called "distances," and finding the minimum value of that list each time around. To avoid repetition in calculation of minimums, I replaced each value that had already been tested with float('inf').

import math traindata = [(1,1,"blue"), (2,2,"blue"), (3,3,"black"), (4,4,"blue"), (5,5,"black"), (4,3,"blue"), (4,10,"black"), (5,5,"black"), (6,2,"blue"), (1,7,"black"), (4,9,"blue"), (5,8,"blue")] testdata = [] testdatax = int(input("Enter the x coordinate of your testing data: ")) testdatay = int(input("Enter the y coordinate of your testing data: ")) n = int(input("Enter the number of nearest neighbors you wish to calculate: ")) testdata.append(testdatax) testdata.append(testdatay) distances = [] for x in traindata: a = x[0] b = x[1] distance = math.sqrt((abs(a - testdatax))^2 + (abs(b-testdatay))^2) distances.append(distance) mindistances = [] indices = [] while len(mindistances) < n: mindistance = min(distances) mindistances.append(mindistance) u = distances.index(mindistance) indices.append(u) distances[u] = float('inf') colors = [] for m in indices: value = traindata[m] color = value[2] colors.append(color) blues = [] blacks = [] for k in colors: if k == "blue": blues.append(k) elif k == "black": blacks.append(k) if len(blues)> len(blacks): print("Blue") elif len(blacks)> len(blues): print("Black")

k Nearest Neighbors

import math import operator import math data = [(1,5,"blue"),(2, 3, "red"), (1, 7, "blue"), (3, 6, "blue"), (4, 2, "blue"), (5, 9, "red"), (6, 4, "red"), (7,1, "red")] # ask user to input x and y xCoordinate = input("Give me a number: ") yCoordinate = input("Give me a number: ") #ask for value of K k = input("Give me a value for k: ") myDictionary = {} for x in data: x1 = x[0] y1 = x[1] #fint the distance between the given points to all the points in the dataset distance = math.sqrt((x1-int(xCoordinate))*(x1-int(xCoordinate)) + (y1-int(yCoordinate))*(y1-int(yCoordinate))) #store the distances in correspondance with the tuple, e.x (1,5,"blue") myDictionary[x] = distance # sort the dictionary by least to greatest distance newDictionary = sorted(myDictionary.items(), key=operator.itemgetter(1)) print(newDictionary) AList = [] colors = [] #store all the distances in seperate list for i in newDictionary: AList.append(i[1]) print(AList) #find the first 3 or 5 distances, based on the value of k, loop through those many times for z in range(int(k)): print(newDictionary[z][1]) #find the color of the 3 or 5 closest point, the value of k is what determines if it is 3 or 5 for n in range(int(k)): colors.append(newDictionary[n][0][2]) blueCount = 0 redCount = 0 #find the majority color in the given 3 or 5 points for x in range(0, len(colors)): if(colors[x] == 'blue'): blueCount = blueCount + 1 if(colors[x] == 'red'): redCount = redCount + 1 print(colors) #compare the # of points that are either red or blue if(redCount > blueCount): print("Your point should be red") if(redCount < blueCount): print("Your point should be red")


graph = [[1,1,"s"],[2,1,"s"],[4,5,"w"],[3,4,"w"],[0,3,"w"]] graph1 = [] graph2 = [] k = int(input("Enter k")) point = [0,0] point[0] = int(input("Enter x")) point[1] = int(input("Enter y")) for z in range(0,len(graph),1): y = (point[1] - graph[z][1])**2 x = (point[0] - graph[z][0])**2 graph[z].append(math.sqrt(x+y)) print(graph) for i in range(len(graph)): for z in range(0,len(graph)-i-1): if graph[z][3] > graph[z+1][3]: graph[z], graph[z+1] = graph[z+1], graph[z] print(graph) i = [] for z in range(0,k,1): i.append(graph[z][2]) i = statistics.mode(i) print(i)