[Numpy-discussion] confused with apply_along_axis()

Joris De Ridder joris@ster.kuleuven...
Wed Feb 7 08:11:53 CST 2007


Hi,

I'm confused by the output of apply_along_axis() in the following very simple 
example:

In [93]: a = arange(12.).reshape(2,2,3)
In [95]: a
Out[95]:
array([[[  0.,   1.,   2.],
        [  3.,   4.,   5.]],
       [[  6.,   7.,   8.],
        [  9.,  10.,  11.]]])
In [96]: def myfunc(b):
   ....:     print "slice:", b, "  middle value:", b[1]
   ....:     return b[1]
   ....:
In [97]: apply_along_axis(myfunc,2,a)
slice: [ 0.  1.  2.]   middle value: 1.0
slice: [ 3.  4.  5.]   middle value: 4.0
slice: [ 6.  7.  8.]   middle value: 7.0
slice: [  9.  10.  11.]   middle value: 10.0
Out[97]:
array([[  7.,   7.],
       [ 10.,  10.]])


I expected as output
array([[  1.,   4.],
       [ 7.,  10.]])

Why is this not the case? How exactly does apply_along_axis() use the output 
of myfunc() to produce its result?

Cheers,
J.

P.S. I'm working with Python 2.5 & Numpy 1.0

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the Numpy-discussion mailing list