priyanka Claude Opus 4.6 commited on
Commit
27755d2
·
1 Parent(s): ba9b8bb

Add low-confidence explainer in Lab 03 and challenge solutions for Lab 04

Browse files

- Add "Why Is My Confidence Score Low?" section in Lab 03 README
- Create solutions/lab_04_challenges.md with all 3 challenge answers
- Link solutions from test_api.py YOUR TURN section

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

lab_03_expose_model/README.md CHANGED
@@ -112,6 +112,28 @@ highlights:
112
 
113
  ---
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  ## 🎯 Challenges
116
 
117
  See the YOUR TURN section at the bottom of [app.py](app.py):
 
112
 
113
  ---
114
 
115
+ ## Why Is My Confidence Score Low?
116
+
117
+ If you see confidence around ~0.54 for every prediction, that's expected!
118
+ Our training dataset has only 35 samples, so the model has a limited
119
+ vocabulary. Short or vague notes like *"Patient had fever"* don't
120
+ contain the specific medical phrases the model learned from (e.g.
121
+ "ST-elevation", "troponin", "hemodynamically unstable").
122
+
123
+ Try a note that uses words from the training data and you'll see higher
124
+ confidence:
125
+
126
+ ```bash
127
+ curl -X POST http://127.0.0.1:8000/v1/predictions \
128
+ -H "Content-Type: application/json" \
129
+ -d '{"note": "Patient presents with acute chest pain, ST-elevation on ECG, and elevated troponin levels requiring immediate intervention"}'
130
+ ```
131
+
132
+ This is a real-world ML lesson: **models are only as good as their
133
+ training data.** More data and richer vocabulary = better predictions.
134
+
135
+ ---
136
+
137
  ## 🎯 Challenges
138
 
139
  See the YOUR TURN section at the bottom of [app.py](app.py):
lab_04_test_api/test_api.py CHANGED
@@ -341,3 +341,5 @@ class TestModelInfo:
341
  # - All urgent-sounding notes get "urgent"
342
  # - All routine-sounding notes get "routine"
343
  # This is a great way to build a test suite for your model!
 
 
 
341
  # - All urgent-sounding notes get "urgent"
342
  # - All routine-sounding notes get "routine"
343
  # This is a great way to build a test suite for your model!
344
+ #
345
+ # See solutions/lab_04_challenges.md for all answers.
solutions/lab_04_challenges.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Lab 04 — Challenge Solutions
2
+
3
+ ## Challenge 1 (Easy): Test total count after creating 5 predictions
4
+
5
+ ```python
6
+ @skip_without_model
7
+ def test_total_count_after_five_predictions(client):
8
+ """Create 5 predictions and verify the total count in meta."""
9
+ notes = [
10
+ "Acute chest pain with ST-elevation and troponin rise.",
11
+ "Routine follow-up for managed hypertension. BP stable.",
12
+ "Severe sepsis with lactate 4.2. Blood cultures drawn.",
13
+ "Annual wellness visit. No complaints. Vitals normal.",
14
+ "Status epilepticus not responding to first-line treatment.",
15
+ ]
16
+ for note in notes:
17
+ client.post("/v1/predictions", json={"note": note})
18
+
19
+ response = client.get("/v1/predictions")
20
+ assert response.status_code == 200
21
+ assert response.json()["meta"]["total"] == 5
22
+ assert len(response.json()["data"]) == 5
23
+ ```
24
+
25
+ ---
26
+
27
+ ## Challenge 2 (Medium): Test double-delete behavior
28
+
29
+ The second DELETE should return **404** — the resource no longer exists.
30
+
31
+ ```python
32
+ @skip_without_model
33
+ def test_double_delete_returns_404(client):
34
+ """Deleting a prediction twice: first returns 204, second returns 404."""
35
+ # Create a prediction
36
+ create = client.post(
37
+ "/v1/predictions",
38
+ json={"note": "Acute respiratory failure requiring emergency intubation."},
39
+ )
40
+ pred_id = create.json()["data"]["id"]
41
+
42
+ # First delete — should succeed
43
+ response = client.delete(f"/v1/predictions/{pred_id}")
44
+ assert response.status_code == 204
45
+
46
+ # Second delete — resource is already gone
47
+ response = client.delete(f"/v1/predictions/{pred_id}")
48
+ assert response.status_code == 404
49
+ ```
50
+
51
+ > **Why 404?** After the first DELETE, the resource no longer exists.
52
+ > Any subsequent request for that resource — whether GET or DELETE —
53
+ > should return 404 Not Found. This is idiomatic REST behavior.
54
+
55
+ ---
56
+
57
+ ## Challenge 3 (Stretch): Parameterized test for multiple notes
58
+
59
+ ```python
60
+ @skip_without_model
61
+ @pytest.mark.parametrize(
62
+ "note, expected_label",
63
+ [
64
+ ("Acute MI with ST-elevation. Troponin critically elevated. Cath lab activated.", "urgent"),
65
+ ("Massive GI hemorrhage with hemoglobin of 4.2. Transfusion started.", "urgent"),
66
+ ("Patient in anaphylactic shock after bee sting. Epinephrine administered.", "urgent"),
67
+ ("Routine annual physical. All vitals within normal limits.", "routine"),
68
+ ("Follow-up for well-controlled diabetes. HbA1c stable at 6.2.", "routine"),
69
+ ("Routine prenatal visit at 28 weeks. Fetal heart tones normal.", "routine"),
70
+ ],
71
+ )
72
+ def test_prediction_matches_expected_label(client, note, expected_label):
73
+ """Verify the model predicts the expected label for known notes."""
74
+ response = client.post("/v1/predictions", json={"note": note})
75
+ assert response.status_code == 201
76
+ assert response.json()["data"]["prediction"] == expected_label
77
+ ```
78
+
79
+ > **Note:** This test depends on the model's accuracy. With our small
80
+ > training dataset (35 samples), some edge-case notes may be
81
+ > misclassified. Use notes with strong signal words from the training
82
+ > data for reliable results.
83
+
84
+ Usage:
85
+ ```bash
86
+ pytest lab_04_test_api/test_api.py -v
87
+ ```