MCPcopy
hub / github.com/numpy/numpy / squeeze

Function squeeze

numpy/_core/fromnumeric.py:1635–1701  ·  view source on GitHub ↗

Remove axes of length one from `a`. Parameters ---------- a : array_like Input data. axis : None or int or tuple of ints, optional Selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than on

(a, axis=None)

Source from the content-addressed store, hash-verified

1633
1634@array_function_dispatch(_squeeze_dispatcher)
1635def squeeze(a, axis=None):
1636 """
1637 Remove axes of length one from `a`.
1638
1639 Parameters
1640 ----------
1641 a : array_like
1642 Input data.
1643 axis : None or int or tuple of ints, optional
1644 Selects a subset of the entries of length one in the
1645 shape. If an axis is selected with shape entry greater than
1646 one, an error is raised.
1647
1648 Returns
1649 -------
1650 squeezed : ndarray
1651 The input array, but with all or a subset of the
1652 dimensions of length 1 removed. This is always `a` itself
1653 or a view into `a`. Note that if all axes are squeezed,
1654 the result is a 0d array and not a scalar.
1655
1656 Raises
1657 ------
1658 ValueError
1659 If `axis` is not None, and an axis being squeezed is not of length 1
1660
1661 See Also
1662 --------
1663 expand_dims : The inverse operation, adding entries of length one
1664 reshape : Insert, remove, and combine dimensions, and resize existing ones
1665
1666 Examples
1667 --------
1668 >>> import numpy as np
1669 >>> x = np.array([[[0], [1], [2]]])
1670 >>> x.shape
1671 (1, 3, 1)
1672 >>> np.squeeze(x).shape
1673 (3,)
1674 >>> np.squeeze(x, axis=0).shape
1675 (3, 1)
1676 >>> np.squeeze(x, axis=1).shape
1677 Traceback (most recent call last):
1678 ...
1679 ValueError: cannot select an axis to squeeze out which has size
1680 not equal to one
1681 >>> np.squeeze(x, axis=2).shape
1682 (1, 3)
1683 >>> x = np.array([[1234]])
1684 >>> x.shape
1685 (1, 1)
1686 >>> np.squeeze(x)
1687 array(1234) # 0d array
1688 >>> np.squeeze(x).shape
1689 ()
1690 >>> np.squeeze(x)[()]
1691 1234
1692

Callers

nothing calls this directly

Calls 1

_wrapitFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…