Prediction: your first ML idea
Implement the happy path
Binary prediction turns scores into labels with a threshold, then compares those labels with known truth using explicit metrics.
Before you start
Why this matters
Before running anything, predict one observable result from the case: five examples have truth labels and confidence scores; the threshold must be tuned without pretending the tiny set proves generalization. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.
1Learn the idea
Read
Build the chapter step
Build the complete path once without adding optional features. Enter the example exactly, predict the expected output, run it, and compare. Then change one meaningful value connected to five examples have truth labels and confidence scores; the threshold must be tuned without pretending the tiny set proves generalization and explain why the result should change.
The deliverable for this step is a threshold evaluator that reports predictions, accuracy, and the confusion counts behind that accuracy.
Read
Run the working example
truth = [1, 0, 1, 1, 0]
scores = [0.8, 0.3, 0.6, 0.9, 0.2]
threshold = 0.5
preds = [int(score >= threshold) for score in scores]
pairs = list(zip(truth, preds))
tp = pairs.count((1, 1)); fp = pairs.count((0, 1))
tn = pairs.count((0, 0)); fn = pairs.count((1, 0))
accuracy = (tp + tn) / len(pairs)
print(preds, f"accuracy={accuracy:.2f}", tp, fp, tn, fn)
Expected evidence:
[1, 0, 1, 1, 0] accuracy=1.00 3 0 2 0
The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.
Read
Read it line by line
- the comparison returns Boolean values and
intconverts them to zero or one. zipaligns each known label with exactly one prediction.- the four tuple counts partition every example into confusion-matrix cells.
- accuracy divides correct cells by all evaluated pairs; formatting shows two decimal places.
These lines form one chain: binary truth labels, scores between zero and one, and a chosen threshold becomes binary predictions plus accuracy, true positives, false positives, true negatives, and false negatives. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.
Read
Common errors and fixes
- First failure:
zipsilently stops at the shorter list, so assert equal lengths before scoring. Re-run the smallest command that proves the repair. - Second failure: an empty list causes division by zero and is not a meaningful evaluation. Preserve the failing input as a test when it represents a realistic mistake.
- Misleading success: choosing a threshold after repeatedly inspecting the same labels overfits the evaluation set even though no model weights changed. A clean-looking final line cannot cancel contradictory intermediate evidence.
When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.
Read
Evidence for this stage
Package the evaluator with boundary tests and a short metric note. Downstream users need the threshold, score range, positive-class meaning, and date of the evaluation data.
For the current build step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.
Keep the example small enough to inspect manually. Small does not mean careless: boundary values, file locations, feature order, and held-out data still determine whether the result means what you claim.
Read
Reflect on the result
Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.
Continue learning · glossary & guides
- Which line or command establishes the current step's most important fact?
- What output would reveal that choosing a threshold after repeatedly inspecting the same labels overfits the evaluation set even though no model weights changed?
- Can a new user reproduce a threshold evaluator that reports predictions, accuracy, and the confusion counts behind that accuracy from the stated setup?