Explain Every Mistake

Machine Learning in CodeHard+10 XP+10

Explain Every Mistake

For the write-up's where it fails section, list each test sample the model got wrong, and the one measurement that pushed it hardest towards its wrong answer.

The model is a scaled logistic regression on the breast cancer data. Class 1 is benign, class 0 is malignant. A positive push moves a row towards benign, a negative push towards malignant.

Task: write mistakes(test_size).

  • Load load_breast_cancer(return_X_y=True, as_frame=True) and split with train_test_split(X, y, test_size=test_size, random_state=0, stratify=y).
  • Fit make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)) on the training rows.
  • Go through the test rows in order. For each row the model gets wrong, compute its pushes: scaled values times coef_[0].
  • If it predicted 1, the culprit is the column with the largest push. If it predicted 0, the culprit is the column with the most negative push.
  • Return a list of [position_in_test_set, true_class, predicted_class, culprit] for every wrong row, in test-set order. Positions start at 0, and both classes are plain ints.

Examples

test_size = 0.25
returns [[12, 0, 1, 'radius error'], [47, 1, 0, 'mean fractal dimension'], [48, 0, 1, 'worst texture'], [60, 0, 1, 'mean concave points'], [125, 1, 0, 'worst texture'], [135, 1, 0, 'worst texture']]
test_size = 0.15
returns [[11, 0, 1, 'radius error'], [79, 0, 1, 'radius error']]

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