A Fixed Budget of Tries

Machine Learning in CodeEasy+10 XP+10

A Fixed Budget of Tries

A full grid of tree settings would take too long. You decide how many tries you can afford and let a random search spend them.

Task: write random_tree_search(n_iter, seed).

  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. Run RandomizedSearchCV(DecisionTreeClassifier(random_state=0), space, n_iter=n_iter, cv=5, random_state=seed) on the training rows, where space is
settingvalues
max_depth1, 2, 3, 4, 5, 6, 8, 10
min_samples_leaf1, 2, 4, 8, 16, 32

Return a dict:

  • "tried": how many combinations the search tried, as a whole number,
  • "best": its best_params_, exactly as scikit-learn gives it,
  • "cv": its best cross-validated score, rounded to 3 decimal places.

Examples

n_iter = 8
seed = 0
returns {'tried': 8, 'best': {'min_samples_leaf': 4, 'max_depth': 5}, 'cv': 0.927}
n_iter = 4
seed = 1
returns {'tried': 4, 'best': {'min_samples_leaf': 4, 'max_depth': 8}, 'cv': 0.927}

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