numpy 1.22.4 Pypi GitHub Homepage
Other Docs
ParametersReturnsBackRef
apply_along_axis(func1d, axis, arr, *args, **kwargs)

Execute :None:None:`func1d(a, *args, **kwargs)` where :None:None:`func1d` operates on 1-D arrays and a is a 1-D slice of :None:None:`arr` along :None:None:`axis`.

This is equivalent to (but faster than) the following use of ndindex and :None:None:`s_`, which sets each of ii , jj , and kk to 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 :None:None:`arr` along the specified axis.

axis : integer

Axis along which :None:None:`arr` is sliced.

arr : ndarray (Ni..., M, Nk...)

Input array.

args : any

Additional arguments to :None:None:`func1d`.

kwargs : any

Additional named arguments to :None:None:`func1d`.

versionadded

Returns

out : ndarray (Ni..., Nj..., Nk...)

The output array. The shape of :None:None:`out` is identical to the shape of :None:None:`arr`, except along the :None:None:`axis` dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of :None:None:`func1d`. So if :None:None:`func1d` returns a scalar :None:None:`out` will have one fewer dimensions than :None:None:`arr`.

Apply a function to 1-D slices along the given axis.

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 :None:None:`outarr` is the same as :None:None:`arr`.

>>> 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 :None:None:`axis` dimension.

>>> 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]]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.routines.apply_along_axis numpy.ma.extras.apply_over_axes numpy.take numpy.apply_over_axes

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /numpy/lib/shape_base.py#267
type: <class 'function'>
Commit: