Concept Flow - transpose() for swapping axes
Start with array
Call transpose()
Swap axes
Return new array with swapped axes
The transpose() function takes an array and swaps its axes, returning a new array with dimensions flipped.
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) transposed = arr.transpose() print(transposed)
| Step | Action | Array Shape | Array Content |
|---|---|---|---|
| 1 | Create original array | (2, 3) | [[1 2 3] [4 5 6]] |
| 2 | Call transpose() | (3, 2) | [[1 4] [2 5] [3 6]] |
| 3 | Print transposed array | (3, 2) | [[1 4] [2 5] [3 6]] |
| 4 | End | - | - |
| Variable | Start | After transpose() | Final |
|---|---|---|---|
| arr | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] | [[1 2 3] [4 5 6]] |
| transposed | N/A | [[1 4] [2 5] [3 6]] | [[1 4] [2 5] [3 6]] |
transpose() swaps the axes of an array. Syntax: new_array = array.transpose() Original array shape (m, n) becomes (n, m). Original array is unchanged. Useful for switching rows and columns.
numpy.transpose() function do to an array?transpose() function changes the order of axes in an array, effectively flipping or swapping them.arr?arr.transpose().transpose(arr) is incorrect because transpose is a method, not a standalone function here; arr.transpose misses parentheses; arr.transpose[] is invalid syntax.import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.transpose())
import numpy as np arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]) print(arr.transpose(1, 0))
arr with shape (2, 3, 4), which transpose() call will swap the first and last axes, resulting in shape (4, 3, 2)?Pass the test and this pattern moves unknown