Academia Master Trader

Laboratórios de leitura de gráficos e conceitos financeiros. Projeto educacional brasileiro. Exemplos fictícios, sem recomendação de investimento.

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.

NumPy expand_dims inserts a size-one axis at a chosen position without adding new numerical entries

An array can gain a dimension without gaining more data values. NumPy expand_dims inserts a new axis at a specified position in the expanded shape. The inserted axis has length one. This changes the arrangement described by the shape, rather than repeating the existing entries.

Academia Master Trader presents this explanation in English for readers in Tuvalu. The examples concern numerical array structure only. They are not prices, trading signals or financial results. A short vector makes the difference between adding a row axis and adding a column axis easy to read.

Begin with a one-dimensional array

Let x be a NumPy array containing [1,2]. Its shape is (2,), meaning that it has one axis with two entries. The shape does not include a separate row axis and column axis.

The expression np.expand_dims(x,axis=0) gives an array with one row, [1,2], and shape (1,2). The inserted size-one dimension appears at position zero in the output shape.

Original diagram: NumPy expand_dims inserts a leading size-one axis into a vector of shape two, producing a one-row array with shape one by two and the same entries one and two.

There are still two numerical entries. The new one in the shape describes the length of the added axis. It is not an extra data value inserted before one and two.

Insert the new axis after the existing axis

For the same x, np.expand_dims(x,axis=1) produces rows [1] and [2], with shape (2,1). The original length-two dimension stays first, and the new size-one dimension appears second.

The two outputs contain the same values, but one is a single row and the other is a single column. The axis argument selects where the new dimension appears, so it changes the arrangement even when the entry count stays unchanged.

Read the output shape from beginning to end. In (1,2), the first dimension has length one. In (2,1), the second dimension has length one. Writing only that the result is two-dimensional leaves this distinction unstated.

The axis names an expanded position

The documentation defines axis as a position in the expanded axes. This is important because the position belongs to the result that includes the new axis, rather than merely naming an existing input dimension.

For the one-dimensional vector, positions zero and one can each identify a place for a new axis in a two-dimensional result. They do not select an input axis to remove or replace.

Keep the operation separate from moving an existing axis. Expand_dims adds a dimension of length one. Moving axes changes the positions of dimensions that are already present. The two operations answer different structural questions.

Shape grows while entry count stays fixed

The input shape (2,), the row shape (1,2) and the column shape (2,1) each describe two entries. Multiplying the dimension lengths gives two in each case. The extra dimension does not multiply the data into additional copies.

This is why the word expand needs a precise explanation. It is the number of dimensions that increases, not the numerical values and not necessarily the number of entries.

A small example should therefore state both the shape and the contents. The contents show that one and two are retained. The shape shows where the size-one axis has been inserted.

Several new axes can be requested

The axis argument may be a tuple. For x containing [1,2], the reference shows np.expand_dims(x,axis=(0,1)), whose result has shape (1,1,2).

Two new size-one axes appear before the original length-two dimension. The result has three dimensions but still contains only the two original numerical entries.

The reference also shows axis=(2,0), giving shape (1,2,1). The new axes occupy positions zero and two of the expanded result. The original length-two axis lies between them.

Read tuple positions in the resulting shape

A tuple names positions for the inserted axes in the expanded arrangement. It should not be described as an instruction to repeatedly insert one axis and then reinterpret every subsequent position through a separate call.

For shape (1,2,1), naming the size-one axes at positions zero and two is enough to explain the structure. The remaining position holds the input's original length-two dimension.

Write the full output shape beside a multi-axis call. Nested brackets alone can be difficult to read, especially when several dimensions have length one. A shape gives a compact account of the dimension positions.

The documented result is a view

The reference describes the result as a view of the input with the number of dimensions increased. Do not describe the different bracket layout as evidence that an independent full copy of the data was created.

If later work needs an independent snapshot, that is a separate requirement from inserting an axis. A dimension change explains the arrangement used to address entries, not a guarantee that storage has become separate.

Keep those statements distinct: the output has more dimensions, the added dimensions have length one, the original values are retained and the documented result is a view. Each statement explains a different property.

Name the new dimension and its position

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Read the input shape, identify the output positions for the new size-one axes and state the resulting shape. That keeps adding a dimension distinct from repeating values, changing their magnitude or moving an existing axis.

NumPy moveaxis places selected axes in new positions while keeping the remaining axes in their original relative order

An array's shape is an ordered description of its dimensions. NumPy moveaxis changes that order by placing selected axes at specified destinations. The axes that are not selected keep their original relative order. This makes the function useful to explain as moving dimensions within an ordered list.

