How Few Columns Will Do?

Machine Learning in CodeMedium+10 XP+10

How Few Columns Will Do?

Every column the clinic measures costs time and money. You want the smallest number of columns whose model is nearly as good as using all thirty, judged honestly by cross-validation on the training rows.

Task: write smallest_good_k(k_values, tolerance).

  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). Use the training rows only.
  3. Score the all-columns model, make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)), with cross_val_score and cv=5. Take the mean.
  4. For each k in k_values, score make_pipeline(StandardScaler(), SelectKBest(f_classif, k=k), LogisticRegression(max_iter=1000)) the same way.
  5. Choose the smallest k whose mean score is at least the all-columns mean minus tolerance.

Return a dict:

  • "all": the all-columns mean score, rounded to 3 decimal places,
  • "k": the k you chose,
  • "score": its mean score, rounded to 3 decimal places.

k_values may come in any order. In every test at least one k qualifies. Compare unrounded values.

Examples

k_values = [2, 5, 10, 20]
tolerance = 0.03
returns {'all': 0.974, 'k': 5, 'score': 0.948}
k_values = [2, 5, 10, 20]
tolerance = 0.005
returns {'all': 0.974, 'k': 20, 'score': 0.977}

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