educative.io

Educative

Clarity about 'Axes' Keyword

Dear Sir,

I have not understood clearly how axes command works in actual since the explained concept in the text, while checked weren’t applicable to some other cases.

I request any of the member from this community to please explain me about the axes keyword in np.transpose

Hi @Krishna_Hrithik

A 1D array has one axis, a 2D array has 2 axes, and an nD array has n axes. For instance, take a look at the following illustration:


The shape of the array reveals the number of elements along each axis.


arr = np.arange(24)

arr = np.reshape(arr, (3, 4, 2))

When we do the following:


transposed = np.transpose(arr, axes=(1, 2, 0))

The first dimension of arr becomes the third dimension of transposed, the second dimension of arr becomes the first dimension of transposed, and the third dimension of arr becomes the second dimension of transposed. Hence, the shape of transposed is (4, 2, 3).

Best Regards,
Hiba | Developer Advocate
educative.io

2 Likes

Thank You Sir !!

Thank you so much this clarified my doubt