Academia Master Trader presents this explanation in English for readers in Tonga. The examples concern numerical array structure only. They are not prices, trading signals or financial results. Reading the source axis and destination position separately helps distinguish movement from a simple exchange of two axes.

Start with three named dimensions

Let x be a NumPy array with shape (3,4,5). Call its axes A, B and C for the explanation, with lengths three, four and five respectively. The initial order is A, B, C.

The expression np.moveaxis(x,0,-1) gives shape (4,5,3). Axis zero, labelled A in this explanation, moves to the final position. The remaining axes B and C stay in that relative order.

Original diagram: axes A B C with lengths three four five become B C A with lengths four five three after moving source axis zero to the final destination with NumPy moveaxis.

The letters are teaching labels, not additional function arguments. They help track the dimensions through the operation. The actual call identifies the source and destination through axis positions.

A destination is a position in the output

The source argument identifies the original axis to move. The destination identifies where that axis should appear in the result. In this example, negative one names the final destination position.

The length three travels with its axis. It does not become four merely because a different axis now occupies the first position. The shape changes from (3,4,5) to (4,5,3) because the existing dimensions have been reordered.

Keep axis identifiers and dimension lengths separate. The source value zero does not mean a dimension of length zero, and the destination value negative one does not request a negative dimension length.

Move the final axis to the beginning

For the same x, np.moveaxis(x,-1,0) gives shape (5,3,4). Axis C moves from the final input position to the first output position. Axes A and B retain their relative order after it.

This result differs from moving the first axis to the end. One operation produces B, C, A; the other produces C, A, B. The same dimensions participate, but the source and destination describe different arrangements.

Read both arguments in the call. Saying that an axis moves between the beginning and the end is incomplete unless the direction of that move is also clear.

Movement is not always an exchange

For shape (3,4,5), swapping the first and final axes gives shape (5,4,3). Moving the first axis to the end instead gives (4,5,3). The middle dimension behaves differently in the two arrangements.

With moveaxis, the unselected dimensions keep their relative order. With a pairwise swap, the selected axes exchange places directly. These are different rules, even though both reorder dimensions.

Use a three-axis example when teaching this distinction. A two-axis example can make the operations look identical because there is less remaining order to observe.

Follow a single entry through its indices

An entry at input index (1,2,3) in shape (3,4,5) is identified through axes A, B and C in that order. After moving A to the final position, the same entry is identified at output index (2,3,1).

The index components move with their axes. The numerical entry stored at that location is not being multiplied or replaced. A change of coordinates within the array is separate from an arithmetic change to its contents.

This index example also shows why shape alone is not the entire explanation. The output dimensions describe the arrangement, while the index mapping connects a particular input entry to its resulting position.

Several axes can be moved together

The reference permits a sequence of source axes and a sequence of destinations. Source positions must be unique, and destination positions must also be unique. Each selected source needs a corresponding destination.

For x with shape (3,4,5), the documented example np.moveaxis(x,[0,1],[-1,-2]) gives shape (5,4,3). Axis zero moves to the final position, and axis one moves to the preceding position.

Do not explain this as a series of separate calls that continually renumber the remaining input. The source sequence identifies original axes for the requested arrangement. State the pairings clearly, then read the resulting shape.

The documented result is a view

The reference describes the returned array as a view of the input array. A new visible axis order should therefore not be described as an independent snapshot of all the numerical data.

If later work requires separate storage, that is an additional requirement. Moving axes answers an arrangement question; it does not by itself establish that later changes are isolated from the original array.

Keep the shape, index mapping and view property separate when presenting the result. They explain different aspects of the same operation and prevent a familiar-looking printed array from carrying an unsupported storage claim.

State what moves and what stays ordered

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Name each source axis, state its destination and read the remaining axes in order. That makes an axis movement clear without confusing it with a pairwise exchange, a numerical transformation or a complete reversal of every dimension.

NumPy swapaxes exchanges two array axes while leaving the other axes in their existing positions

A multidimensional array can describe the same entries through a different order of dimensions. NumPy swapaxes performs a specific version of that change: it exchanges two named axes. The function takes the input array and the two axis numbers, making the selected exchange explicit.

Academia Master Trader presents this explanation in English for readers in Samoa. The examples concern numerical arrays only. They are not prices, trading signals or financial results. Reading axis numbers separately from dimension lengths helps explain what changes and what remains in place.

Start with a matrix that has one row

Let x be a NumPy array with one row, [1,2,3], and shape (1,3). The expression np.swapaxes(x,0,1) produces three rows: [1], [2] and [3]. The output shape is (3,1).

