Is there any other function in NumPy to transpose a 2D array?

Whenever I need to transpose a NumPy array or matrix, I use the following approach, as demonstrated in the code snippet.

I’m curious to know if there are alternative methods to do the same task efficiently in the NumPy library. Could someone provide additional approaches?

1 Like

Hello @safiaa.02, an alternative method for your task would be using the T attribute provided by NumPy to transpose any 2D array. It is quite simple to use and it is also efficient, here is an example code that uses this attribute:

Yes, you can use the NumPy np.transpose() method more efficiently by directly passing (1, 0) as an argument, making it easier to obtain the transpose matrix.

This code transposes a NumPy 2D array by switching its rows and columns, demonstrating the use of the NumPy’s transpose() method. The resulting transposed array is then displayed.