ML Foundations

Set explicit infinity replacements with NumPy nan_to_num

NumPy nan_to_num defaults infinities to the largest finite values for the input dtype. Choose posinf and neginf explicitly when that is not the desired policy.

Example results for Set explicit infinity replacements with NumPy nan_to_num. default infinity is finite: 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.

The default is not to clip infinity to zero

`numpy.nan_to_num` replaces NaN with zero by default, but positive and negative infinity use a different rule. For inexact dtypes, infinities become the largest finite values representable by the input dtype, with the corresponding sign. A result can therefore be finite while still being far outside the scale expected by a downstream model. [1]

That behavior can be useful when a finite substitute is specifically desired, but it is not a universal cleanup policy. If the application needs a bounded sentinel or domain-specific value, pass `posinf` and `neginf` explicitly. The function also accepts a separate `nan` replacement, so the three non-finite cases can have deliberately different meanings. [1]

References: [1]

Inspect the replacement and choose your own bounds

The example starts with negative infinity, NaN and positive infinity. It first uses defaults, then supplies explicit replacements of -5, 0 and 5. The default infinity values are checked against `numpy.finfo(float64).max`, which reports the machine limits for the floating-point dtype. No arbitrary contents or platform-dependent printed decimal are used as the assertion. [1, 2]

The second output shows a bounded, predictable result. Select these values from the application domain rather than copying -5 and 5 mechanically; these are illustrative. Keeping each replacement explicit also makes the intended treatment of missing and unbounded values reviewable at the conversion boundary.

Executed with Python 3.12.14 and NumPy 2.3.5 on CPU; assertions passed.

"""Compare NumPy's default and explicit non-finite replacements."""
import numpy as np

values = np.array([-np.inf, np.nan, np.inf], dtype=np.float64)
default = np.nan_to_num(values)
bounded = np.nan_to_num(values, nan=0.0, neginf=-5.0, posinf=5.0)

print("default infinity is finite:", np.isfinite(default[[0, 2]]).all())
print("default positive replacement is near float max:", default[2] == np.finfo(np.float64).max)
print("explicit replacements:", bounded.tolist())

assert np.isfinite(default).all()
assert default[0] == -np.finfo(np.float64).max
assert default[1] == 0.0
assert bounded.tolist() == [-5.0, 0.0, 5.0]

Recorded output

default infinity is finite: True
default positive replacement is near float max: True
explicit replacements: [-5.0, 0.0, 5.0]

References: [1], [2]

Finite values do not prove numerical suitability

After conversion, a finite-value check can establish that NaN and infinity are gone, but it cannot show that the chosen magnitudes are meaningful for the next operation. A largest-finite value may dominate a sum, distance, or gradient. When such values are invalid for the model, reject or mask them instead of silently replacing them with a huge number. [1, 2]

This guide demonstrates NumPy float64 on a three-element vector; it does not benchmark the operation or prescribe application-specific thresholds. The documented replacement scales with the input dtype, so check the relevant `finfo` limits when a different floating type is used. Choose the replacement policy before interpreting downstream numeric results. [1, 2]

References: [1], [2]

Sources

  1. NumPy: nan_to_num
  2. NumPy: finfo