The first axis and the second axis exchange places. The input's three-column dimension becomes the output's three-row dimension, while its single-row dimension becomes the single-column dimension.

Original diagram: NumPy swapaxes exchanges axes zero and one of a one-by-three array, turning the row one two three into three single-entry rows and changing the shape to three by one.

The values remain one, two and three. They are not summed, sorted or replaced. The operation changes how existing entries are indexed through the selected axes.

A single row is not a one-dimensional vector

The shape (1,3) contains two dimensions. The shape (3,) contains only one. Both can display the values one, two and three in a horizontal line, but they do not supply the same set of axes.

The reference's first example deliberately uses a two-dimensional input with a single row. That provides axis zero and axis one for the exchange. Do not remove the extra dimension from the explanation and still describe the same operation.

Read the shape before choosing axis numbers. An axis names a dimension that exists in the input; it is not created merely by including its number in the function call.

Axis numbers are positions, not lengths

For an input shape (2,3,4), exchanging axes zero and two gives shape (4,3,2). The middle axis remains in its original position. The lengths two and four move because the dimensions they describe have exchanged places.

The arguments zero and two identify the first and third axes. They do not request dimensions with lengths zero and two. Keep the axis identifiers separate from the numbers written in the input shape.

Writing both shapes side by side is useful. Mark the two exchanged positions and leave the middle position unmarked. That makes the scope of this operation clearer than saying that every dimension has been reversed.

A square shape can hide the exchange

The reference includes a three-dimensional array with shape (2,2,2). Exchanging axes zero and two leaves that shape unchanged, because both exchanged dimensions have length two. The entries still move to different indexed positions.

The input has two blocks: rows [0,1] and [2,3] in the first block, followed by rows [4,5] and [6,7] in the second. After np.swapaxes(x,0,2), the first block has rows [0,4] and [2,6]; the second has rows [1,5] and [3,7].

The equal dimension lengths make shape an incomplete description of the result. Read selected entries as well as the shape. An unchanged shape does not establish that the arrangement stayed unchanged.

Follow an index to understand the rule

In that three-dimensional example, the value one begins at index (0,0,1). Exchanging axes zero and two places it at (1,0,0). The middle index stays zero while the outer index positions exchange places.

This is a rule about index positions, not about reversing the values within every row. The value one remains one, and the new location follows from the selected axes.

A single traced entry can explain a large printed result. State its original index, exchange the selected index positions and identify the resulting location. That links the function call to the output without depending only on visual block layout.

Distinguish a pairwise exchange from a general permutation

Swapaxes names two axes to exchange. A general transpose can specify an entire ordering of axes. When describing a pairwise exchange, the two-axis function makes that intention direct.

For shape (2,3,4), swapping zero and one yields (3,2,4). A default transpose would instead reverse the complete axis order to (4,3,2). The two operations need not produce the same arrangement or shape.

Choose an explanation that matches the intended change. Do not call every axis operation a reversal, because a two-axis exchange can leave several other dimensions untouched.

Keep the return-value condition

The reference states that, for NumPy versions from 1.10.0 onwards, an ndarray input returns a view; otherwise a new array is created. Keep the input condition when describing storage behaviour.

A view is not automatically an independent snapshot. If later work needs separate storage, that is a different requirement from exchanging two axes. A changed printed arrangement does not itself show that a separate copy exists.

For this lesson's matrix example, name x as a NumPy array before discussing its view result. That prevents a statement about one input type from becoming a broader claim about every accepted input.

Explain the selected pair clearly

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Read the input shape, name the two axes and follow an entry into the output. This keeps a pairwise axis exchange distinct from creating a dimension, reversing entries or applying a complete default transpose.

NumPy transpose changes the order of an array's axes rather than sorting or reversing its numerical values

An array's dimensions describe how its entries are arranged. NumPy transpose changes the order of those axes. In two dimensions, it gives the familiar matrix transpose: rows become columns and columns become rows. The operation should be explained through positions and shape, rather than described as a numerical calculation.

Academia Master Trader presents this explanation in English for readers in Vanuatu. The examples concern numerical arrays only. They are not prices, trading signals or financial results. A rectangular matrix makes the change of arrangement visible, while a vector shows an important limit of the operation.

Begin with a rectangular matrix

Let a contain rows [1,2,3] and [4,5,6]. Its shape is (2,3). The expression np.transpose(a) produces rows [1,4], [2,5] and [3,6], with shape (3,2).

The first input row becomes the first output column. The second input row becomes the second output column. The value two, originally in the first row and second column, moves to the second row and first column.

Original diagram: a two-by-three matrix with rows one two three and four five six becomes a three-by-two matrix with rows one four, two five and three six after NumPy transpose.

