Was the Tuning Worth It?

Machine Learning in CodeHard+10 XP+10

Was the Tuning Worth It?

Tuning often gains less than people hope, and scikit-learn's defaults are good. So the team adopts a rule before looking at any numbers: keep the tuned settings only if they beat the defaults by more than 0.005 in cross-validation. Then score whichever model wins on the test rows, once.

Task: write tuned_or_default(c_values, gamma_values).

  1. Make the data with make_classification(n_samples=600, n_features=20, n_informative=6, n_redundant=4, flip_y=0.05, random_state=0).
  2. Split it with train_test_split(X, y, test_size=0.25, random_state=0, stratify=y).
  3. The default model is Pipeline([("scale", StandardScaler()), ("model", SVC())]). Its cross-validated score is the mean of cross_val_score on the training rows with cv=5.
  4. Run GridSearchCV on the same pipeline with cv=5, over every combination of c_values for C and gamma_values for gamma. Fit it on the training rows. Its cross-validated score is best_score_.
  5. If the tuned score is more than the default score plus 0.005, the choice is "tuned". Otherwise it is "default".
  6. Score the chosen model on the test rows. For "default", fit the default pipeline on the training rows first.

Return a dict:

  • "choice": "tuned" or "default",
  • "default_cv" and "tuned_cv": the two cross-validated scores, rounded to 3 decimal places,
  • "test": the chosen model's accuracy on the test rows, rounded to 3 decimal places.

Compare unrounded scores when you decide.

Examples

c_values = [10]
gamma_values = [0.1]
returns {'choice': 'tuned', 'default_cv': 0.811, 'tuned_cv': 0.833, 'test': 0.8}
c_values = [1, 10]
gamma_values = [0.03]
returns {'choice': 'default', 'default_cv': 0.811, 'tuned_cv': 0.807, 'test': 0.8}

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