Leak-Free Score
Scaling the whole table and then cross-validating lets every test fold leak into the scaler. Put the standardizing inside the model instead: build a pipeline of a standard scaler and a 5 nearest neighbors classifier, score it on X and y by 5-fold cross-validation, and return the mean accuracy. This way the scaler is refitted on the training folds each time.
Example 1
X [[0,0],[1,700],[2,100],[3,800],[4,200],[5,900],[6,300],[7,1000],[8,400],[9,1100],[0,500],[1,1200],[2,600],[3,0],[4,700],[5,100],[6,800],[7,200],[8,900],[9,300],[0,1000],[1,400],[2,1100],[3,500],[4,1200],[5,600],[6,0],[7,700],[8,100],[9,800]]
y [0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1]
returns 0.9333333333333333
The label follows the first column. Unscaled, the second column, a hundred times larger, would decide who the neighbors are.
Constraints
30 <= len(X) <= 60- Labels are 0 and 1, about half each
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More scikit-learn problems
- Mixed ColumnsHard
- Pick the PenaltyHard
- Right Amount of BendHard
- Scored in Time OrderHard
- Spam or NotHard
- Tune Then TestHard
- Hold Some BackBeginner
- Learn Then PredictBeginner