There are six values before and after the operation. The row and column counts exchange places, but no new numerical values are created. Stating the two shapes helps separate a change of arrangement from a change of array size.

A transpose is not a quarter turn

The transposed output begins with [1,4]. A counterclockwise quarter turn of the same matrix would instead begin with [3,6]. Both can produce a three-row arrangement, but their entry positions differ.

A matching output shape therefore does not prove that two operations are equivalent. Follow a specific entry from its original row and column to its new position. That reveals the distinction more clearly than comparing only the dimensions.

Transposition is also not sorting. The output is determined by the axes, regardless of whether the numerical values happen to be increasing, decreasing or mixed. It is the positions that supply the rule.

A one-dimensional vector stays one-dimensional

For the vector [1,2,3,4], np.transpose(a) leaves the vector unchanged. Its shape remains (4,). There is only one axis, so changing the axis order does not turn it into a two-dimensional column.

The reference makes this distinction explicit. To obtain a two-dimensional column from a one-dimensional array, an additional dimension must be introduced. Transposition by itself does not supply that extra axis.

This matters when describing a vector as a row or column. A printed horizontal sequence can still be a one-dimensional array. Read its shape before assuming that transposition will exchange a row dimension with a column dimension.

The default reverses the order of axes

When axes is omitted, transpose reverses the axis order. For a two-dimensional array, (0,1) becomes (1,0). For an input shape (2,3,4,5), the default output shape is (5,4,3,2).

That rule reverses the list of dimensions, not the numerical sequence within every dimension. It should not be confused with flipping entries along an axis. An axis permutation changes which dimension occupies each position in the array description.

For a higher-dimensional input, write the original shape and its reversed order side by side. That makes the default understandable even when a full printed array would contain too many entries to read comfortably.

An explicit permutation names the input axes

The axes argument must contain a permutation of the input axes. The axis at position i in the output corresponds to input axis axes[i]. This is an output-order specification, not a list of new dimension lengths.

For an input with shape (1,2,3), np.transpose(a,(1,0,2)) gives shape (2,1,3). The output first uses input axis one, then input axis zero, then input axis two.

All input axes appear once in that specification. Do not interpret (1,0,2) as a request to create dimensions of lengths one, zero and two. The numbers identify existing axes and state their new order.

Negative axis names count from the end

The documentation allows negative indices in an axis permutation. For an input shape (3,4,5), np.transpose(a,(-1,0,-2)) gives shape (5,3,4).

Here, negative one names the final input axis and negative two names the preceding input axis. The tuple therefore chooses the third, first and second input dimensions in that output order.

The negative signs do not negate any entries and do not request negative dimension lengths. They are a convention for naming axes relative to the end of the input's axis list.

A view is returned whenever possible

The reference states that transpose returns a view whenever possible. Keep that qualification. It is more precise than saying that every possible use always creates an independent copy, or that every use has identical storage behaviour.

A displayed change in layout is not evidence of independent storage. If a later operation needs a separate snapshot of the entries, that requirement should be considered separately from the axis permutation.

When presenting an example, identify the output shape and the axis order first. Discuss storage only with the documented qualification, rather than making a broader promise from the appearance of a printed matrix.

Read positions through the shape

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State the input shape, name the output axis order and follow one entry to its new position. Those details keep transposition distinct from rotation, reversal and adding a dimension.

NumPy rot90 rotates an array by quarter turns in a plane defined by two different axes

Turning a matrix through a right angle changes the positions of its entries in a particular way. NumPy rot90 expresses that operation as a number of quarter turns within a plane defined by two axes. For a two-dimensional array with the default settings, one turn is counterclockwise.

Academia Master Trader presents this explanation in English for readers in Solomon Islands. The examples concern numerical arrays only. They are not prices, trading signals or financial results. Reading the selected plane, the turn count and the output arrangement together makes the operation clear.

Begin with a two-by-two matrix

Let m have rows [1,2] and [3,4]. The expression np.rot90(m) produces rows [2,4] and [1,3]. This is the reference's basic two-dimensional example, with the default turn count of one and axes (0,1).

Follow the value two. It begins at the upper right and ends at the upper left. Four begins at the lower right and ends at the upper right. These positions show the counterclockwise turn without relying on the function name alone.

Original diagram: the matrix with rows one two and three four rotates one quarter turn counterclockwise into rows two four and one three under the default NumPy rot90 settings.

The values remain one, two, three and four. Rotation does not calculate a new numerical value for any entry. It changes their arrangement according to the selected axes and the number of turns.

The turn count is not a degree value

The parameter k counts how many times the array is rotated by ninety degrees. The default is one. For the same m, np.rot90(m,2) returns rows [4,3] and [2,1], corresponding to two quarter turns.

