NumPy flipud reverses row order without reversing the entries inside each row

The direction of an array reversal matters. Moving a bottom row to the top is different from reading each row from right to left. Academia Master Trader explains NumPy flipud in English for readers in Greece. This lesson follows a three row matrix, records coordinate changes and compares vertical reversal with other familiar arrangements. All supplied values remain present. The example describes array structure and does not create new observations, predictions or evidence of financial performance.

Begin with a rectangular input

After import numpy as np, define a = np.array([[8,3],[2,9],[6,4]]). Its shape is (3,2): three rows and two columns. Row zero contains eight followed by three, row one contains two followed by nine and row two contains six followed by four. Writing the complete input avoids treating a vertical list of numbers as though it were a horizontal row.

The call b = np.flipud(a) returns [[6,4],[2,9],[8,3]], still with shape (3,2). Original row two becomes output row zero. Original row zero becomes output row two. The middle row stays in the middle. Each row preserves its own internal left to right order. Six still precedes four in their row, and eight still precedes three in theirs.

Track row coordinates rather than numerical size

Eight moves from input position (0,0) to output position (2,0). Three moves from (0,1) to (2,1). The column coordinate does not change. Likewise, six moves from (2,0) to (0,0) and four moves from (2,1) to (0,1). These movements follow row position rather than the size of each value.

For an input with m rows, the row mapping is i -> m-1-i. With three rows, this maps zero to two, one to one and two to zero. The middle row is fixed in this example because the number of rows is odd. With an even number of rows, there is no single central row index that maps to itself. The same mapping rule still applies.

NumPy flipud

Read the figure from complete row to complete row

The figure places the original and reversed matrices side by side. Matching row colours identify the row containing eight and three and the row containing six and four. Read each coloured row as a pair. The upper original pair appears at the bottom of the result, while the lower original pair appears at the top. The two middle entries keep their middle row.

The shape labels stay three by two. Reversal neither adds a row nor removes a column. Looking down original column zero gives eight, two, six; looking down output column zero gives six, two, eight. This is the vertical counterpart of the row mapping. The total of all entries is unchanged because every input value appears exactly once in the output.

Compare with horizontal reversal and transpose

For the same a, np.fliplr(a) returns [[3,8],[9,2],[4,6]]. It reverses the order of columns, not the order of rows. Its first row contains three and eight, while the first row of our flipud result contains six and four. Both results have the input shape, so their shapes alone do not distinguish the direction. The coordinate changes do.

The transpose a.T is different again: it returns [[8,2,6],[3,9,4]] with shape (2,3). It exchanges the roles of rows and columns. Our vertical reversal remains three by two. Name the operation before interpreting a displayed matrix as a rotation or transpose. Similar looking arrangements can arise from different rules, especially when an input has repeated values or symmetry.

Keep the view and input requirements clear

NumPy flipud accepts an input with at least one dimension. For np.array([8,3,6]), it returns [6,3,8]. That one dimensional case reverses the sequence along axis zero; it does not create a second dimension. In the matrix example, axis zero corresponds to the row order we followed.

The documented output is a view that shares data with the input. Use .copy() when you need an independent result for later edits. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Write the shape, follow the top and bottom rows and check that column coordinates stay fixed. Those steps make vertical reversal clear without confusing a change in position with a change in value.