NumPy squeeze removes length-one axes without discarding array entries

A dimension with length one can describe a row, a column or an extra grouping direction. NumPy squeeze removes such dimensions from an array's shape. Academia Master Trader presents this English lesson for readers in Gabon, using constructed arrays to explain selective axis removal, output dimensionality and shared data storage.

A singleton dimension contains one position

After import numpy as np, define x = np.array([[[2],[4],[6]]]). The input has shape (1,3,1): its first and final axes have length one, while its middle axis has length three.

The call np.squeeze(x) returns an array displaying [2,4,6], with shape (3,). Both length-one axes have been removed from the shape. The three stored entries remain present and in the same order.

Squeeze does not remove entries whose numeric value happens to be one. Its rule concerns axis lengths, not data values. A length-three axis remains length three even if all its entries contain the number one.

NumPy squeeze shape change

Selecting an axis preserves other singleton directions

The call np.squeeze(x,axis=0) produces shape (3,1). It removes only the first axis, leaving the final singleton axis in place.

The call np.squeeze(x,axis=2) produces shape (1,3). It removes only the final axis, leaving the first singleton axis in place. These two results contain the same three entries but have different row and column interpretations.

The call np.squeeze(x,axis=(0,2)) produces shape (3,), matching the default result for this particular input. Listing axes explicitly makes the intended structural change clear.

Axis numbers refer to the input shape. Keep that shape beside the call so the selected axes can be interpreted without guessing from the printed values.

A longer axis cannot be squeezed away

The call np.squeeze(x,axis=1) raises ValueError because axis one has length three. Squeeze does not combine those three entries, choose one of them or discard them to satisfy the request.

This restriction distinguishes axis removal from aggregation or indexing. A sum could combine values, and indexing could select a position, but neither operation has the same meaning as removing a singleton direction.

When a later calculation expects a row or a column, removing every singleton axis may remove useful structure. Select only the axes whose absence matches the intended output contract.

Removing every axis leaves a zero-dimensional array

For single = np.array([[7]]), the call np.squeeze(single) returns a zero-dimensional array with shape (). It is not a length-one vector, and squeeze does not turn it into a Python scalar.

Squeeze returns the input itself or a view into it, rather than promising independent copied data. If later editing must not affect the source, make an explicit copy after squeezing.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, chosen axes and expected output shape before removing singleton dimensions.