to_numpy#

ivy.to_numpy(x, /, *, copy=True)[source]#

Convert an array into a numpy array.

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

  • copy (bool, default: True) – whether to copy the array to a new address or not. Default is True.

Return type:

ndarray

Returns:

ret – a numpy array copying all the element of the array x.

Examples

With ivy.Array inputs:

>>> x = ivy.array([-1, 0, 1])
>>> y = ivy.to_numpy(x, copy=True)
>>> print(y)
[-1  0  1]
>>> x = ivy.array([[-1, 0, 1],[-1, 0, 1], [1,0,-1]])
>>> y = ivy.to_numpy(x, copy=True)
>>> print(y)
[[-1  0  1]
[-1  0  1]
[ 1  0 -1]]

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]))
>>> y = ivy.to_numpy(x)
>>> print(y)
{
    a: array([-1, 0, 1], dtype=int32)
}
>>> x = ivy.Container(a=ivy.array([[-1.0, 0., 1.], [-1, 0, 1], [1, 0, -1]]),
...                   b=ivy.array([[-1, 0, 0], [1, 0, 1], [1, 1, 1]]))
>>> y = ivy.to_numpy(x)
>>> print(y)
{
    a: array([[-1., 0., 1.],
              [-1., 0., 1.],
              [1., 0., -1.]], dtype=float32),
    b: array([[-1, 0, 0],
              [1, 0, 1],
              [1, 1, 1]], dtype=int32)
}
Array.to_numpy(self, /, *, copy=True)[source]#

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

Parameters:
  • self (Array) – input array.

  • copy (bool, default: True) – whether to copy the array to a new address or not. Default is True.

Return type:

ndarray

Returns:

ret – a numpy array copying all the element of the array self.

Examples

With ivy.Array inputs:

>>> x = ivy.array([-1, 0, 1])
>>> y = x.to_numpy()
>>> print(y)
[-1  0  1]
>>> x = ivy.array([[-1, 0, 1],[-1, 0, 1], [1,0,-1]])
>>> y = x.to_numpy()
>>> print(y)
[[-1  0  1]
[-1  0  1]
[ 1  0 -1]]
Container.to_numpy(self, /, *, copy=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

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

Parameters:
  • self (Container) – input container.

  • copy (Union[bool, Container], default: True) – Whether to copy the input. Default is True.

  • 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 – a container of numpy arrays copying all the element of the container self.

Examples

With one ivy.Container instances:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]), b=ivy.array([1, 0, 1, 1]))
>>> y = x.to_numpy()
>>> print(y)
{
    a: array([-1, 0, 1], dtype=int32),
    b: array([1, 0, 1, 1], dtype=int32)
}
>>> x = ivy.Container(a=ivy.native_array([[-1, 0, 1], [-1, 0, 1], [1, 0, -1]]),
...                   b=ivy.native_array([[-1, 0, 0], [1, 0, 1], [1, 1, 1]]))
>>> y = x.to_numpy()
>>> print(y)
{
    a: array([[-1, 0, 1],
            [-1, 0, 1],
            [1, 0, -1]], dtype=int32),
    b: array([[-1, 0, 0],
            [1, 0, 1],
            [1, 1, 1]], dtype=int32)
}