ML Foundations

Choose NumPy maximum or fmax for NaN values

See when element-wise maxima propagate NaNs and when they ignore one missing operand.

Example results for Choose NumPy maximum or fmax for NaN values. maximum: [nan, nan, nan, 4.0]
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.

Element-wise maxima have an explicit missing-value rule

maximum and fmax both compare corresponding elements and follow NumPy broadcasting rules. Their important difference appears when either input is NaN: maximum propagates a NaN, while fmax returns the other operand when only one is NaN. If both values are NaN, the result remains NaN. [1, 2]

This choice is a data policy, not just a spelling preference. Propagation keeps a missing or invalid input visible in the result. Ignoring one NaN can be useful when that value means an unavailable alternative and the other operand is acceptable.

References: [1], [2]

Compare the functions on the same pairs

Run python examples/numpy_maximum_fmax_nan_policy.py. The first pair contains one NaN and one finite value, the next pair reverses those positions, and the third contains two NaNs. maximum returns NaN for the first three positions; fmax returns the finite value for either single-NaN pair and keeps NaN for the double-NaN pair. The final finite pair agrees. [1, 2]

The assertions check both output policies and the all-NaN case. This distinguishes element-wise maximum selection from nanmax, which reduces across an array axis and answers a different question. [1]

Executed with Python 3.12.14; exact stdout and SHA-256 recorded in private evidence.

import numpy as np

left = np.array([np.nan, 0.0, np.nan, 4.0])
right = np.array([1.0, np.nan, np.nan, 3.0])
propagated = np.maximum(left, right)
ignored_when_possible = np.fmax(left, right)
print("maximum:", propagated.tolist())
print("fmax:", ignored_when_possible.tolist())
assert np.isnan(propagated[:3]).all()
assert np.allclose(ignored_when_possible[:2], [1.0, 0.0])
assert np.isnan(ignored_when_possible[2])
assert ignored_when_possible[3] == 4.0

Recorded output

maximum: [nan, nan, nan, 4.0]
fmax: [1.0, 0.0, nan, 4.0]

References: [1], [2]

Choose the rule from the meaning of NaN

Use maximum when any missing operand should make the comparison result unknown. Use fmax only when the application explicitly treats a single NaN as absent and the remaining value is a valid fallback. Neither function explains why a value is missing, fills two missing inputs, or validates the data that produced them. [1, 2]

This example uses real-valued one-dimensional arrays with paired elements. It does not cover complex NaN ordering, masked arrays, reductions, or performance. The topic is an editorial-inference choice based on a gap in the existing guide titles, not a newly observed community trend or search-volume claim.

References: [1], [2]

Sources

  1. NumPy maximum
  2. NumPy fmax