numpy.vectorize
takes a function f:a->b and turns it into g:a[]->b[].
This works fine when a
and b
are scalars, but I can\'t t
A new parameter signature
in 1.12.0 does exactly what you what.
def f(x):
return x * np.array([1,1,1,1,1], dtype=np.float32)
g = np.vectorize(f, signature='()->(n)')
Then g(np.arange(4)).shape
will give (4L, 5L)
.
Here the signature of f
is specified. The (n)
is the shape of the return value, and the ()
is the shape of the parameter which is scalar. And the parameters can be arrays too. For more complex signatures, see Generalized Universal Function API.