What Imbalance Does and Does Not Say
Separate what skewed label counts reveal about accuracy baselines from the error costs needed to choose an operating threshold.
The problem
Across the previous five lessons, one skewed table exposed a trap: when four of five rows carry label 0, accuracy reaches 0.8 even while positive recall sits at 0.0. Readers often jump to the opposite extreme and assume imbalanced probabilities are broken or that every rare-class problem demands the lowest possible threshold.
We now put accuracy, the majority-class baseline, and weighted error cost side by side on the same five scored rows [0.10, 0], [0.15, 0], [0.35, 1], [0.20, 0], and [0.42, 0]. Each false positive costs 1 unit and each missed positive (FN) costs miss_cost units, returning [accuracy, majority_accuracy, weighted_cost].
Work through the numbers
First compute what class imbalance does say: the majority class holds max(1, 4) = 4 of 5 rows, fixing majority_accuracy = 4 / 5 = 0.8. Any classifier scoring 0.8 accuracy has not beaten the zero-feature Always-N baseline on raw row count.
Now check what imbalance does not say by varying miss_cost and threshold. At threshold = 0.5, we have FP = 0 and FN = 1, so accuracy = 0.8 and weighted_cost = miss_cost * 1 + 0. When miss_cost = 4, total cost is 4 * 1 + 0 = 4.
Lower threshold to 0.3: row 2 (0.35, true 1) becomes a true positive and row 4 (0.42, true 0) becomes a false positive, giving FP = 1 and FN = 0. Accuracy is still 0.8, matching majority_accuracy, yet weighted_cost drops from 4 to 4 * 0 + 1 = 1—without retraining the score column. If miss_cost = 1, both 0.5 and 0.3 tie at cost 1.
The live model
The top bars compare model accuracy against the 0.8 majority baseline, while the bottom bar tracks miss_cost * FN + FP alongside the raw FP and FN counts. Drag threshold from 0.5 to 0.3 at miss_cost = 4 to watch cost fall from 4 to 1 while accuracy stays pinned at 0.8, then slide miss_cost to 1 or 8 to see how domain stakes—not label counts alone—govern the trade-off.

let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
let miss_cost = scene.parameter("miss_cost", 4.0, 1.0, 8.0, 1.0);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let total = float(prob.numel());
let pred = prob.gt(threshold).to(DType.F64);
let neg_pred = pred.rsub(1.0);
let neg_label = label.rsub(1.0);
let tp = (pred * label).sum();
let fp = (pred * neg_label).sum();
let miss_pos = (neg_pred * label).sum();
let tn = (neg_pred * neg_label).sum();
let accuracy = (tp + tn).div(total);
let pos_total = label.sum();
let neg_total = neg_label.sum();
let pick_neg = neg_total.gt(pos_total).to(DType.F64);
let maj_acc = (pos_total + (neg_total - pos_total) * pick_neg).div(total);
let cost = miss_pos * miss_cost + fp;
let parts = torch.list().add(accuracy.reshape(1)).add(maj_acc.reshape(1));
let answer = torch.cat(parts.add(cost.reshape(1)), 0);
let err_pair = torch.list().add(fp.reshape(1)).add(miss_pos.reshape(1));
let errors = torch.cat(err_pair, 0);
out.set("errors", errors).set("summary", answer);
{
"errors": [
0,
1
],
"summary": [
0.8,
0.8,
4
]
}Watch the parameter change
Your turn
Complete [accuracy, maj_acc, cost] so maj_acc uses the larger class count on any table and cost weights each missed positive by miss_cost plus fp. The starter counts unweighted mistakes miss_pos + fp, ignoring miss_cost. Check tests unseen tables—including a positive-majority split—and different cost weights.

let threshold = scene.parameter("threshold", 0.5, 0.1, 0.6, 0.1);
let miss_cost = scene.parameter("miss_cost", 4.0, 1.0, 8.0, 1.0);
let prob = input.select(1, 0);
let label = input.select(1, 1);
let total = float(prob.numel());
let pred = prob.gt(threshold).to(DType.F64);
let neg_pred = pred.rsub(1.0);
let neg_label = label.rsub(1.0);
let tp = (pred * label).sum();
let fp = (pred * neg_label).sum();
let miss_pos = (neg_pred * label).sum();
let tn = (neg_pred * neg_label).sum();
let accuracy = (tp + tn).div(total);
let pos_total = label.sum();
let neg_total = neg_label.sum();
# Fix maj_acc for any majority class and weight miss_pos by miss_cost.
let maj_acc = neg_total.div(total);
let cost = miss_pos + fp;
let parts = torch.list().add(accuracy.reshape(1)).add(maj_acc.reshape(1));
let answer = torch.cat(parts.add(cost.reshape(1)), 0);
let err_pair = torch.list().add(fp.reshape(1)).add(miss_pos.reshape(1));
let errors = torch.cat(err_pair, 0);
out.set("errors", errors).set("summary", answer);
{
"errors": [
0,
1
],
"summary": [
0.8,
0.8,
1
]
}Compute [accuracy, maj_acc, cost] so maj_acc equals the larger class fraction on any split and cost weights each missed positive by miss_cost plus fp.
Read the result carefully
Class imbalance tells you that overall accuracy has a high floor (max(pos, neg) / N) and that rare-class behavior must be inspected separately via TP, FP, and FN. Imbalance does not say the learned ranking failed—here row 2 already outranks three of four negatives—and it never supplies miss_cost on its own; only the real-world consequence of a missed positive versus a false alarm settles the cutoff.
Beyond moving the threshold as done here, standard responses to class imbalance include class weights in the training loss, resampling (applied only ever to the training split, never the evaluation split), stratified splits that preserve class ratios across folds, and balanced accuracy (recall + specificity) / 2. On this table the Always-N rule has recall = 0 / 1 = 0 and specificity = 4 / 4 = 1, so balanced accuracy is (0 + 1) / 2 = 0.5 rather than 0.8.
Recap
- Skewed counts set
majority_accuracy = max(pos, neg) / N(0.8here), exposing when high accuracy carries zero positive detection. - Imbalance does not mean predicted scores are useless: shifting
thresholdfrom0.5to0.3catches row2at0.8accuracy. - Choosing between
[FP = 0, FN = 1]and[FP = 1, FN = 0]requires explicit error weightsmiss_cost * FN + FP, not class counts alone.