I have seen a couple of codes using numpy.apply_along_axis and I always have to test the codes to see how this works 'cause I didn't understand the axis idea in Python yet.
For example, I tested this simple codes from the reference.
I can see that for the first case it was took the first column of each row of the matrix, and in the second case, the row itself was considered.
So I build an example to test how this works with an array of matrices (the problem that took me to this axis question), which can also be seen as a 3d matrix, where each row is a matrix, right?
a = [[[1,2,3],[2,3,4]],[[4,5,6],[9,8,7]]]
import numpy
data = numpy.array([b for b in a])
def my_func(x):
return (x[0] + x[-1]) * 0.5
b = numpy.apply_along_axis(my_func, 0, data)
b = numpy.apply_along_axis(my_func, 1, data)
Which gave me:
array([[ 2.5, 3.5, 4.5],
[ 5.5, 5.5, 5.5]])
And:
array([[ 1.5, 2.5, 3.5],
[ 6.5, 6.5, 6.5]])
For the first result I got what I expected. But for the second one, I though I would receive:
array([[ 2., 3.],
[ 5., 8.]])
Then I though that maybe should be an axis=2 and I got the previous result testing it. So, I'm wondering how this works to work it properly.
Thank you.