[Numpy-discussion] Apply a function to all indices
Pauli Virtanen
pav at iki.fi
Fri Feb 26 06:51:35 EST 2010
pe, 2010-02-26 kello 12:43 +0100, Ernest Adrogué kirjoitti:
[clip]
> Or if you want to produce a different array of the same shape
> as the original, then you probably need a vectorised function.
>
> def myfunc(x):
> print 'myfunc of', x
> if x > 0.8:
> return x + 2
> else:
> return x
> vect_func = numpy.frompyfunc(myfunc, 1, 1)
> vect_func(a)
Note that frompyfunc always makes the vectorized function return object
arrays, which may not be what is wanted. Instead, one can use
numpy.vectorize.
--
Pauli Virtanen
More information about the NumPy-Discussion
mailing list