unstack#

ivy.unstack(x, /, *, copy=None, axis=0, keepdims=False)[source]#

Unpacks the given dimension of a rank-R array into rank-(R-1) arrays.

Parameters:
  • x (Union[Array, NativeArray]) – Input array to unstack.

  • 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.

  • axis (int, default: 0) – Axis for which to unpack the array.

  • keepdims (bool, default: False) – Whether to keep dimension 1 in the unstack dimensions. Default is False.

Return type:

List[Array]

Returns:

ret – List of arrays, unpacked along specified dimensions.

Examples

With ivy.Array input:

>>> x = ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
>>> y = ivy.unstack(x, axis=0)
>>> print(y)
[ivy.array([[1, 2],
            [3, 4]]), ivy.array([[5, 6],
            [7, 8]])]
>>> x = ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
>>> y = ivy.unstack(x, axis=1, keepdims=True)
>>> print(y)
[ivy.array([[[1, 2]],
            [[5, 6]]]), ivy.array([[[3, 4]],
            [[7, 8]]])]

With ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
                        b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]))
>>> ivy.unstack(x, axis=0)
[{
    a: ivy.array([[1, 2],
                  [3, 4]]),
    b: ivy.array([[9, 10],
                  [11, 12]])
}, {
    a: ivy.array([[5, 6],
                  [7, 8]]),
    b: ivy.array([[13, 14],
                  [15, 16]])
}]
>>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
...                   b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]))
>>> ivy.unstack(x, axis=1, keepdims=True)
[{
    a: ivy.array([[[1, 2]],
                  [[5, 6]]]),
    b: ivy.array([[[9, 10]],
                  [[13, 14]]])
}, {
    a: ivy.array([[[3, 4]],
                  [[7, 8]]]),
    b: ivy.array([[[11, 12]],
                  [[15, 16]]])
}]

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.

Array.unstack(self, /, *, copy=None, axis=0, keepdims=False)[source]#

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

Parameters:
  • self (Array) – Input array to unstack.

  • 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.

  • axis (int, default: 0) – Axis for which to unpack the array.

  • keepdims (bool, default: False) – Whether to keep dimension 1 in the unstack dimensions. Default is False.

Return type:

Array

Returns:

ret – List of arrays, unpacked along specified dimensions.

Examples

>>> x = ivy.array([[1, 2], [3, 4]])
>>> y = x.unstack(axis=0)
>>> print(y)
[ivy.array([1, 2]), ivy.array([3, 4])]
>>> x = ivy.array([[1, 2], [3, 4]])
>>> y = x.unstack(axis=1, keepdims=True)
>>> print(y)
[ivy.array([[1],
        [3]]), ivy.array([[2],
        [4]])]
Container.unstack(self, /, *, copy=None, axis=0, keepdims=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

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

Parameters:
  • self (Container) – Input container to unstack at leaves.

  • 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.

  • axis (Union[int, Container], default: 0) – Axis for which to unpack the array.

  • keepdims (Union[bool, Container], default: False) – Whether to keep dimension 1 in the unstack dimensions. Default is False.

  • 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 – Containers with arrays unpacked at leaves

Examples

With one ivy.Container instances:

>>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]),
                    b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]]))
>>> x.unstack(axis=0)
[{
    a: ivy.array([[1, 2],
                 [3, 4]]),
    b: ivy.array([[9, 10],
                  [11, 12]])
}, {
    a: ivy.array([[5, 6],
                  [7, 8]]),
    b: ivy.array([[13, 14],
                  [15, 16]])
}]