ML Foundations

Handle all-missing rows before a NumPy nanmean

Preserve an explicit validity mask when averaging rows with missing observations.

Example results for Handle all-missing rows before a NumPy nanmean. has observations: True, False, True
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

A missing row is different from a zero mean

NumPy nanmean ignores NaN values when calculating a mean. For an all-NaN slice, the result is NaN and NumPy raises a RuntimeWarning. A downstream report may need to distinguish that undefined row from an observed row whose mean really is zero. [1]

This guide assumes missing observations are encoded as NaN and other values are finite. It computes a row-validity mask first, then only averages rows containing at least one observed value. [1, 2]

References: [1], [2]

Keep a validity mask beside the mean

Run python examples/numpy_nanmean_valid_rows.py. The fixture has a partially observed row, an all-missing row and a row with observed zeroes. The Boolean mask records whether each row has a non-NaN value. An output initialized to NaN retains that marker for the all-missing row; nanmean runs only on the valid subset. [1, 2]

Assertions check the validity mask, the numeric means and the missing marker. The third row has a genuine zero mean, whereas the second has no defined mean. Keeping the mask lets a consumer count or exclude missing rows without treating them as measured zeroes.

The row filter is part of the example policy, not a requirement to use this particular representation everywhere. If the application prefers an explicit error or a separate status column, document that contract and handle it before aggregating the results.

Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.

import numpy as np

x = np.array([[1., np.nan, 3.], [np.nan, np.nan, np.nan], [0., 0., np.nan]])
valid = np.any(~np.isnan(x), axis=1)
means = np.full(x.shape[0], np.nan)
means[valid] = np.nanmean(x[valid], axis=1)
assert valid.tolist() == [True, False, True]
assert np.allclose(means[valid], [2., 0.])
assert np.isnan(means[1])
print("has observations:", valid.tolist())
print("row means:", means.tolist())

Recorded output

has observations: [True, False, True]
row means: [2.0, nan, 0.0]

References: [1], [2]

Check the rest of the data contract

This fixture is a two-dimensional floating array with a nonempty feature dimension. np.isnan does not classify infinities as NaN, so validate or define a separate policy for infinite values if they can occur. [2]

The approach does not repair missing values or establish that dropping them is statistically appropriate. It also does not measure performance. Track observation counts when comparisons between rows require knowing how much data contributed to each mean.

References: [1], [2]

Sources

  1. NumPy nanmean
  2. NumPy isnan