NumPy flip reverses entry order along chosen axes while preserving array shape

Reversing an array changes positional order without sorting its values or changing its dimensions. NumPy flip performs that reversal along one axis, several selected axes or every axis by default. Academia Master Trader explains the operation in English for readers in Liechtenstein. Small constructed examples show exactly what moves, including how a reversed view relates to the original array. Position changes should be described separately from any interpretation of the numbers.

A vector reverses from end to beginning

After import numpy as np, define x = np.array([3,8,2,6]). The call np.flip(x) returns [6,2,8,3]. Six moves from the last position to the first, two moves to the second, eight moves to the third and three moves to the last. The array still has four entries.

The output is neither ascending nor descending numerical order. Flip reverses positions, regardless of the magnitudes stored there. Calling it a sorting operation would therefore describe a different rule. Trace the original indices from last to first to explain why the output begins with six rather than the smallest or largest value.

NumPy flip

Choose the axis when reversing a matrix

Define a = np.array([[1,4,7],[2,5,8]]). The call np.flip(a,axis=0) returns [[2,5,8],[1,4,7]]. It reverses the order of rows. The entries inside each row retain their horizontal order, so the row beginning with two remains two, five, eight.

The call np.flip(a,axis=1) returns [[7,4,1],[8,5,2]]. It reverses the positions within each row instead. The first row remains the first row, but its entries run in the opposite direction. Both outputs keep the original shape of two rows and three columns. The selected axis determines which positions exchange places.

The default reverses every axis

For the same matrix, np.flip(a) returns [[8,5,2],[7,4,1]]. Both the row order and the order within each row are reversed. In this two dimensional case, the result matches np.flip(a,axis=(0,1)).

This default is different from reversing only the rows. Write the axis choice explicitly in an explanation when a reader might otherwise assume a vertical reversal. A tuple names the axes to reverse together; it does not describe a sequence of new dimensions. Flip preserves the dimension lengths while changing their positional directions.

Negative axes count from the final dimension

For this matrix, np.flip(a,axis=-1) gives the same output as np.flip(a,axis=1). The negative index minus one selects the final axis. For a matrix, that is the column direction. The meaning depends on the number of dimensions in the input, rather than on a fixed physical direction for every possible array.

This notation can be useful when the final dimension has a consistent role across inputs with different numbers of dimensions. Record what that dimension represents before using a negative axis. The numerical output can be correct while a description of its rows, columns or other dimensions is incomplete.

A reversed view can share the original storage

For a NumPy array, flip returns a view rather than an independent copy of the entries. To make the consequence visible, start with a fresh b = np.array([1,2,3]) and set r = np.flip(b). Assigning r[0] = 9 changes b to [1,2,9], because the first entry of r refers to the final entry of b.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, selected axes and whether an independent copy is needed. Then trace the first and last positions along each reversed axis. This separates ordering, shape and shared storage when explaining an array transformation.