Vote of the Neighbors
Each row of X is a point with a label in y (0 or 1). Classify each row of X_new by the majority label of its k nearest points in X, where k is odd.
Example 1
X [[0,0],[1,0],[0,1],[9,9],[8,9]]
y [0,0,0,1,1]
X_new [[1,1],[9,8]]
k 3
returns [0,1]
The three points nearest (1, 1) all have label 0. Nearest (9, 8) are the two points with label 1 and one far away with label 0.
Constraints
kis 1, 3 or 5, andk <= len(X) <= 501 <= len(X_new) <= 8
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More scikit-learn problems
- Yes or NoEasy
- Average Over Five FoldsMedium
- Best Number of NeighborsMedium
- Centers of the GroupsMedium
- Column That Matters MostMedium
- How Well It RanksMedium
- No PeekingMedium
- Scale Then VoteMedium