Apply a function to all indices

Hi, I want to apply a function to all indices of an array that fullfill a certain condition. What I tried: ---------------------8<-------------------------------- import numpy def myfunc(x): print 'myfunc of', x a = numpy.random.random((2,3,4)) numpy.apply_along_axis(myfunc, 0, numpy.where(a > 0.8)) ---------------------8<-------------------------------- But this prints just the first index vector and then shows a TypeError: object of type 'NoneType' has no len() What is wrong with my code and how can I do it right? Best regards Ole

Hi, 26/02/10 @ 11:23 (+0100), thus spake Ole Streicher:
Hi,
I want to apply a function to all indices of an array that fullfill a certain condition.
What I tried:
---------------------8<-------------------------------- import numpy
def myfunc(x): print 'myfunc of', x
a = numpy.random.random((2,3,4)) numpy.apply_along_axis(myfunc, 0, numpy.where(a > 0.8)) ---------------------8<--------------------------------
But this prints just the first index vector and then shows a TypeError: object of type 'NoneType' has no len()
What is wrong with my code and how can I do it right?
Your function returns nothing (i.e. None), and the numpy function was expecting a scalar or an array-like object, that's why it fails. It depends on what exactly you want to do. If you just want to iterate over the array, try something liks this for element in a[a > 0.8]: myfunc(element) 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) But in this case, myfunc() has to return a scalar value for each element in a.
Best regards
Ole
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

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

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

On 26-Feb-10, at 8:12 AM, Ernest Adrogué wrote:
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.
(previous reply mysteriously didn't make it to the list...) Still happening to me in latest svn. Can you file a ticket? http://projects.scipy.org/numpy/report Thanks, David

28/02/10 @ 01:56 (-0500), thus spake David Warde-Farley:
On 26-Feb-10, at 8:12 AM, Ernest Adrogué wrote:
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.
(previous reply mysteriously didn't make it to the list...)
Still happening to me in latest svn. Can you file a ticket? http://projects.scipy.org/numpy/report
Filed. http://projects.scipy.org/numpy/ticket/1416 Cheers.

Hello Ernest, Ernest Adrogué <eadrogue@gmx.net> writes:
It depends on what exactly you want to do. If you just want to iterate over the array, try something liks this for element in a[a > 0.8]: myfunc(element)
No; I need to iterate over the *indices*, not over the elements. a = numpy.random.random((2,3,4)) for index in ???(a > 0.5): print index[0], index[1], index[2] Best regards Ole

26/02/10 @ 13:31 (+0100), thus spake Ole Streicher:
Hello Ernest,
Ernest Adrogué <eadrogue@gmx.net> writes:
It depends on what exactly you want to do. If you just want to iterate over the array, try something liks this for element in a[a > 0.8]: myfunc(element)
No; I need to iterate over the *indices*, not over the elements.
a = numpy.random.random((2,3,4))
for index in ???(a > 0.5): print index[0], index[1], index[2]
Ah, I think np.argwhere does that: for index in numpy.argwhere(a > 0.5): print index index is an array, so it can be indexed, for example: for index in numpy.argwhere(a > 0.5): print a[index[0], index[1], index[2]] prints all elements in a that are greater than 0.5. Bye.
Best regards
Ole
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
David Warde-Farley
-
Ernest Adrogué
-
Ole Streicher
-
Pauli Virtanen