ML Foundations
Handle values outside NumPy interpolation samples
See when interpolation uses endpoint values and how to mark outside queries.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Outside points use endpoint values by default
NumPy interp computes a one-dimensional piecewise linear interpolant from sample coordinates and their values. By default, queries below the first sample get the first sample value, and queries above the last sample get the last value. [1]
This endpoint fill can look like linear extrapolation but it remains constant outside the sample range. When comparing implementations, inspect both the query coordinates and the chosen boundary behavior. [1]
References: [1]
Mark outside queries explicitly
Run python examples/numpy_interp_endpoint_fill.py. Sample points one, two and four have values ten, twenty and forty. For queries zero through five in the fixture, the default returns endpoint values outside the interval, while the query at three interpolates to thirty. [1]
The second call supplies NaN for left and right. The two outside queries are marked, while queries exactly at the first and last sample remain valid. Assertions check the boundary mask and interior values. [1]
Executed with Python 3.12.14; stdout and checksum recorded in private evidence.
import numpy as np
xp = np.array([1.0, 2.0, 4.0])
fp = np.array([10.0, 20.0, 40.0])
queries = np.array([0.0, 1.0, 3.0, 4.0, 5.0])
default = np.interp(queries, xp, fp)
marked = np.interp(queries, xp, fp, left=np.nan, right=np.nan)
assert default.tolist() == [10.0, 10.0, 30.0, 40.0, 40.0]
assert np.isnan(marked[[0, 4]]).all()
assert marked[1:4].tolist() == [10.0, 30.0, 40.0]
print("queries:", queries.tolist())
print("default:", default.tolist())
print("out-of-range marked:", marked.tolist())
print("outside mask:", np.isnan(marked).tolist())
Recorded output
queries: [0.0, 1.0, 3.0, 4.0, 5.0] default: [10.0, 10.0, 30.0, 40.0, 40.0] out-of-range marked: [nan, 10.0, 30.0, 40.0, nan] outside mask: [True, False, False, False, True]
References: [1]
Validate assumptions before interpolation
Without a period, sample coordinates should be strictly increasing. For a nonempty numeric sequence, checking that all adjacent differences are positive is one way to detect duplicate or descending coordinates before the call. NumPy documents that non-increasing sample positions can yield meaningless results. [1, 2]
Choose a missing-value policy that fits the caller: NaN marks outside queries in this example, but downstream code must handle those values. With the period argument, NumPy ignores left and right and applies periodic handling instead. This example does not calculate linear extrapolation. [1]