NumPy 函数式编程¶
参考:
NumPy 函数式编程主要有以下几种方式
apply_along_axis(func1d, axis, arr, *args, ...)
Apply a function to 1-D slices along the given axis.apply_over_axes(func, a, axes)
Apply a function repeatedly over multiple axes.vectorize(pyfunc[, otypes, doc, excluded, ...])
Generalized function class.frompyfunc(func, nin, nout)
Takes an arbitrary Python function and returns a NumPy ufunc.piecewise(x, condlist, funclist, *args, **kw)
Evaluate a piecewise-defined function.
numpy.vectorize¶
class numpy.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 an single or tuple of numpy array as output. 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.
基于输入的 python func 返回一个向量化的函数
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])