NumPy flipud reverses the first axis without reversing entries inside each row

An up and down reversal is easiest to understand by naming the axis that changes. NumPy flipud reverses axis zero and returns a view of an input array. Academia Master Trader presents this English lesson for readers in Jordan, using constructed data to distinguish row reversal from column reversal and to explain what shared storage means after the operation.

Whole rows move to new positions

After import numpy as np, define m = np.array([[3,6],[9,12],[15,18]]). The call u = np.flipud(m) produces [[15,18],[9,12],[3,6]], with shape (3,2).

The last row becomes the first row, and the first row becomes the last. The middle row remains in the middle. Inside every row, the entries retain their left to right order. Fifteen still precedes eighteen. This is a positional reversal, not sorting, transposition or a change in the values themselves.

NumPy first axis reversal

The first axis is numbered zero

NumPy axis numbers begin at zero. For a two dimensional matrix, reversing axis zero changes the row positions. The expression m[::-1,...] gives the same reversal as flipud, and np.flip(m,axis=0) names the chosen axis explicitly.

The negative step walks the first axis in reverse. The ellipsis retains the other axes. No axis is removed, and the number of positions along each axis stays the same. Record the unchanged shape beside the result rather than assuming that reversing rows also exchanges dimensions.

A one dimensional array is a valid input

Define v = np.array([4,8,12,16]). The call np.flipud(v) returns [16,12,8,4], still with shape (4,). A vector has a first axis even though it has no separate row and column axes.

Flipud requires at least one dimension. A scalar array such as np.array(7) has zero dimensions, so applying flipud to that scalar raises ValueError. The requirement concerns the existence of axis zero, not the total number of numeric entries in the input.

A higher dimensional array reverses whole leading blocks

Define b = np.arange(1,9).reshape(2,2,2). Its leading blocks are [[1,2],[3,4]] and [[5,6],[7,8]]. Calling np.flipud(b) exchanges those blocks, placing the block beginning with five first.

Each block keeps its internal arrangement. Neither the second nor the third axis is reversed. The name up and down can become ambiguous for such data, while the phrase reverse axis zero still specifies the operation precisely. Choose the axis from the data layout, not from how a printed array happens to look.

A second reversal along axis zero restores the previous positional order. For an unchanged array, np.flipud(np.flipud(m)) contains the same entries in the same positions as m. This describes the ordering rule and does not make a separate copy of the underlying data.

A reversed view is not independent storage

In the original matrix example, assigning u[0,0] = 99 also changes m[2,0] to ninety nine. Both arrays expose shared data through different index arrangements. Use np.flipud(m).copy() when later assignments must affect a separate array.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Describe a reversal with its axis number, output shape and storage relationship so that the next calculation has a clear input.