Apply a function repeatedly over multiple axes. `func` is called as `res = func(a, axis)`, where `axis` is the first element of `axes`. The result `res` of the function call must have either the same dimensions as `a` or one less dimension. If `res` has one less dimension tha
(func, a, axes)
| 420 | |
| 421 | @array_function_dispatch(_apply_over_axes_dispatcher) |
| 422 | def apply_over_axes(func, a, axes): |
| 423 | """ |
| 424 | Apply a function repeatedly over multiple axes. |
| 425 | |
| 426 | `func` is called as `res = func(a, axis)`, where `axis` is the first |
| 427 | element of `axes`. The result `res` of the function call must have |
| 428 | either the same dimensions as `a` or one less dimension. If `res` |
| 429 | has one less dimension than `a`, a dimension is inserted before |
| 430 | `axis`. The call to `func` is then repeated for each axis in `axes`, |
| 431 | with `res` as the first argument. |
| 432 | |
| 433 | Parameters |
| 434 | ---------- |
| 435 | func : function |
| 436 | This function must take two arguments, `func(a, axis)`. |
| 437 | a : array_like |
| 438 | Input array. |
| 439 | axes : array_like |
| 440 | Axes over which `func` is applied; the elements must be integers. |
| 441 | |
| 442 | Returns |
| 443 | ------- |
| 444 | apply_over_axis : ndarray |
| 445 | The output array. The number of dimensions is the same as `a`, |
| 446 | but the shape can be different. This depends on whether `func` |
| 447 | changes the shape of its output with respect to its input. |
| 448 | |
| 449 | See Also |
| 450 | -------- |
| 451 | apply_along_axis : |
| 452 | Apply a function to 1-D slices of an array along the given axis. |
| 453 | |
| 454 | Notes |
| 455 | ----- |
| 456 | This function is equivalent to tuple axis arguments to reorderable ufuncs |
| 457 | with keepdims=True. Tuple axis arguments to ufuncs have been available since |
| 458 | version 1.7.0. |
| 459 | |
| 460 | Examples |
| 461 | -------- |
| 462 | >>> import numpy as np |
| 463 | >>> a = np.arange(24).reshape(2,3,4) |
| 464 | >>> a |
| 465 | array([[[ 0, 1, 2, 3], |
| 466 | [ 4, 5, 6, 7], |
| 467 | [ 8, 9, 10, 11]], |
| 468 | [[12, 13, 14, 15], |
| 469 | [16, 17, 18, 19], |
| 470 | [20, 21, 22, 23]]]) |
| 471 | |
| 472 | Sum over axes 0 and 2. The result has same number of dimensions |
| 473 | as the original array: |
| 474 | |
| 475 | >>> np.apply_over_axes(np.sum, a, [0,2]) |
| 476 | array([[[ 60], |
| 477 | [ 92], |
| 478 | [124]]]) |
| 479 |
searching dependent graphs…