How to permute along axis in Numpy

I will show you how to use numpy to permute the elements of an array along a given axis.

Permuting means rearranging the order of the elements in a way that preserves their shape and size. For example, if we have an array of shape (2, 3, 4), we can permute the elements along the first axis (axis=0) to get a new array of shape (2, 3, 4) with different rows.

Understanding Numpy Permutation

To permute an array along an axis, we need two things: a permutation vector and the numpy.transpose function. A permutation vector is a list or array of integers that specifies the new order of the elements along the axis. For example, if we want to swap the first and second rows of an array, we can use the permutation vector [1, 0]. The numpy.transpose function takes an array and a list of axes as arguments and returns a new array with the axes permuted according to the list. For example, if we want to swap the first and second axes of an array, we can use the list [1, 0, 2].

See also  How to calculate absolute value using Numpy

Examples of Permuting an Array

Let’s see some examples of how to use numpy to permute an array along an axis. First, let’s create a random array of shape (2, 3, 4) using numpy.random.randint:

        
import numpy as np
arr = np.random.randint(10, size=(2, 3, 4))
        
    

Now let’s permute the elements along the first axis (axis=0) using the permutation vector [1, 0]. We can use numpy.arange to create the permutation vector easily:

        
perm = np.arange(arr.shape[0])[::-1] 
arr_perm = np.transpose(arr, axes=[perm[0], perm[1], 2]) 
        
    

We can see that the rows of arr_perm are swapped compared to arr. Note that we have to use perm[0] and perm[1] as indices for the axes argument of numpy.transpose because it expects a list of integers.

See also  How to sum up array in Numpy?

We can also permute the elements along any other axis by changing the permutation vector and the axes argument accordingly.

Permuting the Second Axis

Let’s permute the elements along the second axis (axis=1) using the permutation vector [2, 0, 1]. We can use numpy.roll to create the permutation vector easily:

        
perm = np.roll(np.arange(arr.shape[1]), -1)
arr_perm = np.transpose(arr, axes=[0, perm[0], perm[1], 2])
        
    

We can see that the columns of arr_perm are shifted to the left compared to arr. Note that we have to use perm[0] and perm[1] as indices for the axes argument of numpy.transpose because it expects a list of integers.

See also  How to resolve TypeError: Cannot perform reduce with flexible type

Permuting the Third Axis

Finally, let’s permute the elements along the third axis (axis=2) using the permutation vector [3, 2, 1, 0]. We can use numpy.flip to create the permutation vector easily:

        
perm = np.flip(np.arange(arr.shape[2]))
arr_perm = np.transpose(arr, axes=[0, 1, perm[0], perm[1], perm[2], perm[3]])
        
    

We can see that the elements of arr_perm are reversed along the third axis compared to arr. Note that we have to use perm[0], perm[1], perm[2], and perm[3] as indices for the axes argument of numpy.transpose because it expects a list of integers.