This is a pre-release version (2.5.0.dev0+git20251130.2de293a).Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / squeeze

_ArrayFunctionDispatcher

numpy:squeeze

source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/fromnumeric.py:1596

Signature

def squeeze(a, axis = None)

Summary

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 a itself or a view into a. Note that if all axes are squeezed, the result is a 0d array and not a scalar.

Raises

: ValueError

If axis is not None, and an axis being squeezed is not of length 1

Examples

import numpy as np
x = np.array([[[0], [1], [2]]])
x.shape
np.squeeze(x).shape
np.squeeze(x, axis=0).shape
✓
(3, 1)
np.squeeze(x, axis=1).shape
✗
Traceback (most recent call last):
  File "<doctest numpy:squeeze[0]>", line 1, in <module>
    np.squeeze(x, axis=1).shape
    ~~~~~~~~~~^^^^^^^^^^^
  File "/home/runner/dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/fromnumeric.py", line 1663, in squeeze
    return squeeze(axis=axis)
ValueError: cannot select an axis to squeeze out which has size not equal to one
np.squeeze(x, axis=2).shape
x = np.array([[1234]])
x.shape
✓
(1, 1)
np.squeeze(x)
✗
array(1234)
np.squeeze(x).shape
✓
()
np.squeeze(x)[()]
✗
np.int64(1234)

See also

expand_dims

The inverse operation, adding entries of length one

reshape

Insert, remove, and combine dimensions, and resize existing ones

Aliases

  • numpy.squeeze