Do not read k as a direct angle in degrees. A value of two means two right-angle turns, not two degrees. Naming the unit as quarter turns keeps the parameter's role separate from a general geometric angle.

A useful explanation writes the function call, states the turn count and then reads the full result. That sequence gives the reader a concrete arrangement to compare with the original matrix.

Reversing the axes reverses the direction

The reference defines the rotation direction from the first selected axis towards the second. With the default two-dimensional axes (0,1), the default turn is counterclockwise. Swapping their order to (1,0) reverses that direction.

For the same m, np.rot90(m,1,axes=(1,0)) produces rows [3,1] and [4,2]. The value three moves from the lower left to the upper left, making the clockwise arrangement visible.

The documentation also states that this reversed axis order with k equal to one is equivalent to keeping axes (0,1) and using k equal to negative one. The sign and the ordered axis pair both matter when explaining direction.

Two different axes define the plane

The input must have at least two dimensions. The axes parameter names two different axes, because those axes define the plane in which the rotation occurs. A single vector does not supply that two-axis plane on its own.

For an array with more than two dimensions, the selected pair still determines the plane. The function does not require every dimension to participate in the rotation. Specify the pair rather than describing the operation only as rotating the array.

The reference includes a three-dimensional example using axes (1,2). That selection rotates within the plane of the final two dimensions while retaining the other dimension as a separate part of the arrangement. A two-dimensional diagram should not be mistaken for a picture of every possible plane.

A rectangular matrix can change its shape

A square example hides a useful detail. Let r have rows [1,2,3] and [4,5,6], giving it shape (2,3). The expression np.rot90(r) produces rows [3,6], [2,5] and [1,4].

The output shape is (3,2). There are still six entries, but the row and column counts exchange places under this quarter turn. Preserving the number of entries is not the same as preserving the shape.

Read both dimensions before and after the operation. That makes rotation distinct from reversing the order of rows or columns, where a rectangular two-dimensional array can retain its original shape.

The documented result is a view

The reference describes the return value as a rotated view of the input. Do not call it an independent full copy merely because its printed layout differs from the original. Arrangement and storage independence are separate properties.

That distinction matters if a later step changes entries. A view should not automatically be treated as a separate snapshot. If independent storage is needed, describe that as an additional requirement rather than as something guaranteed by rotation itself.

For teaching, keep the claims precise: the operation rotates entry positions, the axes define a plane, k counts quarter turns and the documented result is a view. None of those properties implies a new measurement or new numerical observation.

Read direction and shape together

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Name the ordered axes, state the number of quarter turns and compare the input and output shapes. Following one corner entry then makes the rotation easy to distinguish from reversal or a simple exchange of row and column labels.

NumPy roll moves entries around an array axis and brings values that cross the boundary back at the other end

Moving entries by a fixed number of positions can mean several different things. NumPy roll uses a circular arrangement: entries that pass one end return at the other. It does not fill newly exposed positions with zeros or discard the entries that cross the boundary. The output keeps the input shape.

Academia Master Trader presents this explanation in English for readers in Papua New Guinea. The examples describe numerical arrays only. They are not prices, trading signals or financial results. A clear account of the shift, the axis and the boundary behaviour makes this operation easier to read.

Begin with a short circular shift

Let a be [0,1,2,3,4]. The expression np.roll(a,2) produces [3,4,0,1,2]. Each entry moves two positions in the positive direction, and the final two entries reappear at the beginning.

There are still five values. Nothing is added to fill an empty space, because the movement wraps around the boundary. The order within the wrapped group remains three followed by four, then the remaining sequence continues from zero.

Original diagram: NumPy roll moves the vector zero one two three four by positive two positions, producing three four zero one two, with the wrapped entries highlighted.

The result is not [0,0,0,1,2], which would describe a different boundary rule. It is also not [2,1,0,4,3], because roll is not a reversal. A small complete result is often clearer than the phrase move the values.

A negative shift moves the other way

For the same a, np.roll(a,-2) produces [2,3,4,0,1]. The first two entries move around to the end. Positive and negative shifts describe opposite directions along the array axis.

The sign belongs to the positional shift, not to the values. The zero and one retain their values when they reappear at the end. Rolling an array does not negate its entries or calculate a numerical difference between neighbours.

Name the shift direction together with the chosen example. Phrases such as forwards and backwards can be read differently, while the actual input and output make the intended arrangement explicit.

The default flattens before shifting

Now let x have rows [0,1,2,3,4] and [5,6,7,8,9]. Its shape is (2,5). The expression np.roll(x,1) returns rows [9,0,1,2,3] and [4,5,6,7,8].

