ML Foundations

Preserve a missing-value mask when cleaning NumPy arrays

Distinguish original zeros from replaced nonfinite values and inspect the defaults for infinities.

Example results for Preserve a missing-value mask when cleaning NumPy arrays. finite: True, False, False, 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.

Cleaning can erase information

Before passing an array to downstream code, an engineer may replace NaN or infinite values. NumPy nan_to_num replaces NaN with zero by default, and replaces positive and negative infinity with large finite values appropriate to the input dtype. A later finite-value check cannot tell which entries were originally nonfinite. [1, 2]

A replaced NaN can become indistinguishable from a genuine zero. That distinction matters if missing measurements should be excluded, counted, imputed or investigated rather than treated as observed zeros. Preserve a mask before replacing anything.

References: [1], [2]

Record validity before filling

Run python examples/numpy_nonfinite_mask.py. The fixture contains a real zero, a NaN, positive and negative infinity, and an ordinary finite value. The isfinite mask marks only the original zero and the ordinary value as valid. [2]

The program shows that the default NaN replacement is zero and confirms that the infinities become the dtype maximum and minimum. It then uses an explicit zero fill while retaining the original mask. The printed missing positions distinguish replaced values from the real zero even though the filled values look the same. [1, 2]

The mask and filled array must travel together. If the mask is discarded or computed only after cleaning, this example cannot recover which zeros were real observations. The assertions verify the chosen fill and mask for this fixture.

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

import numpy as np

values = np.array([0.0, np.nan, np.inf, -np.inf, 2.5])
finite = np.isfinite(values)
default_cleaned = np.nan_to_num(values)
filled = np.where(finite, values, 0.0)
print('finite:', finite.tolist())
print('default NaN:', float(default_cleaned[1]))
print('default +inf is dtype maximum:', bool(default_cleaned[2] == np.finfo(values.dtype).max))
print('default -inf is dtype minimum:', bool(default_cleaned[3] == np.finfo(values.dtype).min))
print('explicit fill:', filled.tolist())
print('missing positions:', np.flatnonzero(~finite).tolist())
assert finite.tolist() == [True, False, False, False, True]
assert filled.tolist() == [0.0, 0.0, 0.0, 0.0, 2.5]

Recorded output

finite: [True, False, False, False, True]
default NaN: 0.0
default +inf is dtype maximum: True
default -inf is dtype minimum: True
explicit fill: [0.0, 0.0, 0.0, 0.0, 2.5]
missing positions: [1, 2, 3]

References: [1], [2]

Choose a domain-specific policy

Zero filling is only an example, not a universal imputation rule. A model may need to reject nonfinite inputs, use a fitted imputer, add a missingness feature, or propagate missingness instead. If infinities and NaNs mean different things in the application, keep separate masks for them.

This finite float fixture does not establish a policy for complex arrays, integer input, missing labels, extreme magnitudes or real model outcomes. Validate the replacement and mask handling at the boundary where your system consumes the array.

References: [1], [2]

Sources

  1. NumPy nan_to_num
  2. NumPy isfinite