numpy.ma.apply_along_axis
- 
numpy.ma.apply_along_axis(func1d, axis, arr, *args, **kwargs)[source]
- 
Apply a function to 1-D slices along the given axis. Execute func1d(a, *args)wherefunc1doperates on 1-D arrays andais a 1-D slice ofarralongaxis.This is equivalent to (but faster than) the following use of ndindexands_, which sets each ofii,jj, andkkto a tuple of indices:Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): f = func1d(arr[ii + s_[:,] + kk]) Nj = f.shape for jj in ndindex(Nj): out[ii + jj + kk] = f[jj]Equivalently, eliminating the inner loop, this can be expressed as: Ni, Nk = a.shape[:axis], a.shape[axis+1:] for ii in ndindex(Ni): for kk in ndindex(Nk): out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk])Parameters: - 
func1d : function (M,) -> (Nj…)
- 
This function should accept 1-D arrays. It is applied to 1-D slices of arralong the specified axis.
- 
axis : integer
- 
Axis along which arris sliced.
- 
arr : ndarray (Ni…, M, Nk…)
- 
Input array. 
- 
args : any
- 
Additional arguments to func1d.
- 
kwargs : any
- 
Additional named arguments to func1d.New in version 1.9.0. 
 Returns: - 
out : ndarray (Ni…, Nj…, Nk…)
- 
The output array. The shape of outis identical to the shape ofarr, except along theaxisdimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value offunc1d. So iffunc1dreturns a scalaroutwill have one fewer dimensions thanarr.
 See also - 
apply_over_axes
- Apply a function repeatedly over multiple axes.
 Examples>>> def my_func(a): ... """Average first and last element of a 1-D array""" ... return (a[0] + a[-1]) * 0.5 >>> b = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> np.apply_along_axis(my_func, 0, b) array([ 4., 5., 6.]) >>> np.apply_along_axis(my_func, 1, b) array([ 2., 5., 8.]) For a function that returns a 1D array, the number of dimensions in outarris the same asarr.>>> b = np.array([[8,1,7], [4,3,9], [5,2,6]]) >>> np.apply_along_axis(sorted, 1, b) array([[1, 7, 8], [3, 4, 9], [2, 5, 6]])For a function that returns a higher dimensional array, those dimensions are inserted in place of the axisdimension.>>> b = np.array([[1,2,3], [4,5,6], [7,8,9]]) >>> np.apply_along_axis(np.diag, -1, b) array([[[1, 0, 0], [0, 2, 0], [0, 0, 3]], [[4, 0, 0], [0, 5, 0], [0, 0, 6]], [[7, 0, 0], [0, 8, 0], [0, 0, 9]]])
- 
    © 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
    https://docs.scipy.org/doc/numpy-1.16.1/reference/generated/numpy.ma.apply_along_axis.html