flopscope.numpy.squeeze
fnp.squeeze(a, axis=None)[flopscope source][numpy source]
Remove axes of length one from `a`.
Adapted from NumPy docs np.squeeze
Cost
0
Flopscope Context
Remove size-1 dimensions.
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 one, an error is raised.
Returns
- squeezed:ndarray
The input array, but with all or a subset of the dimensions of length 1 removed. This is always
aitself or a view intoa. Note that if all axes are squeezed, the result is a 0d array and not a scalar.
Raises
- :ValueError
If
axisis not None, and an axis being squeezed is not of length 1
See also
- we.flops.expand_dims The inverse operation, adding entries of length one
- we.flops.reshape Insert, remove, and combine dimensions, and resize existing ones
Examples
>>> import flopscope.numpy as fnp
>>> x = flops.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> flops.squeeze(x).shape
(3,)
>>> flops.squeeze(x, axis=0).shape
(3, 1)
>>> flops.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
>>> flops.squeeze(x, axis=2).shape
(1, 3)
>>> x = flops.array([[1234]])
>>> x.shape
(1, 1)
>>> flops.squeeze(x)
array(1234) # 0d array
>>> flops.squeeze(x).shape
()
>>> flops.squeeze(x)[()]
1234