The apply_along_axis() function is used to apply a specific function to a 1D slice along a specified axis.
The function syntax is as shown:
The parameters are as shown:
- func1d – specifies the function that operates on the 1-D slices.
- axis – specifies along which axis the array is sliced.
- arr – refers to the input array.
The function returns an output array except along the axis. The axis is removed and replaced with the dimensions equal to the shape of the function return value.
To apply the mean function along the zero axis of a one-dimensional array, we can do:
This should calculate and return the mean of elements in the input array along the specified axis.
An example return value is as shown:
The example below shows how function behaves in a two-dimensional array.
This should return: