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.copy (
Optional
[bool
], default:None
) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.num_or_size_splits (
Optional
[Union
[int
,Sequence
[int
],Array
,NativeArray
]], default:None
) – Number of equal arrays to divide the array into along the given axis if an 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
, default:0
) – The axis along which to split, default is0
.with_remainder (
bool
, default:False
) – If the tensor does not split evenly, then store the last remainder entry. Default isFalse
.
- 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: ivy.array([10]) }, { a: ivy.array([45]) }, { a: ivy.array([2]) } ]
- Array.split(self, /, *, copy=None, num_or_size_splits=None, axis=0, with_remainder=False)[source]#
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.copy (
Optional
[bool
], default:None
) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.num_or_size_splits (
Optional
[Union
[int
,Sequence
[int
],Array
,NativeArray
]], default:None
) – Number of equal arrays to divide the array into along the given axis if an 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
, default:0
) – The axis along which to split, default is0
.with_remainder (
bool
, default:False
) – If the tensor does not split evenly, then store the last remainder entry. Default isFalse
.
- 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)[source]#
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.copy (
Optional
[Union
[bool
,Container
]], default:None
) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default:None
.num_or_size_splits (
Optional
[Union
[int
,Sequence
[int
],Array
,NativeArray
,Container
]], default:None
) – Number of equal arrays to divide the array into along the given axis if an 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 (
Union
[int
,Container
], default:0
) – The axis along which to split, default is0
.with_remainder (
Union
[bool
,Container
], default:False
) – If the tensor does not split evenly, then store the last remainder entry. Default isFalse
.key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
],Container
]], default:None
) – The key-chains to apply or not apply the method to. Default isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.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 isFalse
.
- 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]) }]