NumPy fliplr reverses axis one while leaving the row order unchanged

A left to right reversal changes positions within an array rather than sorting its values. NumPy fliplr reverses axis one and returns a view of the input. Academia Master Trader presents this English lesson for readers in Oman, using constructed arrays to explain column order, dimensional requirements and the effect of shared data.

Read the reversal one row at a time

After import numpy as np, define m = np.array([[2,5,8],[11,14,17]]). The call f = np.fliplr(m) produces [[8,5,2],[17,14,11]], with shape (2,3).

The first row remains the first row, but its entries appear in reverse order. The second row behaves the same way. Each original column remains a column, while the column positions are reversed. This differs from exchanging rows, transposing the array or ordering the numbers by size.

NumPy axis one reversal

Axis one is the second axis

NumPy counts axes from zero. For a two dimensional matrix, axis zero identifies the row direction and axis one identifies the column direction. Fliplr operates along axis one, so the familiar left to right description fits this matrix arrangement.

The expression m[:,::-1] describes the same reversal. The first slice retains the rows in their original order, while the second slice walks the columns with a step of negative one. The explicit call np.flip(m,axis=1) is another equivalent way to name the axis.

The input must have at least two dimensions

A vector such as v = np.array([2,5,8]) has shape (3,) and no axis one. Calling fliplr on it raises ValueError. A single row matrix such as np.array([[2,5,8]]) has shape (1,3), so its second axis exists and can be reversed.

The distinction is dimensional, not a count of how many values the input contains. Three values can belong to a vector or a row matrix. Inspect shape and ndim before choosing an operation whose name assumes a two dimensional arrangement.

Higher dimensional input still reverses axis one

Define b = np.arange(1,13).reshape(2,2,3). The first block is [[1,2,3],[4,5,6]]. After np.fliplr(b), that block becomes [[4,5,6],[1,2,3]].

The last axis is not reversed: the sequences one, two, three and four, five, six each retain their internal order. Axis one is the middle axis in this example. A spatial nickname such as left to right can therefore be less clear than the explicit axis number when the data has more than two dimensions.

Reversing the same axis twice restores the original positional arrangement. For an unchanged matrix, np.fliplr(np.fliplr(m)) has the same entries in the same positions as m. This identity describes positions, not independent storage: applying a reversal twice is not a substitute for making a copy when later assignments must remain separate.

A view shares data with the original array

Using the original matrix example, assigning f[0,0] = 99 also changes m[0,2] to ninety nine. The two array objects expose shared data in different positional orders. Use np.fliplr(m).copy() when an independent array is needed.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the axis, the unchanged shape and the shared data relationship when explaining a reversal.