ML Foundations
Validate per-feature bounds before NumPy clipping
Detect inverted clipping intervals before they silently collapse feature values.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
An inverted interval is still accepted
A preprocessing step may clip each feature to a different interval. NumPy clip supports bounds that broadcast against the input, but it does not check that each lower bound is less than or equal to its upper bound. For an inverted interval, the documented result is the upper bound. A configuration error can therefore turn a feature into a constant without raising an exception. [1]
The input fixture uses three finite features and one inverted pair. It illustrates validation of the bounds themselves; it is not a guide to choosing statistical thresholds.
Check each pair before transforming
Run python examples/numpy_clip_bounds.py. The raw clip result shows the second feature replaced by its upper bound even though its lower bound is larger. The elementwise comparison lower > upper identifies the offending pair, and np.any reduces that mask to one decision. [1, 2]
The example prints a rejection rather than sending the raw result downstream. After correcting the second interval, the same feature value is retained. Assertions check both the invalid mask and the two outputs. The example uses equally shaped one-dimensional arrays so the correspondence between each feature and its bounds is explicit.
Do not automatically swap bounds without deciding whether inversion means a typo or a deeper configuration error. For production code, reject the configuration before applying the transform, and include enough context to identify the affected feature.
Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.
import numpy as np
values = np.array([0.0, 5.0, 10.0])
lower = np.array([0.0, 8.0, 1.0])
upper = np.array([2.0, 3.0, 9.0])
raw = np.clip(values, lower, upper)
invalid = lower > upper
assert raw.tolist() == [0.0, 3.0, 9.0]
assert invalid.tolist() == [False, True, False]
print("raw clip:", raw.tolist())
print("inverted bounds:", invalid.tolist())
if np.any(invalid):
print("validation: rejected")
else:
print("validation: accepted")
corrected = np.clip(values, [0.0, 3.0, 1.0], [2.0, 8.0, 9.0])
assert corrected.tolist() == [0.0, 5.0, 9.0]
print("corrected clip:", corrected.tolist())
Recorded output
raw clip: [0.0, 3.0, 9.0] inverted bounds: [False, True, False] validation: rejected corrected clip: [0.0, 5.0, 9.0]
Define the rest of the input contract
Real bounds may broadcast over batches or feature axes. Validate their shapes and intended axis before relying on an elementwise check; otherwise a valid broadcast can still apply the wrong limits to the wrong feature. [1]
This fixture uses finite values and bounds. Missing or nonfinite bounds need an explicit policy because a simple greater-than comparison is not a complete validity test for NaN. The example makes no model-quality or performance claim.