With no axis supplied, the function first uses the flattened array for the shift and then restores the original shape. Nine wraps to the beginning of the flattened sequence, and four moves into the first position of the second row.

The restored shape does not mean that each original row was shifted independently. The movement crossed a row boundary in the flattened arrangement. Explain that default explicitly when presenting a two-dimensional input.

Select the row axis

The expression np.roll(x,1,axis=0) produces rows [5,6,7,8,9] and [0,1,2,3,4]. Here the rows move as units along the first axis, with the bottom row returning at the top.

The entries within each row keep their order. The output still has shape (2,5), but the grouping differs from the result with no axis supplied. Both outputs preserve shape, so shape alone does not identify the movement.

Because this example has only two rows, a shift of positive one and a shift of negative one along axis zero give the same row order. That equality follows from this particular two-position cycle; it is not a general rule for every array length.

Select the column axis

For the same x, np.roll(x,1,axis=1) produces rows [4,0,1,2,3] and [9,5,6,7,8]. Each row wraps independently along the column axis.

Compare this with the default result. Under the explicit column axis, four returns to the beginning of its own row and nine returns to the beginning of its own row. Under the default flattened shift, nine moves to the start of the whole arrangement.

Reading one complete row makes this distinction concrete. State the axis before describing which entry appears first, because the same shift count can produce different outputs under different axis choices.

Multiple axes can have separate shifts

The reference supports a tuple of shift counts together with a tuple of axes of the same length. Each shift corresponds to the axis in the same tuple position. The order of that pairing should be written clearly.

For the two-row x, np.roll(x,(2,1),axis=(1,0)) produces rows [8,9,5,6,7] and [3,4,0,1,2]. The column axis receives a shift of two, and the row axis receives a shift of one.

An integer shift can also be used with a tuple of axes, applying that same count to each named axis. Distinguish a shared count from separate counts, rather than assuming that every multidimensional operation has only one direction.

Explain the boundary rule first

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Identify the shift count, state whether the array is flattened or an axis is selected, and show where wrapped entries return. That keeps circular movement distinct from reversal, padding and removing values.

NumPy flip reverses the order along selected axes while preserving an array's shape

Reversing an array means changing where its entries appear, not changing the values themselves. NumPy flip reverses their order along a chosen axis or set of axes. The output retains the input shape. Reading both the axis and the arrangement is therefore more useful than describing the operation simply as turning data around.

Academia Master Trader presents this explanation in English for readers in Fiji. The numerical examples illustrate array structure only. They are not prices, trading signals or financial results. A small matrix makes the difference between reversing rows, reversing columns and reversing both easy to see.

Begin with a vector

For the vector [1,2,3,4], reversing its only axis produces [4,3,2,1]. The first entry becomes the last and the last becomes the first. There are still four entries, and their numerical values remain unchanged.

This operation is not sorting. It follows the existing order in reverse, whatever that order happens to be. A vector such as [3,1,4,2] becomes [2,4,1,3], rather than a sequence arranged from largest to smallest.

Stating that distinction matters whenever the input is not already ordered numerically. Reverse order describes positions. Ascending or descending order describes a relationship between values, which is a different requirement.

Reverse the row axis

Let x contain rows [1,2,3] and [4,5,6]. Its shape is (2,3). The expression np.flip(x,axis=0) produces rows [4,5,6] and [1,2,3]. The bottom row becomes the top row.

Within each row, the entry order is unchanged. Four still precedes five and six; one still precedes two and three. Only the order along the first axis is reversed in this example.

The output shape remains (2,3). There are still two rows and three columns. Reordering those rows does not exchange the dimensions or turn the matrix into a three-row array.

Original diagram: the two rows one two three and four five six are reversed along axis zero, producing four five six followed by one two three while keeping shape two by three.

Reverse the column axis

For the same x, np.flip(x,axis=1) produces rows [3,2,1] and [6,5,4]. The top row stays the top row, but the entries within each row appear in reverse column order.

This output also has shape (2,3). A shape check alone cannot distinguish it from the axis-zero result, because both operations preserve the dimensions. The selected axis and a reading of the entries explain what changed.

For a two-dimensional array, axis zero and axis one identify different directions of arrangement. Always tie those names to the displayed matrix rather than assuming that the word reverse identifies one unique result.

Omit the axis to reverse every axis

The default axis is None. In this case, flip reverses all axes of the input. For the two-row x, np.flip(x) produces rows [6,5,4] and [3,2,1].

Both the row order and the order within each row are reversed. The result is not merely the axis-zero output or merely the axis-one output. The default applies the reversal across both dimensions of this matrix.

