Drop the Weakest, Repeat

Machine Learning in CodeMedium+10 XP+10

Drop the Weakest, Repeat

Recursive feature elimination fits a model, throws out its weakest column, and repeats until only n columns are left. Because the model is involved at every step, it can notice when two columns say the same thing.

Task: write rfe_columns(n).

  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 make_pipeline(StandardScaler(), RFE(LogisticRegression(max_iter=1000), n_features_to_select=n), LogisticRegression(max_iter=1000)) and fit it on the training rows.

Return a dict:

  • "kept": the names of the columns the RFE step kept, in the order they appear in the data,
  • "test": the pipeline's accuracy on the test rows, rounded to 3 decimal places.

Examples

n = 5
returns {'kept': ['mean concave points', 'worst radius', 'worst texture', 'worst area', 'worst concave points'], 'test': 0.937}
n = 3
returns {'kept': ['worst radius', 'worst area', 'worst concave points'], 'test': 0.93}

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