numpy.vectorize

class numpy.vectorize(pyfunc, otypes='', doc=None)

Generalized function class.

Define a vectorized function which takes nested sequence of objects or numpy arrays as inputs and returns a numpy array as output, evaluating the function over successive tuples of the input arrays like the python map function except it uses the broadcasting rules of numpy.

Data-type of 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 as either a string of typecode characters or a list of data-types specifiers. There should be one data-type specifier for each output.

Parameters:

f : callable

A Python function or method.

Examples

>>> def myfunc(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])

Previous topic

numpy.apply_over_axes

Next topic

numpy.frompyfunc

This Page

Quick search