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 ...
Stack Overflow
stackoverflow.com › questions › 43692119 › numpy-take-along-2-axes
python - numpy `take` along 2 axes - Stack Overflow
I have a 3D array a of data and a 2D array b of indices. I need to take a sub-array of a along the 3rd axis, using the indices from b. I can do it with take like this: a = np.arange(24).reshape((2...
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 › 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
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 › 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 ...
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 › stable › reference › generated › numpy.ma.apply_over_axes.html
numpy.ma.apply_over_axes — NumPy v2.3 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.
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 › 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 ...
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
Top answer 1 of 2
47
If you have a sufficiently recent NumPy, you can do
m_mean = m.mean(axis=(1, 2))
I believe this was introduced in 1.7, though I'm not sure. The documentation was only updated to reflect this in 1.10, but it worked earlier than that.
If your NumPy is too old, you can take the mean a bit more manually:
m_mean = m.sum(axis=2).sum(axis=1) / np.prod(m.shape[1:3])
These will both produce 1-dimensional results. If you really want that extra length-1 axis, you can do something like m_mean = m_mean[:, np.newaxis] to put the extra axis there.
2 of 2
2
You can also use the numpy.mean() ufunc and pass the output array as an argument to out= as in:
np.mean(m, axis=(1, 2), out=m_mean)
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.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 › 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 › stable › reference › generated › numpy.put_along_axis.html
numpy.put_along_axis — NumPy v1.26 Manual
January 31, 2021 - The axis to take 1d slices along.