Quick answer: np.squeeze removes axes whose length is one and leaves all non-singleton dimensions unchanged. Use the axis argument when the output contract matters, because removing every singleton axis can produce a zero-dimensional array or an unexpected shape for downstream code.

numpy.squeeze() removes axes whose length is 1 from an array shape. It is useful when data arrives with extra dimensions from batching, image processing, model output, or file loading, and the real data is easier to work with after those singleton axes are removed. The official NumPy squeeze documentation describes the function as a shape operation; it does not change the values in the array.
The important rule is that only axes of length 1 can be squeezed. If an axis has length 2, 3, or any other value, NumPy must keep it unless you reshape the array another way. That is why squeeze is safe for cleaning singleton dimensions but not a general replacement for NumPy reshape.
Extra singleton axes often appear after reading data from APIs, adding a batch dimension for a model, or slicing arrays in a way that preserves a dimension. Removing them makes the shape easier to reason about, but it should be done deliberately so later code still receives the shape it expects.
Basic NumPy squeeze Example
Here an array starts with shape (1, 3, 1). Both the first and last axes have length 1, so np.squeeze removes them and leaves the middle axis.
import numpy as np
array = np.array([[[1], [2], [3]]])
print(array.shape)
squeezed = np.squeeze(array)
print(squeezed.shape)
print(squeezed)
The result shape is (3,). The values are still 1, 2, and 3; only the shape metadata changed. This makes downstream indexing and plotting easier because there are fewer unnecessary brackets to handle.
When debugging shape problems, print the shape before and after squeezing. That one line usually shows whether you removed the intended axes or accidentally changed the layout too much.
Use The Array Method
You can call squeeze as a NumPy function or as an array method. Both forms are common, and both remove every singleton axis when no specific axis is provided.
import numpy as np
data = np.zeros((1, 2, 1, 3))
result = data.squeeze()
print(data.shape)
print(result.shape)
This changes (1, 2, 1, 3) into (2, 3). If the original dimensions carry meaning, write a short shape check before squeezing so another reader can tell which axes are expected to disappear.
The method form is convenient inside chained array code. The function form is often clearer in tutorials or utility functions because the operation name appears before the array being transformed.
Squeeze One Axis Only
Use the axis argument when you only want to remove a known singleton axis. This keeps other singleton axes in place, which is useful when a later step expects a specific layout.
import numpy as np
data = np.arange(3).reshape(1, 3, 1)
result = np.squeeze(data, axis=0)
print(result.shape)
The result shape is (3, 1), not (3,), because only axis 0 was removed. This precision matters in machine-learning arrays where batch, channel, row, and column axes each have different meaning. For feature arrays, the related NumPy one-hot encoding guide shows why shape choices matter before model input.
Handle Invalid Axis Choices
If you ask NumPy to squeeze an axis whose length is not 1, it raises ValueError. That behavior is helpful because it catches a wrong shape assumption early.
import numpy as np
data = np.arange(6).reshape(2, 3, 1)
try:
np.squeeze(data, axis=0)
except ValueError as error:
print(error)
In this example, axis 0 has length 2, so it cannot be removed. If the goal is to rearrange or flatten data, use reshape instead of forcing squeeze into a job it was not designed to do.
Squeeze Multiple Known Axes
The axis argument can also be a tuple. Every axis in the tuple must have length 1, or NumPy will raise an error.
import numpy as np
data = np.zeros((1, 4, 1, 5))
result = np.squeeze(data, axis=(0, 2))
print(result.shape)
This gives shape (4, 5). Passing a tuple is clearer than squeezing all singleton axes when only certain positions should disappear. It also protects code from future shape changes that add another singleton axis somewhere else.
Reverse The Operation With expand_dims
The opposite operation is numpy.expand_dims, which adds an axis of length 1. Together, expand_dims and squeeze are useful when a library expects data with a batch axis but your original array does not have one.
import numpy as np
data = np.array([1, 2, 3])
batched = np.expand_dims(data, axis=0)
restored = np.squeeze(batched, axis=0)
print(batched.shape)
print(restored.shape)
Use squeeze when the extra axis is known and harmless. Avoid squeezing blindly in production pipelines if the shape carries meaning. For numerical workflows where shape and bins affect output, see our NumPy histogram guide and NumPy extrapolation guide.
The safest workflow is to treat shape as part of the data contract. Check the shape, squeeze only the singleton axes you expect, and leave a clear example in tests for the shape that should enter and leave the function.
Remove Only Singleton Axes
An axis of length two or more cannot be removed by squeeze. This makes the operation useful for cleaning batch or channel dimensions, but it is not a general reshape operation.
Specify An Axis When Needed
Calling squeeze without axis removes all singleton dimensions. Passing an axis documents which dimension is optional and raises an error if that axis is not length one, catching an input-shape change early.
Track The Result Shape
Removing all singleton dimensions can turn a shape such as (1, 3, 1) into (3,), or can produce a zero-dimensional array when every axis has length one. Check the result before indexing or broadcasting.
Compare reshape And ravel
Use reshape when you need a specific compatible shape, and ravel when you need one dimension. Use squeeze only when the rule is specifically to remove dimensions of length one.
Make Model Boundaries Explicit
API responses, image tensors, and model outputs often add batch dimensions. Normalize the shape once at the boundary and test representative singleton and non-singleton inputs before the array enters the rest of the pipeline.
The NumPy squeeze reference defines the axis rule and result shape. Related references include ravel, axes, and shape tests.
For related shape decisions, compare ravel, axes, and shape tests when removing dimensions.
Frequently Asked Questions
What does NumPy squeeze remove?
It removes axes with length one and leaves axes with any other length unchanged.
Can squeeze change an array into a scalar?
Yes, removing all singleton axes can produce a zero-dimensional array, so check the shape your downstream code expects.
How do I squeeze one axis only?
Pass the axis argument to remove a specific singleton dimension and get an error if that axis is not length one.
What is the difference between squeeze and reshape?
squeeze removes singleton dimensions according to a rule, while reshape lets you request a specific compatible shape.