For this input, explicitly selecting axis=(0,1) gives the same arrangement. A tuple names the axes to reverse. This becomes especially useful when an input has more than two dimensions and only some of them should change order.

Read negative axes from the end

The reference allows negative axis numbers, counted from the final axis backwards. In the two-dimensional example, axis=-1 refers to the column axis, so it produces the same arrangement as axis=1.

The meaning depends on the number of dimensions. The final axis of a three-dimensional array is not its second axis. Describe a negative axis as a position counted from the end, then connect it to the actual input shape.

This is a naming convention for dimensions, not a request to make values negative. Reversing [1,2,3] does not produce [-1,-2,-3]. Order changes while the values are retained.

A view is not an independent copy

The documented return value is a view of the input with the chosen axis entries reversed. The reference explains that this makes the operation constant time. Do not describe flip as always allocating a separate full copy of the numerical data.

That distinction matters if a later step changes array entries. A view should not be treated as an independent snapshot merely because it displays a different order. If independent storage is required, that is a separate requirement from reversing the arrangement.

For a short explanation, identify three properties separately: the output shape, the visible order and whether the result is a view. They answer different questions about the operation and should not be collapsed into the single word reversed.

Name the axes before reading the result

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State which axes are reversed, confirm that the shape is preserved and read a complete output row. That makes the operation clear without confusing reversal with sorting, transposition or a change in numerical sign.

NumPy tile repeats a complete array pattern along specified dimensions rather than grouping copies of each value

A repeated pattern can look simple while hiding an important question: what exactly is being repeated? NumPy tile constructs an output array by repeating the input array according to repetition counts. The repeated unit is the array pattern. This differs from repeating each individual entry beside itself.

Academia Master Trader presents this explanation in English for readers in Seychelles. The examples are small numerical arrays, not prices, trading signals or financial results. Reading the pattern, the repetition counts and the output shape together makes the operation easier to explain.

Start with one complete pattern

Let a be the vector [0,1,2]. The expression np.tile(a,2) produces [0,1,2,0,1,2]. The original three-value pattern appears twice in sequence. The zero is followed by one and two before the next copy begins.

The count two means two occurrences of the pattern in the result. It does not mean the original pattern followed by two extra copies. There are six output entries, arranged as two groups of three.

Original diagram: NumPy tile repeats the complete pattern zero, one, two twice, producing zero, one, two, zero, one, two.

Compare that output with [0,0,1,1,2,2]. Both contain six entries and each input value occurs twice, but the order differs. A count of values alone therefore cannot distinguish repeating a pattern from grouping repetitions of each entry.

A tuple describes repetition along dimensions

The expression np.tile(a,(2,2)) returns two rows. Each row is [0,1,2,0,1,2]. The output shape is (2,6): two rows and six columns.

The tuple describes two repetitions along each of two dimensions. To interpret this example, the one-dimensional input is treated as a single row before repetition. That gives a starting shape of (1,3), followed by the two-by-two repetition arrangement.

This does not mean the input values become two. The repetition counts control how often the pattern occurs along dimensions; they are not replacement values placed into the array.

Read a matrix example separately

Now let b contain rows [1,2] and [3,4]. The expression np.tile(b,2) produces rows [1,2,1,2] and [3,4,3,4]. The row count stays two and the column count becomes four.

The scalar repetition count applies to the last dimension in this example. It does not repeat the two-row matrix vertically as well. Naming the output shape (2,4) makes that clear without relying on the printed arrangement alone.

When the input has more dimensions than the repetition specification, NumPy extends the repetition specification by placing ones at its beginning. Here, the count two behaves like (1,2) for a two-dimensional input. The leading one leaves the row dimension unchanged.

Repeat the matrix vertically

For the same b, np.tile(b,(2,1)) returns rows [1,2], [3,4], [1,2] and [3,4]. The output shape is (4,2). The whole two-row pattern occurs twice along the first dimension.

Read the row order carefully. The first row is followed by the second row, and then that two-row sequence begins again. This is different from placing two copies of the first row together before two copies of the second row.

The second repetition count is one, so the column pattern is not repeated horizontally. Each row still has two entries. The pair of counts explains both the changed dimension and the unchanged dimension.

Understand the dimension rule

The reference states that the output has as many dimensions as the larger of the input dimension count and the length of the repetition specification. If the repetition specification has more dimensions, NumPy adds new leading axes to the input for the operation.

For example, the vector shape (3,) is treated as (1,3) when two-dimensional repetition is requested. With three-dimensional repetition, that same vector can be treated as (1,1,3). These leading dimensions explain why the result need not remain a vector.

