k-nearest-neighbors classification
Implement k-nearest-neighbors classification. Given a list of training points (each a list of floats), a parallel list of integer labels, a query point, and an integer k, return the predicted label: the most common label among the k training points closest to the query by Euclidean distance. There are at least k points. Break distance ties by preferring the earlier point (lower index). Break label-vote ties by choosing the smallest label value.
Implement
knn_classify(points: list[list[float]], labels: list[int], query: list[float], k: int) → intExamples
in
[[[0,0],[1,1],[5,5]],[0,0,1],[0,0],1]out0What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 25 min
solution.py
InputExpectedGot
[[[0,0],[1,1],[5,5]],[0,0,1],[0,0],1]0not run yetsample