bundles / numpy 2.4.3 / numpy / vectorize
class
numpy:vectorize
source: /numpy/__init__.py
Summary
Returns an object that acts like pyfunc, but takes arrays as input.
Extended Summary
Define a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns a single numpy array or a tuple of numpy arrays. The vectorized function evaluates pyfunc over successive tuples of the input arrays like the python map function, except it uses the broadcasting rules of numpy.
The data type of the output of vectorized is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.
Parameters
pyfunc: callable, optionalA python function or method. Can be omitted to produce a decorator with keyword arguments.
otypes: str or list of dtypes, optionalThe output data type. It must be specified as either a string of typecode characters or a list of data type specifiers. There should be one data type specifier for each output.
doc: str, optionalThe docstring for the function. If None, the docstring will be the
pyfunc.__doc__.excluded: set, optionalSet of strings or integers representing the positional or keyword arguments for which the function will not be vectorized. These will be passed directly to pyfunc unmodified.
cache: bool, optionalIf neither otypes nor signature are provided, and cache is
True, then cache the number of outputs.signature: string, optionalGeneralized universal function signature, e.g.,
(m,n),(n)->(m)for vectorized matrix-vector multiplication. If provided,pyfuncwill be called with (and expected to return) arrays with shapes given by the size of corresponding core dimensions. By default,pyfuncis assumed to take scalars as input and output.
Returns
out: callableA vectorized function if
pyfuncwas provided, a decorator otherwise.
Notes
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.
If neither otypes nor signature are specified, then a call to the function with the first argument will be used to determine the number of outputs. The results of this call will be cached if cache is True to prevent calling the function twice. However, to implement the cache, the original function must be wrapped which will slow down subsequent calls, so only do this if your function is expensive.
The new keyword argument interface and excluded argument support further degrades performance.
Examples
import numpy as np def myfunc(a, b): "Return a-b if a>b, otherwise return a+b" if a > b: return a - b else: return a + b✓
vfunc = np.vectorize(myfunc) vfunc([1, 2, 3, 4], 2)✓
vfunc.__doc__ vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`') vfunc.__doc__✓
out = vfunc([1, 2, 3, 4], 2) type(out[0]) vfunc = np.vectorize(myfunc, otypes=[float]) out = vfunc([1, 2, 3, 4], 2) type(out[0])✓
def mypolyval(p, x): _p = list(p) res = _p.pop(0) while _p: res = res*x + _p.pop(0) return res✓
vpolyval = np.vectorize(mypolyval, excluded={0, 'p'}) vpolyval([1, 2, 3], x=[0, 1]) vpolyval(p=[1, 2, 3], x=[0, 1])✓
import scipy.stats pearsonr = np.vectorize(scipy.stats.pearsonr, signature='(n),(n)->(),()')✓
pearsonr([[0, 1, 2, 3]], [[1, 2, 3, 4], [4, 3, 2, 1]])
✗convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)') convolve(np.eye(4), [1, 2, 1])✓
@np.vectorize def identity(x): return x identity([0, 1, 2]) @np.vectorize(otypes=[float]) def as_float(x): return x as_float([0, 1, 2])✓
See also
- frompyfunc
Takes an arbitrary Python function and returns a ufunc
Aliases
-
numpy.vectorize