Accuracy Is Four Out of Five
Compute accuracy from true positives and true negatives and see why negative counts dominate it under class imbalance.
The problem
Accuracy adds the two diagonal cells of the confusion table—true positives plus true negatives—and divides by total row count N. Because TP and TN enter the numerator with equal weight of one per row, whichever class supplies most of the rows controls most of the score.
Using the same five scored rows [0.10, 0], [0.15, 0], [0.35, 1], [0.20, 0], and [0.42, 0], we compute [TP, TN, accuracy] under prob > threshold. Our goal is to watch how TP and TN trade places across thresholds even when their sum leaves accuracy unchanged.
Work through the numbers
At threshold = 0.5, the confusion counts are TP = 0, FP = 0, FN = 1, and TN = 4. Accuracy is (TP + TN) / N = (0 + 4) / 5 = 0.8, or four out of five. Every point of that 0.8 score comes from TN = 4; the classifier detected zero positive cases.
Now lower threshold step by step. At threshold = 0.4, row 4 (0.42, true 0) becomes a false positive while row 2 (0.35, true 1) stays missed, so TP = 0, TN = 3, and accuracy drops to (0 + 3) / 5 = 0.6.
At threshold = 0.3, row 2 crosses the cutoff too: TP rises to 1 while TN stays 3. Accuracy becomes (1 + 3) / 5 = 0.8—identical to the 0.8 at threshold = 0.5. Accuracy stays at 0.8 for threshold = 0.2, then falls to (1 + 1) / 5 = 0.4 at threshold = 0.1.
The live model
The stacked bar decomposes total accuracy into its green TP / N slice and its blue TN / N slice across a zero-to-one axis, alongside individual TP and TN counters. Drag threshold from 0.5 to 0.3: total bar length returns to 0.8, yet its composition shifts from pure blue (0 and 4) to mixed green and blue (1 and 3).

let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let total = float(label.numel());
let pred = prob.gt(threshold).to(DType.F64);
let tp = (pred * label).sum();
let tn = (pred.rsub(1.0) * label.rsub(1.0)).sum();
let accuracy = (tp + tn).div(total);
let parts = torch.list().add(tp.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(parts.add(accuracy.reshape(1)), 0);
out.set("total_rows", total).set("metrics", answer);
{
"total_rows": 5,
"metrics": [
0,
4,
0.8
]
}Watch the parameter change
Your turn
Complete [tp, tn, accuracy] so accuracy includes both tp and tn over total. The starter divides tn alone by total, a bug that looks harmless at threshold = 0.5 where tp = 0 and fails as soon as a threshold catches positive rows. Check tests unseen tables and lower thresholds.

let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let total = float(label.numel());
let pred = prob.gt(threshold).to(DType.F64);
let tp = (pred * label).sum();
let tn = (pred.rsub(1.0) * label.rsub(1.0)).sum();
let accuracy = tn.div(total);
let parts = torch.list().add(tp.reshape(1)).add(tn.reshape(1));
let answer = torch.cat(parts.add(accuracy.reshape(1)), 0);
out.set("total_rows", total).set("metrics", answer);
{
"total_rows": 5,
"metrics": [
0,
4,
0.8
]
}Return [tp, tn, accuracy] under rule prob > threshold so accuracy divides the sum of true positives and true negatives by total rows.
Read the result carefully
Two models with identical 0.8 accuracy on the same five rows can behave completely differently in practice: one misses the defective part silently (TP = 0, TN = 4), while the other catches the defective part at the cost of one extra inspection (TP = 1, TN = 3). Reporting accuracy without its TP and TN components treats one false negative and one false positive as interchangeable.
Recap
- Accuracy equals
(TP + TN) / N, weighting every row equally regardless of class rarity. - At
threshold = 0.5,TN = 4alone produces4 / 5 = 0.8even withTP = 0. - Equal accuracy at
0.5and0.3(0.8both times) hides the shift from[TP = 0, TN = 4]to[TP = 1, TN = 3].