Keep the leading-axis rule separate from the numerical contents. It describes the arrangement used for repetition, not an arithmetic transformation of zero, one or two. The original value order remains the pattern being tiled.

Choose the operation from the intended arrangement

Tile and repeat answer different structural questions. Tile repeats an array pattern. Repeat repeats elements along a selected axis, or from a flattened input when no axis is supplied. Compare a complete small output before choosing between them.

The NumPy reference also recommends broadcasting operations and functions rather than using tile merely for broadcasting. Repetition should express the arrangement actually needed, rather than being an automatic preliminary step for every calculation involving different shapes.

Explain the pattern before the count

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Name the input pattern, read the repetition specification and state the output shape. Those details distinguish a repeated block from repeated individual entries and make a multidimensional result understandable.

NumPy repeat repeats individual entries along a chosen axis rather than repeating an entire sequence as a block

Repeating values is a structural operation as well as a numerical one. NumPy repeat places repetitions of each entry next to that entry along a selected axis. Its result depends on both the repetition count and the axis choice. When no axis is supplied, the input is flattened and the result is one-dimensional.

Academia Master Trader presents this explanation in English for readers in Mauritius. The examples concern numerical arrays only. They are not prices, trading signals or financial results. The aim is to distinguish repeating individual values, repeating rows and reading a flattened result.

Begin with a short vector

The expression np.repeat([2,5,8],2) produces [2,2,5,5,8,8]. Each input entry appears twice in a neighbouring pair. The output preserves the order of the input groups: the twos come first, followed by the fives and then the eights.

The count two means two occurrences in the result for each input entry. It does not mean the original value followed by two additional copies. There are six output entries, not nine.

Original diagram: input values two, five and eight each become an adjacent pair after NumPy repeat with a repetition count of two.

This output is different from [2,5,8,2,5,8]. That second arrangement repeats the whole three-value pattern as a block. Repeat's basic element-wise example instead groups each value's repetitions together before moving to the next input value.

No axis means a flattened result

Consider the two-row input with rows [1,2] and [3,4]. The expression np.repeat(x,2) produces [1,1,2,2,3,3,4,4]. Because axis is omitted, the function uses the flattened input and returns a flat output array.

The result has eight entries on one axis. It is not a four-by-two matrix merely because the original input had two rows. The default axis behaviour changes the arrangement as well as increasing the number of repeated values.

Explain that flattening explicitly when showing a multidimensional input. Without it, a reader may expect the original row structure to remain visible. The axis argument determines whether that structure is handled along a particular dimension instead.

Axis zero repeats rows in a two-dimensional example

For the same input x, np.repeat(x,2,axis=0) produces rows [1,2], [1,2], [3,4] and [3,4]. The row count increases from two to four. The column count remains two.

Each original row is repeated beside itself along the row axis before the next original row is handled. The result is not the original two-row matrix followed by another complete two-row block. Reading the row order makes the distinction concrete.

The output shape is (4,2). Naming both dimensions shows which axis changed and which stayed the same. An entry count alone would show eight values without describing their two-dimensional arrangement.

Axis one repeats values across columns

The expression np.repeat(x,2,axis=1) produces rows [1,1,2,2] and [3,3,4,4]. There are still two rows, while each row now contains four columns. The shape is (2,4).

Compare that with the axis-zero result. Both outputs contain eight values, but the shapes and visible groupings differ. The same repetition count does not determine one unique arrangement unless the axis choice is also known.

A helpful explanation reads the input shape, names the chosen axis and then reads one complete output row. That sequence makes the transformation visible without relying only on a long printed array.

Repetition counts can differ

The reference allows repeats to be an integer or an array of integers. With the two-row input, np.repeat(x,[1,2],axis=0) returns rows [1,2], [3,4] and [3,4]. The first row appears once and the second appears twice.

The two counts refer to positions along the selected axis. They are not values copied into the output as new data. In this example, one and two control how often the original rows occur.

The repeat specification must fit the selected axis under the function's compatibility rules. Do not assume any list of counts can be paired with any axis. A small example should show which original row or entry each count describes.

Order and shape are part of the result

Repeating an entry does not add a new kind of value to the numerical example. It changes how often existing values occur and how they are arranged. That can matter in a later calculation, so the repetition count should be explained rather than treated as cosmetic formatting.

The documented result is an output array. Describe the operation in terms of the input, the counts and the selected axis. Do not infer a purpose such as extra observations or additional measurements merely because a value now appears more often.

Read counts with their axis

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. State whether the input is flattened or an axis is selected, identify the repetition counts and read the output order. This separates repeated entries from a repeated whole pattern and makes the resulting shape clear.