apply_along_axis(func1d, axis, arr, *args, **kwargs)¶
Apply a function to 1-D slices along the given axis.
LAX-backend implementation of
Original docstring below.
Execute func1d(a, *args, **kwargs) where func1d operates on 1-D arrays and a is a 1-D slice of arr along axis.
This is equivalent to (but faster than) the following use of ndindex and s_, which sets each of
kkto 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])
func1d (function (M,) -> (Nj...)) – This function should accept 1-D arrays. It is applied to 1-D slices of arr along the specified axis.
axis (integer) – Axis along which arr is sliced.
arr (ndarray (Ni..., M, Nk...)) – Input array.
args (any) – Additional arguments to func1d.
kwargs (any) – Additional named arguments to func1d.
out – The output array. The shape of out is identical to the shape of arr, except along the axis dimension. This axis is removed, and replaced with new dimensions equal to the shape of the return value of func1d. So if func1d returns a scalar out will have one fewer dimensions than arr.
- Return type
ndarray (Ni…, Nj…, Nk…)