k-nearest-neighbor classification
Implement deterministic nearest-neighbor classification.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input features and labels contain training examples, query is one vector with the same dimension, and 1 ≤ k ≤ training size. Rank squared Euclidean distances ascending, breaking ties by original row index. Return neighbors (row IDs) and label (most votes, breaking vote ties by the lexicographically smallest string label).
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Implement deterministic nearest-neighbor classification.
Reproduction
{"features":[[10],[3],[1]],"labels":["A","B","C"],"query":[0],"k":1}{"neighbors":[2],"label":"C"}{"features":[[1]],"labels":["a"],"query":[5],"k":1}{"neighbors":[0],"label":"a"}{"features":[[-1],[1]],"labels":["z","a"],"query":[0],"k":1}{"neighbors":[0],"label":"z"}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.