Speed

Missing Cells Stay Visible

Separate unrecorded sensor entries from genuine zero measurements by computing an explicit missingness mask before any replacement.

The problem

A raw table often arrives with unrecorded cells alongside genuine numbers. Suppose a roadside weather station records daily rainfall in millimeters and noon air temperature in degrees Celsius across five days: [4, 18], [-1, 20], [0, 17], [6, -1], and [-1, 22]. Because numeric tensors cannot hold blank text, the station writes the sentinel -1 whenever a gauge goes offline.

A sentinel is safe only when no real reading can equal it: rainfall is never negative, and this station sits where noon temperatures stay above freezing. For a column that can go below zero, a negative sentinel would erase real readings; libraries usually store a not-a-number value (NaN) for a blank and test for it directly. The lessons of this book keep the sentinel because every column in them is non-negative.

Notice the contrast between day 1 ([-1, 20]) and day 2 ([0, 17]). Day 2 had zero millimeters of rain, a real dry-day reading. Day 1 had no rain measurement at all. Overwriting blanks with 0 or feeding -1 directly into a linear formula mixes equipment outages with dry weather. The first step of preprocessing is building a missingness mask that keeps every gap visible.

Work through the numbers

Test each cell of the 5-by-2 table against the sentinel condition strictly below 0. Day 0 ([4, 18]) has two non-negative readings, giving mask row [0, 0]. Day 1 ([-1, 20]) flags the missing rain cell and keeps temperature observed, giving [1, 0]. Day 2 ([0, 17]) satisfies 0 >= 0 in both columns, so its mask row is [0, 0].

Day 3 ([6, -1]) flags the missing temperature cell as [0, 1], and day 4 ([-1, 22]) flags rainfall as [1, 0]. Summing the mask down the five rows yields column totals [2, 1]: two missing rainfall cells and one missing temperature cell, or 3 missing entries out of 10 total cells (7 observed).

If we mistakenly tested <= 0 instead of strictly below 0, day 2 rainfall (0 mm) would flip from 0 to 1, inflating the column totals from [2, 1] to [3, 1]. Keeping the comparison strict preserves genuine zeros.

The live model

The diagram places the 5-by-2 sensor log beside its binary mask. Blue cells carry mask 0 (observed), and red cells carry mask 1 (flagged missing). At the default slider value day1_rain = -1, day 1 rainfall is unrecorded, column totals are [2, 1], and total missing cells equal 3. Move day1_rain to 0 to record a dry day: cell (1, 0) turns blue, column totals drop to [1, 1], and total missing drops to 2, where they remain up to day1_rain = 5.

Five weather rows on the left and a binary missingness mask on the right: three -1 entries turn red with mask 1 while zero rain on day 2 stays observed with mask 0.
let day1_rain = scene.parameter("day1_rain", -1.0, -1.0, 5.0, 1.0);
let table = input.clone();
table.set(2, day1_rain);
let answer = table.lt(0.0).to(DType.F64);
let col_missing = answer.sum(0);
out.set("table", table).set("missing_mask", answer);
out.set("column_missing", col_missing).set("total_missing", answer.sum());
{
  "table": [
    [
      4,
      18
    ],
    [
      -1,
      20
    ],
    [
      0,
      17
    ],
    [
      6,
      -1
    ],
    [
      -1,
      22
    ]
  ],
  "missing_mask": [
    [
      0,
      0
    ],
    [
      1,
      0
    ],
    [
      0,
      0
    ],
    [
      0,
      1
    ],
    [
      1,
      0
    ]
  ],
  "column_missing": [
    2,
    1
  ],
  "total_missing": 3
}
Watch the parameter change

Your turn

Day 2 recorded 0 mm of rain on a dry afternoon, yet the starter flags that reading as missing alongside the three offline -1 cells, pushing column_missing up to [3, 1]. Adjust the sentinel check so dry-day zeros stay observed while negative cells still receive 1.

The starter mask wrongly marks 0 mm rain on day 2 as missing alongside the three -1 sentinel cells, inflating column counts to 3 and 1.
let table = input.clone();
let answer = table.le(0.0).to(DType.F64);
let col_missing = answer.sum(0);
out.set("missing_mask", answer).set("column_missing", col_missing);
{
  "missing_mask": [
    [
      0,
      0
    ],
    [
      1,
      0
    ],
    [
      1,
      0
    ],
    [
      0,
      1
    ],
    [
      1,
      0
    ]
  ],
  "column_missing": [
    3,
    1
  ]
}

Flag only unrecorded sentinel cells as 1 so genuine zero measurements remain 0. In the starter, day 2's dry-day reading of 0 mm is counted as missing alongside the three offline cells.

Once dry-day zeros stay unflagged, day 2 rainfall returns to mask 0, column_missing drops from [3, 1] back to [2, 1], and total flagged cells equal 3.

Read the result carefully

A binary mask records which cells are absent without inventing replacement numbers yet. Downstream code can count gaps per column, exclude unrecorded cells when computing a training average, or keep the mask itself as an extra indicator feature. None of that works if sentinel codes such as -1 or 999 pass silently into arithmetic, or if valid zero measurements are mistaken for blanks.

Recap

  • Represent missing entries with an explicit 0 and 1 missingness mask before computing statistics or predictions.
  • Keep genuine 0 measurements distinct from negative sentinel codes so dry days or zero counts stay observed.
  • Sum the mask along rows or columns to audit exactly how many cells each feature lacks.