- numpy.squeeze(a, axis=None)#
Remove axes of length one from a.
- axisNone or int or tuple of ints, optional
New in version 1.7.0.
Selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised.
The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a. Note that if all axes are squeezed, the result is a 0d array and not a scalar.
If axis is not None, and an axis being squeezed is not of length 1
>>> x = np.array([[, , ]]) >>> x.shape (1, 3, 1) >>> np.squeeze(x).shape (3,) >>> np.squeeze(x, axis=0).shape (3, 1) >>> np.squeeze(x, axis=1).shape Traceback (most recent call last): ... ValueError: cannot select an axis to squeeze out which has size not equal to one >>> np.squeeze(x, axis=2).shape (1, 3) >>> x = np.array([]) >>> x.shape (1, 1) >>> np.squeeze(x) array(1234) # 0d array >>> np.squeeze(x).shape () >>> np.squeeze(x)[()] 1234