numpy 1.22.4 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None)

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 :None:None:`vectorized` is determined by calling the function with the first element of the input. This can be avoided by specifying the otypes argument.

Notes

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.

If otypes is not 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 :None:None:`cache` is :None:None:`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 :None:None:`excluded` argument support further degrades performance.

Parameters

pyfunc : callable

A python function or method.

otypes : str or list of dtypes, optional

The 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, optional

The docstring for the function. If None, the docstring will be the pyfunc.__doc__ .

excluded : set, optional

Set 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.

versionadded
cache : bool, optional

If :None:None:`True`, then cache the first function call that determines the number of outputs if otypes is not provided.

versionadded
signature : string, optional

Generalized universal function signature, e.g., (m,n),(n)->(m) for vectorized matrix-vector multiplication. If provided, pyfunc will be called with (and expected to return) arrays with shapes given by the size of corresponding core dimensions. By default, pyfunc is assumed to take scalars as input and output.

versionadded

Returns

vectorized : callable

Vectorized function.

Generalized function class.

See Also

frompyfunc

Takes an arbitrary Python function and returns a ufunc

Examples

>>> 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) array([3, 4, 1, 2])

The docstring is taken from the input function to vectorize unless it is specified:

>>> vfunc.__doc__
'Return a-b if a>b, otherwise return a+b'
>>> vfunc = np.vectorize(myfunc, doc='Vectorized `myfunc`')
... vfunc.__doc__ 'Vectorized `myfunc`'

The output type is determined by evaluating the first element of the input, unless it is specified:

>>> out = vfunc([1, 2, 3, 4], 2)
... type(out[0]) <class 'numpy.int64'>
>>> vfunc = np.vectorize(myfunc, otypes=[float])
... out = vfunc([1, 2, 3, 4], 2)
... type(out[0]) <class 'numpy.float64'>

The :None:None:`excluded` argument can be used to prevent vectorizing over certain arguments. This can be useful for array-like arguments of a fixed length such as the coefficients for a polynomial as in polyval :

>>> 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=['p'])
... vpolyval(p=[1, 2, 3], x=[0, 1]) array([3, 6])

Positional arguments may also be excluded by specifying their position:

>>> vpolyval.excluded.add(0)
... vpolyval([1, 2, 3], x=[0, 1]) array([3, 6])

The :None:None:`signature` argument allows for vectorizing functions that act on non-scalar arrays of fixed length. For example, you can use it for a vectorized calculation of Pearson correlation coefficient and its p-value:

>>> 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]]) (array([ 1., -1.]), array([ 0., 0.]))

Or for a vectorized convolution:

>>> convolve = np.vectorize(np.convolve, signature='(n),(m)->(k)')
... convolve(np.eye(4), [1, 2, 1]) array([[1., 2., 1., 0., 0., 0.], [0., 1., 2., 1., 0., 0.], [0., 0., 1., 2., 1., 0.], [0., 0., 0., 1., 2., 1.]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

numpy.frompyfunc numpy.vectorize

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/__init__.py#None
type: <class 'type'>
Commit: