numpy.apply_along_axis(func1d, axis, arr, *args)

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

Execute func1d(a[i],*args) where func1d takes 1-D arrays, a is the input array, and i is an integer that varies in order to apply the function along the given axis for each 1-D subarray in a.


func1d : function

This function should be able to take 1-D arrays. It is applied to 1-D slices of a along the specified axis.

axis : integer

Axis along which func1d is applied.

a : ndarray

Input array.

args : any

Additional arguments to func1d.


out : ndarray

The output array. The shape of out is identical to the shape of a, except along the axis dimension, whose length is equal to the size of the return value of func1d.

See also

Apply a function repeatedly over multiple axes.


>>> 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.])

Previous topic

Functional programming

Next topic


This Page

Quick search