NumPy apply_over_axes()

The apply_over_axes() method allows you to apply a function repeatedly over multiple axes.


apply_over_axes() Syntax

The syntax of apply_over_axes() is:

numpy.apply_over_axes(func, array, axis)

apply_over_axes() Arguments

The apply_over_axes() method takes the following arguments:

  • func - the function to apply
  • axis - the axis along which the functions are applied
  • array - the input array

Note: The func should take two arguments, an input array and axis.

apply_over_axes() Return Value

The apply_over_axes() method returns the resultant array with functions applied.


Example 1: Apply a Function Along Multiple Axes

Output

Original Array:
[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]
Sum along axes (0, 1):
 [[[12 16]]]
Sum along axes (0, 2):
 [[[10]
  [18]]]

Example 2: Apply a lambda Function in an Array

We can return an array of values from the function.

Output

[[ 6]
 [15]
 [24]]

Recommended Reading

Numpy apply_along_axis()