numpy.squeeze#
- numpy.squeeze(a,axis=None)[source]#
Remove axes of length one froma.
- Parameters:
- aarray_like
Input data.
- axisNone or int or tuple of ints, optional
Selects a subset of the entries of length one in theshape. If an axis is selected with shape entry greater thanone, an error is raised.
- Returns:
- squeezedndarray
The input array, but with all or a subset of thedimensions of length 1 removed. This is alwaysa itselfor a view intoa. Note that if all axes are squeezed,the result is a 0d array and not a scalar.
- Raises:
- ValueError
Ifaxis is not None, and an axis being squeezed is not of length 1
See also
expand_dimsThe inverse operation, adding entries of length one
reshapeInsert, remove, and combine dimensions, and resize existing ones
Examples
>>>importnumpyasnp>>>x=np.array([[[0],[1],[2]]])>>>x.shape(1, 3, 1)>>>np.squeeze(x).shape(3,)>>>np.squeeze(x,axis=0).shape(3, 1)>>>np.squeeze(x,axis=1).shapeTraceback (most recent call last):...ValueError:cannot select an axis to squeeze out which has sizenot equal to one>>>np.squeeze(x,axis=2).shape(1, 3)>>>x=np.array([[1234]])>>>x.shape(1, 1)>>>np.squeeze(x)array(1234) # 0d array>>>np.squeeze(x).shape()>>>np.squeeze(x)[()]1234
On this page