take#

ivy.take(x, indices, /, *, axis=None, mode='fill', fill_value=None, out=None)[source]#

Return elements of an array along an axis.

Note

Conceptually, take(x, indices, axis=3) is equivalent to x[:,:,:,indices,…]; however, explicit indexing via arrays of indices is not currently supported in this specification due to concerns regarding __setitem__ and array mutation semantics.

Parameters:
  • x (Union[int, Array, NativeArray]) – input array

  • indices (Union[int, Array, NativeArray]) – array indices. Must have an integer data type.

  • axis (Optional[int], default: None) – axis over which to select values. If axis is negative, the function must determine the axis along which to select values by counting from the last dimension. By default, the flattened input array is used.

  • mode (str, default: 'fill') – specifies how out-of-bounds indices will behave. - ‘raise’ – raise an error - ‘wrap’ – wrap around - ‘clip’ – clip to the range (all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.) - ‘fill’ (default) = returns invalid values (e.g. NaN) for out-of bounds indices (see also fill_value below)

  • fill_value (Optional[Number], default: None) – fill value to return for out-of-bounds slices (Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.)

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret

    an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by axis whose size must equal the number of elements in indices.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/)

  • API_specification/generated/array_api.max.html>`_

  • in the standard.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([4,5,6])
>>> indices = ivy.array([2,1,0])
>>> y = ivy.take(x, indices)
>>> print(y)
ivy.array([6, 5, 4])
>>> x = ivy.array([4.7,5.2,6.5])
>>> indices = ivy.array([[0,1]])
>>> y = ivy.zeros_like(indices, dtype=x.dtype)
>>> ivy.take(x, indices, out=y)
>>> print(y)
ivy.array([[4.7, 5.2]])
>>> x = ivy.array([False, False, True])
>>> indices = ivy.array([[4,3,2]])
>>> y = ivy.zeros_like(indices, dtype=x.dtype)
>>> ivy.take(x, indices, out=y, mode="wrap")
>>> print(y)
ivy.array([[False, False, True]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([True,False,False]),
...                     b=ivy.array([2.3,4.5,6.7]),
...                     c=ivy.array([1,2,3]))
>>> indices = ivy.array([[1,9,2]])
>>> y = ivy.take(x, indices)
>>> print(y)
{
    a: ivy.array([[False, True, False]]),
    b: ivy.array([[4.5, nan, 6.69999981]]),
    c: ivy.array([[2, -2147483648, 3]])
}
Array.take(self, indices, /, *, axis=None, mode='fill', fill_value=None, out=None)[source]#

ivy.Array instance method variant of ivy.take.

This method simply wraps the function, and so the docstring for ivy.take also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array

  • indices (Union[int, Array, NativeArray]) – array indices. Must have an integer data type.

  • axis (Optional[int], default: None) – axis over which to select values. If axis is negative, the function must determine the axis along which to select values by counting from the last dimension. By default, the flattened input array is used.

  • mode (str, default: 'fill') – specifies how out-of-bounds indices will behave. - ‘raise’ – raise an error - ‘wrap’ – wrap around - ‘clip’ – clip to the range (all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.) - ‘fill’ (default) = returns invalid values (e.g. NaN) for out-of bounds indices (see also fill_value below)

  • fill_value (Optional[Number], default: None) – fill value to return for out-of-bounds slices (Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.)

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by axis whose size must equal the number of elements in indices.

Examples

With ivy.Array input:

>>> x = ivy.array([4,5,6])
>>> indices = ivy.array([2,1,0])
>>> y = x.take(indices)
>>> print(y)
ivy.array([6, 5, 4])
>>> x = ivy.array([4.7,5.2,6.5])
>>> indices = ivy.array([[0,1]])
>>> y = ivy.zeros_like(indices, dtype=x.dtype)
>>> x.take(indices, out=y)
>>> print(y)
ivy.array([[4.7, 5.2]])
>>> x = ivy.array([False, False, True])
>>> indices = ivy.array([[4,3,2]])
>>> y = ivy.zeros_like(indices, dtype=x.dtype)
>>> x.take(indices, out=y, mode="wrap")
>>> print(y)
ivy.array([[False, False, True]])
Container.take(self, indices, /, *, axis=None, mode='fill', fill_value=None, out=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.take.

This method simply wraps the function, and so the docstring for ivy.take also applies to this method with minimal changes.

Parameters:
  • self (Union[int, Array, NativeArray, Container]) – input array

  • indices (Union[int, Array, NativeArray, Container]) – array indices. Must have an integer data type.

  • axis (Optional[Union[int, Container]], default: None) – axis over which to select values. If axis is negative, the function must determine the axis along which to select values by counting from the last dimension. By default, the flattened input array is used.

  • mode (Union[str, Container], default: 'fill') – specifies how out-of-bounds indices will behave. - ‘raise’ – raise an error - ‘wrap’ – wrap around - ‘clip’ – clip to the range (all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.) - ‘fill’ (default) = returns invalid values (e.g. NaN) for out-of bounds indices (see also fill_value below)

  • fill_value (Optional[Union[Number, Container]], default: None) – fill value to return for out-of-bounds slices (Defaults to NaN for inexact types, the largest negative value for signed types, the largest positive value for unsigned types, and True for booleans.)

  • out (Optional[Union[Array, Container]], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains 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.

Return type:

Container

Returns:

ret – an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by axis whose size must equal the number of elements in indices.

Examples

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([True,False,False]),
...                     b=ivy.array([2.3,4.5,6.7]),
...                     c=ivy.array([1,2,3]))
>>> indices = ivy.array([[1,9,2]])
>>> y = x.take(indices)
>>> print(y)
{
    a: ivy.array([[False, True, False]]),
    b: ivy.array([[4.5, nan, 6.69999981]]),
    c: ivy.array([[2, -2147483648, 3]])
}