Scale, Then Vote

Machine Learning in CodeMedium+10 XP+10

Scale, Then Vote

Nearest neighbours measures distance, so it needs scaled columns. Do the scaling by hand, the honest way.

Task: write honest_score(name, k, seed). name is "iris", "wine" or "breast_cancer".

  • Load the dataset as X and y, and split with train_test_split(X, y, test_size=0.25, random_state=seed).
  • Fit a StandardScaler on the training rows only. Use it to scale the training rows and the test rows.
  • Train a KNeighborsClassifier(n_neighbors=k) on the scaled training rows.
  • Return its score on the scaled test rows, rounded to 3 decimal places.

Examples

name = "wine"
k = 5
seed = 0
returns 0.978
name = "breast_cancer"
k = 5
seed = 0
returns 0.951

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run