squeeze#

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

Remove singleton dimensions (axes) from x.

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

  • axis (Optional[Union[int, Sequence[int]]], default: None) – 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

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

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the 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 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.

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)[source]#

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.

Parameters:
  • self (Array) – input array.

  • axis (Optional[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.

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

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

Return type:

Array

Returns:

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

Examples

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

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 (Optional[Union[int, Sequence[int], Container]]) – axis (or axes) to squeeze.

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

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

  • out (Optional[Container], default: None) – optional output container, for writing the result to. It must have a shape 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(axis=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(axis=0)
>>> print(y)
{
    a: ivy.array([[10.],
                  [11.]]),
    b: ivy.array([[11.],
                  [12.]])
}