Searching#

class ivy.data_classes.array.searching._ArrayWithSearching[source]#

Bases: ABC

_abc_impl = <_abc_data object>#
argmax(*, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]#

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

Parameters:
  • self (Array) – input array. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of (default: None) the maximum value of the flattened array. Deafult: None.

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as (default: False) dimensions with size one. With this option, the result will broadcast correctly against the array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – Optional data type of the output array. (default: None)

  • select_last_index (bool) – If this is set to True, the index corresponding to the (default: False) last occurrence of the maximum value will be returned.

  • out (Optional[Array]) – If provided, the result will be inserted into this array. It should be of (default: None) the appropriate shape and dtype.

Return type:

Union[Array, int]

Returns:

ret – if axis is None, a zero-dimensional array containing the index of the first occurrence of the maximum value; otherwise, a non-zero-dimensional array containing the indices of the maximum values. The returned array must have the default array index data type.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([0., 1., 2.])
>>> y = x.argmax()
>>> print(y)
ivy.array(2)
>>> x = ivy.array([[1., -0., -1.], [-2., 3., 2.]])
>>> y = x.argmax(axis=1)
>>> print(y)
ivy.array([0, 1])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = x.argmax(axis=1, keepdims=True)
>>> print(y)
ivy.array([[0], [2]])
>>> x = ivy.array([[4., 0., -1.], [2., -3., 6]])
>>> y = x.argmax(axis=1, dtype=ivy.int64)
>>> print(y, y.dtype)
ivy.array([0, 2]) int64
argmin(*, axis=None, keepdims=False, dtype=None, select_last_index=False, out=None)[source]#

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

Parameters:
  • self (Array) – input array. Should have a numeric data type.

  • axis (Optional[int]) – axis along which to search. If None, the function must return the index of (default: None) the minimum value of the flattened array. Default = None.

  • keepdims (bool) – if True, the reduced axes (dimensions) must be included in the result as (default: False) singleton dimensions, and, accordingly, the result must be compatible with the input array (see Broadcasting). Otherwise, if False, the reduced axes (dimensions) must not be included in the result. Default = False.

  • dtype (Optional[Union[int32, int64]]) – An optional output_dtype from: int32, int64. Defaults to int64. (default: None)

  • select_last_index (bool) – If this is set to True, the index corresponding to the (default: False) last occurrence of the minimum value will be returned.

  • out (Optional[Array]) – if axis is None, a zero-dimensional array containing the index of the first (default: None) occurrence of the minimum value; otherwise, a non-zero-dimensional array containing the indices of the minimum values. The returned array must have the default array index data type.

Return type:

Union[Array, int]

Returns:

ret – Array containing the indices of the minimum values across the specified axis.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([0., 1., -1.])
>>> y = x.argmin()
>>> print(y)
ivy.array(2)
>>> x = ivy.array([[0., 1., -1.],[-2., 1., 2.],[1., -2., 0.]])
>>> y= ivy.zeros((1,3), dtype=ivy.int64)
>>> x.argmin(axis=1, keepdims=True, out=y)
>>> print(y)
ivy.array([[2],
           [0],
           [1]])
argwhere(*, out=None)[source]#

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

Parameters:
  • self (Array) – input array for which indices are desired

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

Return type:

Array

Returns:

ret – Indices for where the boolean array is True.

Examples

Using ivy.Array instance method:

>>> x = ivy.array([[1, 2], [3, 4]])
>>> res = x.argwhere()
>>> print(res)
ivy.array([[0, 0], [0, 1], [1, 0], [1, 1]])
>>> x = ivy.array([[0, 2], [3, 4]])
>>> res = x.argwhere()
>>> print(res)
ivy.array([[0, 1], [1, 0], [1, 1]])
nonzero(*, as_tuple=True, size=None, fill_value=0)[source]#

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

Parameters:
  • self (Array) – input array. Should have a numeric data type.

  • as_tuple (bool) – if True, the output is returned as a tuple of indices, one for each (default: True) dimension of the input, containing the indices of the true elements in that dimension. If False, the coordinates are returned in a (N, ndim) array, where N is the number of true elements. Default = True.

  • size (Optional[int]) – if specified, the function will return an array of shape (size, ndim). (default: None) If the number of non-zero elements is fewer than size, the remaining elements will be filled with fill_value. Default = None.

  • fill_value (Number) – when size is specified and there are fewer than size number of elements, (default: 0) the remaining elements in the output array will be filled with fill_value. Default = 0.

Return type:

Union[Tuple[Array], Array]

Returns:

ret – Array containing the indices of the non-zero values.

where(x1, x2, /, *, out=None)[source]#

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

Parameters:
  • self (Array) – Where True, yield x1, otherwise yield x2.

  • x1 (Array) – input array. Should have a numeric data type.

  • x2 (Array) – values from which to choose when condition is False.

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

Return type:

Array

Returns:

ret – An array with elements from self where condition is True, and elements from x2 otherwise.

Examples

>>> condition = ivy.array([[True, False], [True, True]])
>>> x1 = ivy.array([[1, 2], [3, 4]])
>>> x2 = ivy.array([[5, 6], [7, 8]])
>>> res = x1.where(condition,x2)
>>> print(res)
ivy.array([[1, 6], [3, 4]])