MCPcopy Create free account
hub / github.com/numpy/numpy / apply_along_axis

Function apply_along_axis

numpy/lib/shape_base.py:268–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

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

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 8

normalize_axis_indexFunction · 0.90
transposeFunction · 0.90
ndindexClass · 0.90
asanyarrayFunction · 0.85
nextFunction · 0.50
zerosFunction · 0.50
__array_prepare__Method · 0.45
__array_wrap__Method · 0.45

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