Prediction: your first ML idea
Validate outputs and schemas
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
Evaluate the chapter step
Validation asks whether the artifact is correct, not merely whether it completed. Sweep thresholds such as 0.3, 0.5, and 0.7, but report all confusion counts. Then freeze the threshold and score a separate set. When false positives and false negatives have different costs, accuracy alone is insufficient. Include a deliberately wrong case so the check proves it can fail. A test that never observes a bad result may be checking the wrong thing.
Sweep thresholds such as 0.3, 0.5, and 0.7, but report all confusion counts. Then freeze the threshold and score a separate set. When false positives and false negatives have different costs, accuracy alone is insufficient.
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
Read every command or statement before running it. The examples deliberately expose intermediate state so a surprising result has somewhere concrete to point.
For the current evaluate 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.
On this page, the practical job is to compare the result with an independent expectation. The running case is five examples have truth labels and confidence scores; the threshold must be tuned without pretending the tiny set proves generalization.
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
zipsilently stops at the shorter list, so assert equal lengths before scoring? - Can a new user reproduce a threshold evaluator that reports predictions, accuracy, and the confusion counts behind that accuracy from the stated setup?