NumPy squeeze removes size-one axes while preserving the numerical entries in an array

A dimension of length one can describe a row, a column or another position in an array's shape. NumPy squeeze removes axes whose length is one. It changes the shape without selecting fewer numerical entries. The operation is about dimensions, rather than deleting values that happen to equal one.

Academia Master Trader presents this explanation in English for readers in Kiribati. The examples concern array structure only. They are not market prices, signals or financial results. A shape containing two size-one dimensions makes the operation easy to follow.

Read the input shape before removing an axis

Consider an array x containing the nested values [[[0],[1],[2]]]. Its shape is (1,3,1). The first dimension has length one, the second has length three and the third has length one.

The call np.squeeze(x) gives shape (3,). With the default axis argument, the size-one axes are removed. The length-three dimension remains, and the entries zero, one and two are retained.

Original diagram: NumPy squeeze changes shape one by three by one into shape three by removing axes zero and two, both length one, while retaining entries zero, one and two.

The result has one dimension and three entries. The value one is still present. This matters because squeeze acts on dimension lengths, not on whether an individual entry has the numerical value one.

Remove only a selected size-one axis

The optional axis argument can limit the operation. For the same input, np.squeeze(x,axis=0) produces shape (3,1). Only the first size-one axis has been removed.

The original last axis still has length one, so the result is two-dimensional. A selected axis does not mean that every other eligible size-one axis must disappear too.

The call np.squeeze(x,axis=2) instead produces shape (1,3). The last size-one axis is removed, while the leading one remains. Both calls keep the same three numerical entries but give different shapes.

The selected axis must have length one

For shape (1,3,1), the dimension at axis one has length three. The call np.squeeze(x,axis=1) raises ValueError because this dimension cannot be removed by squeeze.

The operation does not reduce the three entries to a single number. It does not choose a representative entry, calculate an average or combine neighbouring values. A selected dimension must already have length one.

Before supplying an axis, write down its length in the input shape. This gives a direct explanation of why axes zero and two are eligible in this example, while axis one is not.

Keep axis position separate from axis length

In shape (1,3,1), axis zero and axis two each have length one. The axis number names a position. The corresponding shape entry gives the length at that position.

Saying remove axis one would therefore name the middle dimension, not mean remove any dimension whose length is one. These are different uses of the number one and should be described separately.

A clear explanation states both pieces of information: remove axis zero, whose length is one. That wording avoids confusing an index with a dimension size or an array entry.

Several selected axes can be named

The reference allows axis to be an integer or a tuple of integers, as well as the default None. A tuple selects a subset of size-one dimensions for removal.

For the example shape (1,3,1), selecting axes zero and two removes the two size-one dimensions and leaves shape (3,). Each selected dimension satisfies the same length-one requirement.

Read the selected positions from the input shape. When explaining a tuple, list the dimensions it selects and the remaining dimensions in order. This makes the resulting shape easier to understand than relying only on nested brackets.

Entry count does not shrink with dimension count

The product of the input dimension lengths is one times three times one, which equals three. The output shape (3,) also describes three entries.

Removing a dimension of length one does not discard a row full of distinct data. That dimension has only one position, so removing its place in the shape does not remove the values described by the other dimensions.

This is why shape reduction and data reduction need separate names. Squeeze simplifies dimensions that have length one. It is not an operation for summarising multiple values into fewer values.

Removing every axis leaves a zero-dimensional array

The reference gives another example: an array containing [[1234]] has shape (1,1). Calling squeeze removes both size-one dimensions and produces an array with shape ().

That result is a zero-dimensional array, not an ordinary scalar. It still contains the numerical value 1234, but its shape has no dimension lengths listed.

Do not describe the empty shape tuple as an empty array. There is still one value. The absence of axes is different from the absence of entries, and the reference explicitly distinguishes this result from a scalar.

The documented result shares the input relationship

The documentation states that the result is the input itself or a view into it. A shorter shape does not establish that a separate, independent copy of the numerical data has been created.

If later work requires an independent copy, that is a separate requirement from removing size-one dimensions. Shape and storage relationship answer different questions and should be explained separately.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Read the input shape, identify dimensions of length one and state which of them are removed. Then describe the output shape without suggesting that values equal to one were deleted.