take_along_axis#
- named_arrays.take_along_axis(a, indices, axis)#
Take values from the input array by matching indices along the given axis.
This is the named-array analogue of
numpy.take_along_axis(). Unlike thenumpyversion,indicesis broadcast againstaby matching axis names, soindicesonly needs to define the axes it varies along.- Parameters:
a (ArrayT) – The source array to take values from.
indices (AbstractScalarArray | ArrayT) – The integer indices to take along
axis. For scalars and uncertain scalars this is an instance ofnamed_arrays.AbstractScalarArray. For vectors this may either be a scalar (the same indices are used for every component) or a vector of the same type asa(a separate set of indices for each component).axis (str) – The axis of
aalong which the values are taken. The result replaces this axis with the axes ofindices.
- Return type:
ArrayT
See also
numpy.take_along_axis()The equivalent
numpyfunction.numpy.argsort()Produces indices suitable for this function.
named_arrays.AbstractArray.take_along_axis()A method version of this function.
Examples
Sort an array by taking values along an axis using indices from
numpy.argsort().import numpy as np import named_arrays as na a = na.ScalarArray(np.array([3, 1, 2, 0]), axes="x") indices = na.ScalarArray(np.argsort(a.ndarray), axes="x") na.take_along_axis(a, indices, axis="x")
ScalarArray( ndarray=[0, 1, 2, 3], axes=('x',), )