ML Foundations
Avoid the extra probe call in NumPy vectorize
See how NumPy vectorize infers output types from the first input and how otypes makes that call explicit.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
Output inference calls the Python function on the first value
numpy.vectorize lets a Python callable accept arrays and follows NumPy broadcasting rules, but it evaluates the callable over input values. When you omit otypes, NumPy calls the function with the first input value to infer the output type. That probe is separate from the element-by-element evaluation, so a function with side effects can observe the first value twice. [1]
This is useful to know when the callable records work, consumes an iterator, changes state, or performs an operation that must happen once per element. It is a consequence of output-type inference, not a property of the input array itself. [1]
References: [1]
Declare the output type when it is known
The example records each scalar passed to the callable. With inferred output type, the first value appears twice in the call log. Supplying otypes=[int] provides the output type before iteration, and the recorded calls then match the three input values one for one. The printed results confirm that the output values are unchanged in this fixture. [1]
For a Python function that returns multiple outputs, provide one type per output. Use an explicit, correct type for the function’s actual return values; the option does not validate domain rules or make the Python callable a compiled NumPy operation. [1]
Executed with Python 3.12.14; exact stdout and SHA-256 recorded in review evidence.
import numpy as np
calls = []
def twice(value):
calls.append(int(value))
return int(value) * 2
values = np.array([1, 2, 3])
inferred = np.vectorize(twice)(values)
print("inferred values:", inferred.tolist())
print("inferred calls:", calls)
assert calls == [1, 1, 2, 3]
calls.clear()
declared = np.vectorize(twice, otypes=[int])(values)
print("declared values:", declared.tolist())
print("declared calls:", calls)
assert calls == [1, 2, 3]
assert np.array_equal(inferred, declared)
Recorded output
inferred values: [2, 4, 6] inferred calls: [1, 1, 2, 3] declared values: [2, 4, 6] declared calls: [1, 2, 3]
References: [1]
Distinguish a wrapper from a ufunc
NumPy documents vectorize as a convenience wrapper whose implementation is essentially a loop, so this guide makes no speed claim. frompyfunc is a separate way to create a ufunc from a Python function, but its results are always object arrays. These interfaces solve different API needs, and the output dtype behavior matters when downstream NumPy operations expect numeric arrays. [1, 2]
The test uses a deterministic three-element input and a function that returns Python integers. It does not benchmark either interface or test broadcasting, multiple outputs, exceptions, or mutable arguments.