NumPy atleast 3d adds dimensions in documented positions rather than using one shape for every input

A minimum of three dimensions is not a complete description of an array's shape. The positions of its lengths still matter. NumPy atleast 3d supplies that minimum while using documented arrangements for one dimensional and two dimensional inputs. Reading those arrangements prevents a common shortcut: assuming that every input simply gains all its new axes at the beginning or at the end.

Academia Master Trader presents this guide in English for readers in the Maldives. The small arrays are educational examples for following shapes and return structure. They are not market prices or financial results. The goal is to keep each input's original dimensions visible while identifying the extra length one axes supplied by the function.

The function sets a lower bound

The signature is np.atleast_3d(*arys). The official reference describes its purpose as viewing inputs as arrays with at least three dimensions.

It accepts one or more array-like inputs. Non-array inputs are converted to arrays. Arrays that already have three or more dimensions are preserved.

The return condition is a.ndim>=3 for each resulting array. This states a minimum, not a command to reduce every input to exactly three axes. An input already beyond that minimum does not need to be flattened to satisfy it.

Original diagram: one dimensional shape N becomes one N one; two dimensional shape M N becomes M N one; an input already having three or more dimensions is preserved.

A scalar has a three dimensional display

The reference begins with np.atleast_3d(3.0). It displays the result as array([[[3.]]]).

The nested brackets show the added dimensional structure around the value. This does not describe three copies of the numerical value. There is still one displayed entry.

When explaining the result, keep the number of dimensions separate from the number of values. Several axes of length one can surround a single value without repeating it.

A one dimensional input gains two length one axes

The return description gives the general rule: a one dimensional array of shape (N,) becomes a view of shape (1,N,1).

The original length N occupies the middle position. A new length one appears before it and another appears after it.

The reference creates x=np.arange(3.0) and shows np.atleast_3d(x).shape as (1,3,1). This concrete example makes the placement visible. The shape is not (1,1,3) and not (3,1,1) for this call.

A two dimensional input gains a final axis

The general rule for a two dimensional array of shape (M,N) is a view of shape (M,N,1).

Here the two original lengths stay in their positions, with a new length one at the end. This is a different insertion pattern from the one dimensional case, because the input starts with a different number of axes.

The documented example uses x=np.arange(12.0).reshape(4,3). The resulting shape from np.atleast_3d(x) is (4,3,1).

Reading the before and after shapes together explains the result more clearly than describing a generic conversion into a three dimensional block.

Several inputs remain separate arrays

The function can return an array or a tuple of arrays. Each result satisfies the minimum dimensional requirement independently.

The reference passes [1,2], [[1,2]] and [[[1,2]]] in one call. The printed output shapes are (1,2,1), (1,2,1) and (1,1,2) respectively.

The first two inputs reach the same shape from different starting dimensions. The third input already has three dimensions and retains its arrangement. Having the same two values does not make all three output shapes identical.

This call does not combine the inputs into one larger array. Keep each corresponding result separate when documenting it.

Views and copies are different questions

The reference says that copies are avoided where possible and that views with three or more dimensions are returned.

For its reshaped two dimensional example, it also shows np.atleast_3d(x).base is x.base as True, noting that x itself is a reshape.

Do not replace this specific relationship with a blanket statement that a printed shape change proves an independent copy. The input form and the documented return relationship both matter.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda gives access to educational resources. Record the input shape before reading the new one. For this function, one dimensional and two dimensional inputs use different placements for the added axes, while inputs already meeting the minimum retain their dimensions.