Would More Rows Help?

Machine Learning in CodeMedium+10 XP+10

Would More Rows Help?

Collecting more data is expensive. A learning curve tells you if it would help: train on growing slices of the data and watch the gap between the training score and the validation score.

Task: write learning_gaps(model_name, fractions).

  1. Load the data with load_breast_cancer(return_X_y=True, as_frame=True) and use all the rows.
  2. Build the model named by model_name:
    • "logistic": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
    • "one_neighbour": make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=1))
  3. Run learning_curve on it with train_sizes=fractions and cv=5.

Return a dict:

  • "sizes": the training-set sizes learning_curve used, as whole numbers,
  • "gaps": for each size, the mean training score minus the mean validation score, rounded to 4 decimal places.

Examples

model_name = "logistic"
fractions = [0.15, 0.3, 0.6, 1.0]
returns {'sizes': [68, 136, 273, 455], 'gaps': [0.0867, 0.043, 0.0173, 0.0088]}
model_name = "one_neighbour"
fractions = [0.1, 0.3, 0.6, 1.0]
returns {'sizes': [45, 136, 273, 455], 'gaps': [0.2496, 0.0616, 0.0528, 0.0457]}

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