Audit TinyML score thresholds
Use scored decisions to evaluate the TinyML acceptance gate and select a threshold against a false-alarm budget.

Edge Eddie
The Edge & Fog guide asks learners to test TinyML fit and repeat measurements on the intended sensor path before release.
Predict the reading, then compare it with the measurement.
Python 3 in your browser (JupyterLite)
Python · no installOpen the notebook in your browser and run each Python cell; no install or account is needed.
Three ways to run: use JupyterLite here with no install; run main.py locally from the downloadable lab folder; or open the same notebook in Google Colab.
Steps
Step 1
- Do
- Run the first notebook cell in the editor and inspect the synthetic scored-input table.
- You will see
Run the cell. You should see:
- Synthetic scored examples: seed=73, positives=10, negatives=10
- row=00 label=1 score=0.953
- row=06 label=1 score=0.611
- row=09 label=1 score=0.303
- row=10 label=0 score=0.828
- row=12 label=0 score=0.516
- row=19 label=0 score=0.061
- STEP 1 synthetic inputs fixed
- Why it matters
- The labelled seeded set makes each later count reproducible; it is not a sensor trace.
Code for step 1#!/usr/bin/env python3 """Synthetic TinyML score audit; the Wokwi companion does not save labelled scores.""" import random seed = 73 rng = random.Random(seed) # Fixed labels and score bands; jitter makes the sample reproducible without # presenting the scores as sensor measurements or Wokwi output. labels = [1] * 10 + [0] * 10 bands = [.96, .89, .84, .78, .72, .67, .61, .56, .48, .31, .82, .64, .52, .43, .37, .29, .23, .18, .12, .07] scores = [round(max(0, min(1, x + rng.uniform(-.015, .015))), 3) for x in bands] print(f"Synthetic scored examples: seed={seed}, positives=10, negatives=10") for i in [0, 6, 9, 10, 12, 19]: print(f"row={i:02} label={labels[i]} score={scores[i]:.3f}") print("STEP 1 synthetic inputs fixed")
Step 1 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 2
- Do
- Run the second cell in the editor and inspect the confusion-matrix readout at threshold 0.50.
- You will see
Run the cell. You should see:
- Decision rule: alert when score >= 0.50
- actual/predicted alert quiet
- positive 8 2
- negative 3 7
- total=20; false alarms=3; missed positives=2
- STEP 2 confusion matrix computed
- Why it matters
- A confusion matrix exposes false alarms and missed positives that a single accuracy number hides.
Code for step 2def matrix(threshold): decisions = [int(score >= threshold) for score in scores] tp = sum(y == 1 and d == 1 for y, d in zip(labels, decisions)) fp = sum(y == 0 and d == 1 for y, d in zip(labels, decisions)) fn = sum(y == 1 and d == 0 for y, d in zip(labels, decisions)) tn = sum(y == 0 and d == 0 for y, d in zip(labels, decisions)) return tp, fp, fn, tn base = matrix(.50) print("Decision rule: alert when score >= 0.50") print("actual/predicted alert quiet") print(f"positive {base[0]:>2} {base[2]:>2}") print(f"negative {base[1]:>2} {base[3]:>2}") print(f"total={sum(base)}; false alarms={base[1]}; missed positives={base[2]}") print("STEP 2 confusion matrix computed")
= 0.50; actual/predicted alert quiet" loading="lazy">Step 2 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 3
- Do
- Run the third cell in the editor and inspect the threshold-sweep table for precision, recall and false alarms.
- You will see
Run the cell. You should see:
- threshold TP FP FN TN precision recall
- 0.30 10 5 0 5 0.667 1.000
- 0.40 9 4 1 6 0.692 0.900
- 0.50 8 3 2 7 0.727 0.800
- 0.60 7 2 3 8 0.778 0.700
- 0.70 5 1 5 9 0.833 0.500
- 0.80 3 1 7 9 0.750 0.300
- 0.90 1 0 9 10 1.000 0.100
- STEP 3 threshold sweep computed
- Why it matters
- Moving the threshold changes both alert yield and false-alarm cost on the same examples.
Code for step 3thresholds = [.30, .40, .50, .60, .70, .80, .90] sweep = [] print("threshold TP FP FN TN precision recall") for threshold in thresholds: tp, fp, fn, tn = matrix(threshold) precision = tp / (tp + fp) if tp + fp else 0 recall = tp / (tp + fn) if tp + fn else 0 sweep.append((threshold, tp, fp, fn, tn, precision, recall)) print(f"{threshold:.2f} {tp:>2} {fp:>2} {fn:>2} {tn:>2} {precision:.3f} {recall:.3f}") print("STEP 3 threshold sweep computed")
Step 3 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 4
- Do
- Run the fourth cell in the editor and inspect the precision and recall plot in the output panel.
- You will see
Run the cell. You should see:
- Precision and recall plot (each # is 0.1; round to nearest block)
- 0.30 P ####### 0.667
- R ########## 1.000
- 0.40 P ####### 0.692
- R ######### 0.900
- 0.50 P ####### 0.727
- R ######## 0.800
- 0.60 P ######## 0.778
- R ####### 0.700
- 0.70 P ######## 0.833
- R ##### 0.500
- 0.90 P ########## 1.000
- R # 0.100
- STEP 4 precision recall plotted
- Why it matters
- The paired plot makes the precision and recall tradeoff visible without smoothing the small sample.
Code for step 4# A text plot runs both locally and in JupyterLite without a package install. # Each filled block represents 0.1; exact values remain in step 3. print("Precision and recall plot (each # is 0.1; round to nearest block)") for threshold, _, _, _, _, precision, recall in sweep: print(f"{threshold:.2f} P {'#' * round(precision * 10):<10} {precision:.3f}") print(f" R {'#' * round(recall * 10):<10} {recall:.3f}") print("STEP 4 precision recall plotted")
Step 4 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture. Step 5
- Do
- Run the final cell in the editor and inspect the budget decision and invariant checks in the output panel.
- You will see
Run the cell. You should see:
- Budget: <= 2 false alarms in 10 synthetic negatives
- Chosen threshold=0.60; TP=7 FP=2 FN=3 TN=8
- Precision=0.778; recall=0.700
- Selection: maximize recall; break ties by precision, then lower threshold
- Matrix conservation and false-alarm budget: PASS
- This synthetic result does not establish deployed sensor or model performance.
- STEP 5 budget decision validated
- Why it matters
- The stated budget constrains the choice; the conservation check catches counting errors.
Code for step 5budget = 2 # false alarms per 10 synthetic negative examples eligible = [row for row in sweep if row[2] <= budget] chosen = max(eligible, key=lambda row: (row[6], row[5], -row[0])) print(f"Budget: <= {budget} false alarms in 10 synthetic negatives") print(f"Chosen threshold={chosen[0]:.2f}; TP={chosen[1]} FP={chosen[2]} FN={chosen[3]} TN={chosen[4]}") print(f"Precision={chosen[5]:.3f}; recall={chosen[6]:.3f}") print("Selection: maximize recall; break ties by precision, then lower threshold") assert sum(chosen[1:5]) == len(labels) and chosen[2] <= budget print("Matrix conservation and false-alarm budget: PASS") print("This synthetic result does not establish deployed sensor or model performance.") print("STEP 5 budget decision validated")
Step 5 · Python 3 in your browser (JupyterLite); numbered callout added to a real capture.
Chapter checks
These questions refer to the chapter’s examples. Use the return links to review their answers.
A team has a quantized model that fits in flash, but the tensor arena plus audio feature buffer leaves almost no SRAM for stack, logs, and firmware state. What should the team do before approving the TinyML deployment?
Return to the chapter’s knowledge checkA TinyML model has 80,000 INT8 weights. Where do the weights live, and what separately bounds SRAM use?
Return to the chapter’s knowledge check
Caution
Before you start
Read this before you start.
The fixed-seed scores are synthetic; the threshold choice does not establish deployed sensor or model performance.
Technical details
- Version tested
- Version tested: Python 3.12.7 / Pyodide 0.27.6 in JupyterLite 0.6.4; Chromium 148.0.7778.96; synthetic classroom fixtures. Date: 2026-10-08.
https://iotclass.org/labs/tinyml-threshold-audit.html