take_along_axis#

ivy.take_along_axis(arr, indices, axis, /, *, mode='fill', out=None)[source]#

Take values from the input array by matching 1d index and data slices.

Parameters:
  • arr (Union[Array, NativeArray]) – The source array.

  • indices (Union[Array, NativeArray]) – The indices of the values to extract.

  • axis (int) – The axis over which to select values. If axis is None, arr is treated as a flattened 1D array.

  • mode (str, default: 'fill') – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds indices will be handled.

  • out (Optional[Array], default: None) – The output array.

Return type:

Array

Returns:

ret – The returned array has the same shape as indices.

Examples

>>> arr = ivy.array([[4, 3, 5], [1, 2, 1]])
>>> indices = ivy.array([[0, 1, 1], [2, 0, 0]])
>>> y = ivy.take_along_axis(arr, indices, 1)
>>> print(y)
ivy.array([[4, 3, 3], [1, 1, 1]])
Array.take_along_axis(self, indices, axis, /, *, mode='fill', out=None)[source]#

ivy.Array instance method variant of ivy.take_along_axis. This method simply wraps the function, and so the docstring for ivy.take_along_axis also applies to this method with minimal changes.

Parameters:
  • self (Array) – The source array.

  • indices (Array) – The indices of the values to extract.

  • axis (int) – The axis over which to select values.

  • mode (str, default: 'fill') – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds indices will be handled.

  • out (Optional[Array], default: None) – Optional output, for writing the result to.

Return type:

Array

Returns:

ret – The returned array has the same shape as indices.

Examples

>>> arr = ivy.array([[4, 3, 5], [1, 2, 1]])
>>> indices = ivy.array([[0, 1, 1], [2, 0, 0]])
>>> y = arr.take_along_axis(indices, 1)
>>> print(y)
ivy.array([[4, 3, 3], [1, 1, 1]])
Container.take_along_axis(self, indices, axis, mode='fill', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.take_along_axis. This method simply wraps the function, and so the docstring for ivy.take_along_axis also applies to this method with minimal changes.

Parameters:
  • self (Union[Container, Array, NativeArray]) – container with array inputs.

  • indices (Union[Container, Array, NativeArray]) – container with indices of the values to extract.

  • axis (Union[int, Container]) – The axis over which to select values. If axis is None, then arr and indices must be 1-D sequences of the same length.

  • mode (Union[str, Container], default: 'fill') – One of: ‘clip’, ‘fill’, ‘drop’. Parameter controlling how out-of-bounds indices will be handled.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The keychains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output container, for writing the result to.

Return type:

Container

Returns:

ret – a container with arrays of the same shape as those in indices.

Examples

>>> arr = ivy.Container(a=ivy.array([[1, 2], [3, 4]]),                                b=ivy.array([[5, 6], [7, 8]]))
>>> indices = ivy.Container(a=ivy.array([[0, 0], [1, 1]]),                                    b=ivy.array([[1, 0], [1, 0]]))
>>> arr.take_along_axis(indices, axis=1)
[{
    a: ivy.array([[1, 1],
                  [4, 4]]),
    b: ivy.array([[6, 5],
                    [8, 7]])
}]