I have a question regarding output dimensions of np.apply_along_axis
.
In the first case values of x
for the lambda function are [1 2]
, [1 3]
, [1 4]
and they have shape (2,)
since we split across axis=0
. Each output of the lambda function has shape (2,2)
:
import numpy as np
out = np.apply_along_axis(
func1d=lambda x: x + np.asarray([[1,2],[3,4]]),
axis=0,
arr=np.asarray([[1,1,1],[2,3,4]])
)
print(out.shape)
# (2, 2, 3)
print(out)
# [[[2 2 2]
# [4 5 6]]
# [[4 4 4]
# [6 7 8]]]
In the second case values of x
for the lambda function are [1]
, [2]
, [3]
and they have shape (1,)
since we split across axis=1
. Each output of the lambda function has shape (2,2)
:
import numpy as np
out = np.apply_along_axis(
func1d=lambda x: x + np.asarray([[1,2],[3,4]]),
axis=1,
arr=np.asarray([[1,2,3]]).T
)
print(out.shape)
# (3, 2, 2)
print(out)
# [[[2 3]
# [4 5]]
# [[3 4]
# [5 6]]
# [[4 5]
# [6 7]]]
Can you please explain why in the first case the output has shape (2,2,3)
and in the second case its shape is (3,2,2)
given that the output from lambda function has the same shape?