Three Ways to Prepare

Machine Learning in CodeMedium+10 XP+10

Three Ways to Prepare

Does scaling help, and which scaler helps most? Compare three versions of the same model on the same split.

Task: write compare_preparation(name, k). name is "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=0, stratify=y).
  • Build three versions, each ending in KNeighborsClassifier(n_neighbors=k):
    • "none": just the model.
    • "minmax": a pipeline of MinMaxScaler() then the model.
    • "standard": a pipeline of StandardScaler() then the model.
  • Fit each on the training rows and score it on the test rows, rounded to 3 decimal places.
  • Return a dict with those three scores under the keys "none", "minmax" and "standard", plus "best": the key with the highest rounded score. On a tie, prefer the one that comes first in the order "none", "minmax", "standard".

Examples

name = "wine"
k = 5
returns {'none': 0.667, 'minmax': 0.956, 'standard': 0.933, 'best': 'minmax'}
name = "breast_cancer"
k = 5
returns {'none': 0.916, 'minmax': 0.951, 'standard': 0.951, 'best': 'minmax'}

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