split#

ivy.split(x, /, *, copy=None, num_or_size_splits=None, axis=0, with_remainder=False)[source]#

Split an array into multiple sub-arrays.

Parameters:
  • x (Union[Array, NativeArray]) – array to be divided into sub-arrays.

  • num_or_size_splits (Optional[Union[int, Sequence[int], Array, NativeArray]]) – Number of equal arrays to divide the array into along the given axis if an (default: None) integer. The size of each split element if a sequence of integers or 1-D array. Default is to divide into as many 1-dimensional arrays as the axis dimension.

  • axis (int) – The axis along which to split, default is 0. (default: 0)

  • with_remainder (bool) – If the tensor does not split evenly, then store the last remainder entry. (default: False) Default is False.

Return type:

List[Array]

Returns:

  • ret – A list of sub-arrays.

  • 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([1, 2, 3])
>>> y = ivy.split(x)
>>> print(y)
[ivy.array([1]),ivy.array([2]),ivy.array([3])]
>>> x = ivy.array([[3, 2, 1], [4, 5, 6]])
>>> y = ivy.split(x, num_or_size_splits=2, axis=1, with_remainder=True)
>>> print(y)
[ivy.array([[3,2],[4,5]]),ivy.array([[1],[6]])]
>>> x = ivy.array([4, 6, 5, 3])
>>> y = x.split(num_or_size_splits=[1, 3], axis=0, with_remainder=False)
>>> print(y)
ivy.array([[4], [6, 5, 3]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([10, 45, 2]))
>>> y = ivy.split(x)
>>> print(y)
{
    a:(list[3],<classivy.array.Array>shape=[1])
}
Array.split(self, /, *, copy=None, num_or_size_splits=None, axis=0, with_remainder=False)#

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

Parameters:
  • self (Array) – array to be divided into sub-arrays.

  • num_or_size_splits (Optional[Union[int, Sequence[int], Array, NativeArray]]) – Number of equal arrays to divide the array into along the given axis if an (default: None) integer. The size of each split element if a sequence of integers or 1-D array. Default is to divide into as many 1-dimensional arrays as the axis dimension.

  • axis (int) – The axis along which to split, default is 0. (default: 0)

  • with_remainder (bool) – If the tensor does not split evenly, then store the last remainder entry. (default: False) Default is False.

Return type:

List[Array]

Returns:

A list of sub-arrays.

Examples

>>> x = ivy.array([4, 6, 5, 3])
>>> y = x.split()
>>> print(y)
[ivy.array([4]),ivy.array([6]),ivy.array([5]),ivy.array([3])]
Container.split(self, /, *, copy=None, num_or_size_splits=None, axis=0, with_remainder=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)#

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

Parameters:
  • self (Container) – array to be divided into sub-arrays.

  • num_or_size_splits (Optional[Union[int, Sequence[int], Array, NativeArray]]) – Number of equal arrays to divide the array into along the given axis if an (default: None) integer. The size of each split element if a sequence of integers or 1-D array. Default is to divide into as many 1-dimensional arrays as the axis dimension.

  • axis (int) – The axis along which to split, default is 0. (default: 0)

  • with_remainder (bool) – If the tensor does not split evenly, then store the last remainder entry. (default: False) Default is False.

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

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

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

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

Return type:

List[Container]

Returns:

list of containers of sub-arrays.

Examples

>>> x = ivy.Container(a=ivy.array([2, 1, 5, 9]), b=ivy.array([3, 7, 2, 11]))
>>> y = x.split(num_or_size_splits=2)
>>> print(y)
[{
    a: ivy.array([2, 1]),
    b: ivy.array([3, 7])
}, {
    a: ivy.array([5, 9]),
    b: ivy.array([2, 11])
}]