NumPy squeeze removes length one dimensions without deleting numeric entries

A dimension of length one can express a useful grouping, even when it contains no variation along that axis. When an operation requires fewer dimensions, NumPy squeeze can remove selected singleton axes. Academia Master Trader presents this English lesson for readers in the Philippines, using constructed arrays to explain the default behavior, explicit axis selection and the difference between a zero dimensional array and an ordinary number.

Separate axis lengths from stored entries

After import numpy as np, define a = np.array([[[3],[6],[9]]]). Its shape is (1,3,1). There are three dimensions and three numeric entries. The first and final dimensions each have length one, while the middle dimension has length three.

The call np.squeeze(a) returns [3,6,9], with shape (3,). By default, the function removes all length one dimensions. The entries three, six and nine remain present and keep their order. Reducing the number of dimensions is not the same as deleting observations from the data.

NumPy squeeze

Select the singleton dimension to remove

The call np.squeeze(a,axis=0) gives [[3],[6],[9]], with shape (3,1). It removes the leading dimension and leaves the final length one dimension in place. The output is a column shaped matrix rather than a one dimensional vector.

The call np.squeeze(a,axis=2) gives [[3,6,9]], with shape (1,3). It removes the final dimension but keeps the leading one. These two outputs contain the same entries, yet their shapes express different two dimensional arrangements. Name the retained axes when a later calculation expects one particular structure.

A longer dimension cannot be squeezed away

The call np.squeeze(a,axis=1) raises ValueError because source axis one has length three. Squeeze does not choose one of those three entries, combine them into a total or discard that dimension by removing values. An explicitly selected axis must have length one.

This condition makes the intended structural change clear. If the goal is a summary or a selection, use an operation that expresses that goal instead. A smaller number of dimensions can arise through several different operations, and the output shape alone does not explain what happened to the entries.

Several singleton axes can be named together

The call np.squeeze(a,axis=(0,2)) returns the same one dimensional arrangement as the default call in this example. It explicitly identifies the two singleton dimensions in the source shape. The middle length three axis is retained.

An explicit selection also records which structural assumptions the calculation depends on. The function will not silently remove a selected axis that has become longer than one. Keep the original shape beside the axis selection when explaining why a particular dimension can be removed.

Removing every axis still leaves an array

Define z = np.array([[12]]), with shape (1,1). The call q = np.squeeze(z) has shape (). The result is a zero dimensional array containing twelve, not an ordinary Python scalar. It has one numeric entry and no axes.

The documentation states that the result is the input itself or a view into it. With the original a, set s = np.squeeze(a). Writing s[1] = 99 also changes a[0,1,0] to ninety nine. Use an explicit copy when later edits must stay separate. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record removed axis positions and the resulting shape without confusing dimensionality with entry count.