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")
1