Removing a length-one axis changes an array's shape without removing its values

A numeric array can have an axis that contains only one position. Removing that axis changes the way the array is indexed and described, but it does not discard the values stored along its other axes. The operation is often called squeezing.

This distinction matters because shape is part of the information passed between calculations. Two arrays may contain the same numbers while having different numbers of axes. A shorter shape description does not mean that fewer observations remain.

An axis length is different from a stored value

Consider an array with shape one by three by one. It has three axes and contains three values: five, eight and eleven. Removing both length-one axes leaves a one-dimensional array of length three with the same three values.

The ones in the shape describe how many positions those axes contain. They are not values that must be deleted from the data. An axis of length one can hold a value such as five, zero or any other value allowed by the array's data type.

Three values retain their element count after length-one axes are removed

The element count stays three because one times three times one equals three. After the shape becomes a single length of three, the count is still three. Squeezing is a structural operation rather than a rule for selecting observations by their magnitude.

Choosing an axis preserves a different structure

It is not always necessary to remove every length-one axis. From shape one by three by one, removing only the first axis produces shape three by one. Removing only the last axis produces shape one by three.

Those two results each contain three values and each has two axes. Nevertheless, their orientations are different. A later operation that treats the first axis as observations and the second as features will read those shapes differently.

Selecting the intended axis makes that choice explicit. An axis of length three cannot be removed by this rule, because it contains several positions. Removing it would require another operation that defines what should happen to those positions and their values.

A single position can still have a meaning

Suppose the three axes represent batch, observation and channel. A batch length of one may mean that the current calculation contains one batch. A channel length of one may mean that each observation contains one channel.

Removing either axis can be convenient for a particular calculation, but convenience does not erase its meaning. The remaining axes need labels or documentation so that the next step knows which role each position now has.

The original meaning also cannot be recovered from the values alone. Seeing five, eight and eleven does not reveal whether the original length-one axis represented a channel, a batch or something else. Shape changes and semantic labels should therefore be handled together.

Shape affects later combinations

Many array operations use shape to decide how values can be combined. A length-three vector and a three-by-one column do not always behave the same way when paired with another array, even if their listed values match.

For example, combining a three-by-one column with a one-by-three row under standard elementwise broadcasting creates a three-by-three result. Pairing two length-three vectors instead creates a length-three result. The rule is determined by the axes, not by the appearance of the printed numbers alone.

For that reason, removing an axis should be checked against the next calculation's expected shape. A successful operation is not sufficient evidence that the resulting arrangement answers the intended question.

One element can remain after every axis is removed

An array with shape one by one contains one element. Removing both axes leaves a zero-dimensional array in systems such as NumPy. It does not create an empty array, and it is not automatically the same object type as an ordinary scalar.

The number of axes and the number of elements are separate quantities. Zero axes can coexist with one stored value. Confusing those quantities can lead to an incorrect explanation of what the operation preserved.

A clear description states the original shape, the axes removed and the resulting shape. It also keeps the roles of the remaining axes visible. This makes a structural simplification readable without suggesting that observations were filtered, averaged or deleted.

Academia Master Trader shares educational material about arrays, data structure and numerical interpretation at academiamastertrader.com.br.

Aprenda