Milestone 4 of 8
Implement deterministic label selection
Choose a label by count, selected-distance sum, and lexical order while preserving the evidence for every candidate label.
A vote is not just a count. Preserve the evidence used at each tie stage so a prediction can be reconstructed.
Goal
Select a label from one ordered neighbor trace using count, distance sum, and lexicographic tie rules, and return the prediction with complete vote evidence.
Inputs
Use the ordered k neighbors from the previous milestone. For every candidate
label, calculate:
- the number of selected neighbors with that label;
- the sum of their squared distances; and
- the label itself for the final exact tie.
Apply these rules in order: greatest count, then smallest distance sum, then lexicographically smallest label. Do not use expected labels during selection.
Deliverables
Implement src/predict.py with a deterministic label-selection function. It
returns the predicted label and a vote trace containing every candidate label,
its count, its distance sum, and the rule stage that decided the winner.
Save the prediction and vote trace in the format used by
output/vote_traces.json; keep the query ID and ordered neighbor IDs attached.
Checks
Use public fixtures for a unique count winner, a distance-sum tie, and a remaining exact lexical tie. Check that every selected neighbor contributes to exactly one label's count and sum, that no expected label enters the rule, and that the returned label agrees with the stated three-stage order.
Reconstruct the prediction from the saved vote trace and neighbor trace. Check that the function does not mutate the neighbor list or distances and that an empty neighbor list is rejected rather than producing an arbitrary label.
Workspace
Keep vote selection in src/predict.py and consume the ordered trace from
src/neighbors.py. Store vote evidence under output/; do not bury the
distance sums inside a display-only string.
Hints
HintKeep all labels visible
HintTie stages have different questions
Review
Read each fixture's vote trace and identify the stage that decided the result. Explain why a tie rule is part of the algorithm rather than a formatting choice.
How to check your work
Checks compare the vote trace and three tie cases with the supplied fixture. The supplied fixture makes every stage visible and does not use an implicit library ordering.