Fewest Missed Cancers

Machine Learning in CodeHard+10 XP+10

Fewest Missed Cancers

In screening, the worst mistake is calling a malignant tumour benign. In this dataset the label is 1 for benign and 0 for malignant. A model "calls a tumour benign" when its probability of class 1 is greater than or equal to a threshold.

Write safest_model(names, threshold).

  • Load the data with load_breast_cancer(return_X_y=True, as_frame=True) and split it with train_test_split(X, y, test_size=0.25, random_state=0, stratify=y).
  • names lists which models to try, chosen from:
    • "logistic": make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
    • "knn": make_pipeline(StandardScaler(), KNeighborsClassifier(n_neighbors=5))
    • "bayes": GaussianNB()
    • "forest": RandomForestClassifier(n_estimators=100, random_state=0)
  • For each named model, fit it on the training rows and take predict_proba(X_test)[:, 1].
  • Count its missed cancers: test rows it calls benign whose true label is 0.

Return a dict with each name in names mapped to its number of missed cancers (an int), plus one more entry "safest": the name with the fewest missed cancers. On a tie, pick the name that comes first in names.

Examples

names = ['logistic', 'knn']
threshold = 0.5
returns {'logistic': 3, 'knn': 6, 'safest': 'logistic'}
names = ['knn', 'bayes', 'forest']
threshold = 0.7
returns {'knn': 4, 'bayes': 5, 'forest': 2, 'safest': 'forest'}

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