FunctionRacerProblemsRace

Best Number of Neighbors

Mediumscikit-learnPython

How many neighbors should vote? Try n_neighbors of 1, 3, 5 and 7 for a nearest neighbors classifier on X and y, scoring each by 3-fold cross-validation, and return the value that scores best (the smallest on a tie).

Example 1

X [[0,0],[1,2],[2,4],[3,1],[4,3],[5,0],[0,2],[1,4],[2,1],[3,3],[4,0],[5,2],[0,4],[1,1],[2,3],[3,0],[4,2],[5,4],[0,1],[1,3],[2,0],[3,2],[4,4],[5,1],[0,3],[1,0],[2,2],[3,4],[4,1],[5,3]]
y [0,0,1,0,1,1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,0,1,1,1,0,0,0,1,1,1]
returns 7

The label depends on a sum of the two columns. Several neighbors vote better here than one.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More scikit-learn problems