[Numpy-discussion] Apply a function to all indices

Ernest Adrogué eadrogue@gmx....
Fri Feb 26 07:12:58 CST 2010


26/02/10 @ 13:51 (+0200), thus spake Pauli Virtanen:
> 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.

Thanks for the tip. I didn't know that...
Also, frompyfunc appears to crash python when the last argument is 0:

In [9]: func=np.frompyfunc(lambda x: x, 1, 0)

In [10]: func(np.arange(5))
Violació de segment

This with Python 2.5.5, Numpy 1.3.0 on GNU/Linux.

Cheers.

> -- 
> Pauli Virtanen
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list