squeeze#

ivy.squeeze(x, /, axis, *, copy=None, out=None)[source]#

Remove singleton dimensions (axes) from x.

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

  • axis (Union[int, Sequence[int]]) – axis (or axes) to squeeze. If a specified axis has a size greater than one, a ValueError is. If None, then all squeezable axes are squeezed. Default: None

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

Return type:

Array

Returns:

ret – an output array having the same data type and elements as x.

This function conforms to the Array API Standard. This docstring is an extension of the docstring # noqa in the standard.

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.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([[[0, 1], [2, 3]]])
>>> print(ivy.squeeze(x, axis=0))
ivy.array([[0, 1], [2, 3]])
>>> x = ivy.array([[[[1, 2, 3]], [[4, 5, 6]]]])
>>> print(ivy.squeeze(x, axis=2))
ivy.array([[[1, 2, 3], [4, 5, 6]]])
>>> x = ivy.array([[[0], [1], [2]]])
>>> print(ivy.squeeze(x, axis=None))
ivy.array([0, 1, 2])
>>> print(ivy.squeeze(x, axis=0))
ivy.array([[0],
       [1],
       [2]])
>>> print(ivy.squeeze(x, axis=2))
ivy.array([[0, 1, 2]])
>>> print(ivy.squeeze(x, axis=(0, 2)))
ivy.array([0, 1, 2])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),
...                   b=ivy.array([3., 4., 5.]))
>>> y = ivy.squeeze(x, axis=None)
>>> print(y)
{
    a: ivy.array([0., 1., 2.]),
    b: ivy.array([3., 4., 5.])
}
Array.squeeze(self, /, axis, *, copy=None, out=None)#

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

Examples

>>> x = ivy.array([[[0.],[ 1.]]])
>>> y = x.squeeze(2)
>>> print(y)
ivy.array([[0., 1.]])
Return type:

Array

Container.squeeze(self, /, axis, *, copy=None, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#

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

Parameters:
  • self (Container) – input container.

  • axis (Union[int, Sequence[int]]) – axis (or axes) to squeeze.

  • 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 (default: True) will be skipped. Default is True.

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

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

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

Return type:

Container

Returns:

ret – an output container with the results.

Examples

>>> x = ivy.Container(a=ivy.array([[[10.], [11.]]]),
...                   b=ivy.array([[[11.], [12.]]]))
>>> y = x.squeeze(2)
>>> print(y)
{
    a: ivy.array([[10., 11.]]),
    b: ivy.array([[11., 12.]])
}
>>> x = ivy.Container(a=ivy.array([[[10.], [11.]]]),
...                   b=ivy.array([[[11.], [12.]]]))
>>> y = x.squeeze(0)
>>> print(y)
{
    a: ivy.array([[10.], [11.]]),
    b: ivy.array([[11.], [12.]])
}