🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.4 Manual
Take values from the input array by matching 1d index and data slices · This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.apply_over_axes.html
numpy.apply_over_axes — NumPy v2.5 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 1.26 › reference › generated › numpy.ma.apply_over_axes.html
numpy.ma.apply_over_axes — NumPy v1.26 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ma.apply_over_axes.html
numpy.ma.apply_over_axes — NumPy v2.1 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.1 Manual
Take values from the input array by matching 1d index and data slices · This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ma.apply_over_axes.html
numpy.ma.apply_over_axes — NumPy v2.2 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 1.22 › reference › generated › numpy.apply_over_axes.html
numpy.apply_over_axes — NumPy v1.22 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 1.20 › reference › generated › numpy.apply_over_axes.html
numpy.apply_over_axes — NumPy v1.20 Manual
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 than a, a dimension is inserted ...
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.apply_over_axes.html
numpy.apply_over_axes — NumPy v2.0 Manual
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 than a, a dimension is inserted ...
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.take.html
numpy.take — NumPy v2.5 Manual
Take elements from an array along an axis · When axis is not None, this function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. A call such as np.take(arr, indices, axis=3) is equivalent ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.6.dev0 Manual
Take values from the input array by matching 1d index and data slices · This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths
Top answer
1 of 1
1

apply_over_axes() applies the function sequentially to each specified axis, and it preserves the original dimensionality by keeping singleton dimensions for the axes it operates on - numpy.apply_over_axes

For your scenario you can try np.sum() with a tuple of axes

c_simplified = b.sum(axis=(1,2))
print(c_simplified.shape)

In a Picture

import numpy as np
b = np.random.randint(6, size=(100, 101, 102))

result_sum = b.sum(axis=(1, 2))  

result_max = b.max(axis=(1, 2)) 

result_mean = b.mean(axis=(1, 2))

output#

Sum shape: (100,)
Max shape: (100,)
Mean shape: (100,)

Edit 1:

f: Specifies the axis along which np.sum operates. Without it, the function cannot correctly sum over the intended axis.

axes parameter in np.apply_over_axes: Specifies the axes over which the function f should be applied iteratively, reducing them to size 1.

The axis parameter in your function f is necessary because np.sum needs to know which axis to operate on. Without it, the function call will fail, as np.sum defaults to summing over all axes if axis isn't specified, which conflicts with the iterative application of np.apply_over_axes

More can be found at - https://numpy.org/doc/stable/reference/generated/numpy.apply_over_axes.html

Summing Over Axes 1 and 2

import numpy as np

def custom_sum(array, axes):
 
    axes = tuple(axes)
    return np.sum(array, axis=axes)

b = np.random.randint(6, size=(100, 101, 102))
c = custom_sum(b, axes=(1, 2))
print(c.shape)  

output

(100,)

Second Largest Element Over Axes 1 and 2

you can reshape the array, sort along the flattened axes

axes = tuple(axes)
flattened = np.reshape(array, (-1,) + array.shape[len(axes):])
sorted_array = np.sort(flattened, axis=1)
print(sorted_array[:, -2])
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.apply_over_axes.html
numpy.apply_over_axes — NumPy v2.6.dev0 Manual
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 than a, a dimension is inserted ...
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.take.html
numpy.take — NumPy v1.13 Manual
Take elements from an array along an axis · This function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take_along_axis.html
numpy.take_along_axis — NumPy v2.2 Manual
Take values from the input array by matching 1d index and data slices · This iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter. These slices can be different lengths
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.take.html
numpy.take — NumPy v2.1 Manual
Take elements from an array along an axis · When axis is not None, this function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. A call such as np.take(arr, indices, axis=3) is equivalent ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.take.html
numpy.take — NumPy v2.2 Manual
Take elements from an array along an axis · When axis is not None, this function does the same thing as “fancy” indexing (indexing arrays using arrays); however, it can be easier to use if you need elements along a given axis. A call such as np.take(arr, indices, axis=3) is equivalent ...
🌐
Medium
medium.com › @weidagang › understanding-axes-in-numpy-8c889794e541
Understanding Axes in NumPy. Your Key to Array Manipulation | by Dagang Wei | Medium
May 28, 2024 - Functions use axes: Many NumPy operations (like sum, mean, max) take an axis argument to determine the direction of the calculation.