Label Counts Are Visible
Count negative and positive rows directly from the label column before fitting or scoring any model.
The problem
Class imbalance is not hidden inside a neural network or a loss function. It sits in the label column of the table before any model runs. When positive outcomes are rare and negative outcomes are routine, any summary number computed later on that table inherits the uneven split.
Suppose an inspection log stores one sensor reading and one binary outcome per row, where 0 marks a normal part and 1 marks a defective part. Looking at the first five rows [1.2, 0], [0.8, 0], [3.1, 1], [1.5, 0], and [0.9, 0], we want three exact numbers: the negative count, the positive count, and the positive share.
Work through the numbers
Extract the five binary labels [0, 0, 1, 0, 0]. Because each label is 0 or 1, adding the entries counts the positive rows directly: 0 + 0 + 1 + 0 + 0 = 1. Subtracting that sum from the 5 inspected rows leaves 5 - 1 = 4 negative rows.
The positive share divides the positive count by the total row count, giving 1 / 5 = 0.2, or twenty percent. The negative share is the remaining 4 / 5 = 0.8. Notice the denominator: positive share divides by all 5 rows, not by the 4 negative rows. Dividing 1 by 4 gives 0.25, which is the odds of a positive (positives per negative) rather than the fraction of defective parts.
Extending the window across all eight stored rows adds [1.1, 0], [2.9, 1], and [1.4, 0]. At 4 rows the counts are 3 negative and 1 positive (0.25); at 6 rows they are 5 and 1 (about 0.17); at 8 rows they become 6 negative and 2 positive (0.25).
The live model
The top strip draws each inspected row in order, coloring normal rows blue and defective rows amber. Below it, two count bars and a zero-to-one prevalence bar read the computed tensor [negative_count, positive_count, positive_share]. Drag the rows slider from 4 to 8 to watch how adding rows updates both counts and the positive share without fitting any classifier.

let rows = int(scene.parameter("rows", 5.0, 4.0, 8.0, 1.0));
let shown = input.slice(0, 0, rows);
let labels = shown.select(1, 1);
let total = float(rows);
let pos = labels.sum();
let neg = pos.rsub(total);
let share = pos.div(total);
let parts = torch.list().add(neg.reshape(1)).add(pos.reshape(1));
let answer = torch.cat(parts.add(share.reshape(1)), 0);
out.set("labels", labels).set("summary", answer);
{
"labels": [
0,
0,
1,
0,
0
],
"summary": [
4,
1,
0.2
]
}Watch the parameter change
Your turn
Complete the summary vector [negative_count, positive_count, positive_share] so the third entry equals positive rows divided by total inspected rows. The starting model divides by the negative count instead, turning 1 out of 5 into 0.25. Check runs new tables and row counts, so a hardcoded [4, 1, 0.2] will fail.

let rows = int(scene.parameter("rows", 5.0, 4.0, 8.0, 1.0));
let shown = input.slice(0, 0, rows);
let labels = shown.select(1, 1);
let total = float(rows);
let pos = labels.sum();
let neg = pos.rsub(total);
let share = pos.div(neg);
let parts = torch.list().add(neg.reshape(1)).add(pos.reshape(1));
let answer = torch.cat(parts.add(share.reshape(1)), 0);
out.set("labels", labels).set("summary", answer);
{
"labels": [
0,
0,
1,
0,
0
],
"summary": [
4,
1,
0.25
]
}Compute [negative_count, positive_count, positive_share] for the first rows of the table so positive_share divides positive_count by total inspected rows rather than negative rows.
Read the result carefully
Reporting 4 negatives and 1 positive already sets a floor for later evaluation: eighty percent of this five-row sample belongs to class 0. That count describes this specific sample, not necessarily the population from which future rows arrive. If defective parts were deliberately oversampled when building the log, the table's 0.2 positive share will overstate field prevalence unless the sampling rule is recorded alongside the data.
Recap
- Sum
0and1labels to count positive rows, and subtract from the row count to get negative rows. - Divide positive rows by total rows
n, not by negative rows, when reporting the positive share. - Record class counts before reading any accuracy or loss number on the dataset.