MCPcopy
hub / github.com/numpy/numpy / apply_along_axis

Function apply_along_axis

numpy/lib/_shape_base_impl.py:277–414  ·  view source on GitHub ↗

Apply a function to 1-D slices along the given axis. 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 ``i

(func1d, axis, arr, *args, **kwargs)

Source from the content-addressed store, hash-verified

275
276@array_function_dispatch(_apply_along_axis_dispatcher)
277def apply_along_axis(func1d, axis, arr, *args, **kwargs):
278 """
279 Apply a function to 1-D slices along the given axis.
280
281 Execute `func1d(a, *args, **kwargs)` where `func1d` operates on 1-D arrays
282 and `a` is a 1-D slice of `arr` along `axis`.
283
284 This is equivalent to (but faster than) the following use of `ndindex` and
285 `s_`, which sets each of ``ii``, ``jj``, and ``kk`` to a tuple of indices::
286
287 Ni, Nk = a.shape[:axis], a.shape[axis+1:]
288 for ii in ndindex(Ni):
289 for kk in ndindex(Nk):
290 f = func1d(arr[ii + s_[:,] + kk])
291 Nj = f.shape
292 for jj in ndindex(Nj):
293 out[ii + jj + kk] = f[jj]
294
295 Equivalently, eliminating the inner loop, this can be expressed as::
296
297 Ni, Nk = a.shape[:axis], a.shape[axis+1:]
298 for ii in ndindex(Ni):
299 for kk in ndindex(Nk):
300 out[ii + s_[...,] + kk] = func1d(arr[ii + s_[:,] + kk])
301
302 Parameters
303 ----------
304 func1d : function (M,) -> (Nj...)
305 This function should accept 1-D arrays. It is applied to 1-D
306 slices of `arr` along the specified axis.
307 axis : integer
308 Axis along which `arr` is sliced.
309 arr : ndarray (Ni..., M, Nk...)
310 Input array.
311 args : any
312 Additional arguments to `func1d`.
313 kwargs : any
314 Additional named arguments to `func1d`.
315
316 Returns
317 -------
318 out : ndarray (Ni..., Nj..., Nk...)
319 The output array. The shape of `out` is identical to the shape of
320 `arr`, except along the `axis` dimension. This axis is removed, and
321 replaced with new dimensions equal to the shape of the return value
322 of `func1d`. So if `func1d` returns a scalar `out` will have one
323 fewer dimensions than `arr`.
324
325 See Also
326 --------
327 apply_over_axes : Apply a function repeatedly over multiple axes.
328
329 Examples
330 --------
331 >>> import numpy as np
332 >>> def my_func(a):
333 ... \"\"\"Average first and last element of a 1-D array\"\"\"
334 ... return (a[0] + a[-1]) * 0.5

Callers 9

test_simpleMethod · 0.90
test_simple101Method · 0.90
test_3dMethod · 0.90
test_subclassMethod · 0.90
test_scalar_arrayMethod · 0.90
test_0d_arrayMethod · 0.90
test_axis_insertionMethod · 0.90

Calls 6

transposeFunction · 0.90
ndindexClass · 0.90
zeros_likeFunction · 0.90
asanyarrayFunction · 0.85
zerosFunction · 0.85
wrapMethod · 0.80

Tested by 9

test_simpleMethod · 0.72
test_simple101Method · 0.72
test_3dMethod · 0.72
test_subclassMethod · 0.72
test_scalar_arrayMethod · 0.72
test_0d_arrayMethod · 0.72
test_axis_insertionMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…