Prediction: your first ML idea
Set up interfaces and contracts
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
Setup the chapter step
The required setup is: Python 3 is enough; truth and score lists must have the same nonzero length. Confirm it before copying code. The contract separates input from output: binary truth labels, scores between zero and one, and a chosen threshold goes in, and binary predictions plus accuracy, true positives, false positives, true negatives, and false negatives comes out. If either side is ambiguous, later debugging will chase the wrong layer.
The input contract is binary truth labels, scores between zero and one, and a chosen threshold. The visible result is binary predictions plus accuracy, true positives, false positives, true negatives, and false negatives.
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
Document who is affected by an automatic positive label and provide a human review path for consequential decisions. Never describe a toy score as calibrated probability without evidence.
For the current setup 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.
Read every command or statement before running it. The examples deliberately expose intermediate state so a surprising result has somewhere concrete to point.
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 an empty list causes division by zero and is not a meaningful evaluation?
- Can a new user reproduce a threshold evaluator that reports predictions, accuracy, and the confusion counts behind that accuracy from the stated setup?