Turn One Dial, Refit

Machine Learning in CodeEasy+10 XP+10

Turn One Dial, Refit

You have one pipeline and want to try several neighbour counts without rebuilding it each time.

Task: write neighbour_sweep(k_values).

  1. Load the data with load_breast_cancer(return_X_y=True, as_frame=True).
  2. Split it with train_test_split(X, y, test_size=0.25, random_state=0, stratify=y).
  3. Build one pipeline, make_pipeline(StandardScaler(), KNeighborsClassifier()).
  4. For each k in k_values, in order: set the pipeline's neighbour count to k, fit it on the training rows, and score it on the test rows.

Return the list of test accuracies, each rounded to 3 decimal places, in the same order as k_values.

Examples

k_values = [5]
returns [0.951]
k_values = [2, 7, 101]
returns [0.93, 0.958, 0.923]

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