NumPy roll shifts array positions cyclically and brings displaced values back at the opposite end

Moving a sequence by one position can mean several different things. A program might discard a value, leave an empty position or bring the value back at the other end. NumPy roll uses the third rule. It shifts positions cyclically, so entries that pass beyond an end reappear at the opposite end rather than disappearing.

Academia Master Trader presents this explanation in English for readers in Uganda. The examples describe numerical array organisation only. They do not represent prices, trading signals or financial results. The aim is to understand where values move, how the chosen axis affects that movement and why a cyclic shift needs a clear interpretation.

One positive shift in a vector

Consider the vector [2,5,8,11]. The expression np.roll([2,5,8,11],1) produces [11,2,5,8]. Each value moves one position towards the end. The value eleven passes beyond the last position and returns at the first. The output still contains four values and has shape (4,).

The shift is a count of positions, not an amount added to the numbers. Two becomes the second entry; it does not become three. Likewise, the eleven at the beginning is the original last entry, not a new observation or a value calculated from the other three.

Original diagram: a positive shift of one moves eleven from the last position to the first while the remaining values move one place.

A negative shift changes the direction. The expression np.roll([2,5,8,11],-1) produces [5,8,11,2]. The first value moves beyond the beginning and returns at the end. Both directions preserve the input shape and the values present in the sequence.

Count positions around the cycle

A shift equal to the length of this four-element vector brings every value back to its starting position. A shift of four therefore produces [2,5,8,11] again. A shift of five has the same visible result as a shift of one because it includes one complete turn plus one extra position.

This relationship follows from the cyclic rule. It does not mean the operation sorts, groups or simplifies the values. The original order is preserved around the cycle, but the position chosen as the beginning changes. Repeated values can make that change harder to see, so distinct example numbers are useful when explaining the mapping.

Choose the direction within a matrix

Let x have rows [1,2,3] and [4,5,6], giving it shape (2,3). The expression np.roll(x,1,axis=0) produces rows [4,5,6] and [1,2,3]. The rows shift along the first axis. With only two rows, one positive step exchanges their positions.

For np.roll(x,1,axis=1), the rows become [3,1,2] and [6,4,5]. Each row is shifted along its columns. The last entry of each row returns at the beginning of that same row. Values do not move between rows in this axis-one example.

The output shape remains (2,3) for both choices. Axis specifies the dimension in which positions change. It does not select a particular numeric value and does not add a dimension to the result. Naming the direction in words makes the intended operation easier to review.

Without an axis, flattening comes first

The default behaviour is different from choosing axis zero. When axis is omitted, roll flattens the input before shifting and then restores the original shape. For the same matrix, np.roll(x,1) produces rows [6,1,2] and [3,4,5].

The flattened order starts as [1,2,3,4,5,6]. After a shift of one it is [6,1,2,3,4,5]. Restoring the two-by-three shape explains the resulting rows. In this case, a value can cross the boundary between rows because the shift took place in the flattened sequence.

The final matrix still has shape (2,3). Shape preservation does not mean that the default operation treats each row separately. The intermediate order matters. An explicit axis is useful when the program must keep the shift within a particular dimension.

Several axes can move together

Roll also accepts tuples for shift and axis. When both are tuples, the corresponding entries describe the shift for each selected axis. A single integer shift with a tuple of axes applies that same shift to all the listed axes. The mapping between these arguments should be written clearly when more than one direction is involved.

For an introductory explanation, it is often clearer to understand one axis at a time before combining them. Follow one distinctive value from its original coordinates to its final coordinates. That position-based description avoids confusing a cyclic shift with a reversal of the entire order.

A cyclic arrangement needs a reason

Bringing the last value back to the beginning is a structural rule, not proof that the underlying observations form a natural cycle. If positions represent a particular sequence, the program should explain why wrapping the end to the start is appropriate. Roll does not determine that meaning on its own.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to educational material. Identify the shape, the direction and the number of positions, then follow the entries that cross an end. This keeps cyclic movement distinct from addition, reversal and the creation of new data.