0
0
NumPydata~20 mins

transpose() for swapping axes in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Transpose Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of 2D array transpose
What is the output of this code snippet using numpy.transpose() on a 2D array?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.transpose(arr)
print(result)
A
[[1 4]
 [2 5]
 [3 6]]
B
[[1 2 3]
 [4 5 6]]
C
[[1 2]
 [3 4]
 [5 6]]
D
[[6 5 4]
 [3 2 1]]
Attempts:
2 left
💡 Hint
Transpose swaps rows and columns.
data_output
intermediate
2:00remaining
Shape after transpose on 3D array
Given a 3D numpy array with shape (2, 3, 4), what is the shape after applying np.transpose(arr, (1, 0, 2))?
NumPy
import numpy as np
arr = np.zeros((2, 3, 4))
result = np.transpose(arr, (1, 0, 2))
print(result.shape)
A(2, 3, 4)
B(3, 2, 4)
C(4, 3, 2)
D(3, 4, 2)
Attempts:
2 left
💡 Hint
The tuple (1, 0, 2) reorders axes by their indices.
🔧 Debug
advanced
2:00remaining
Identify the error in transpose usage
What error does this code raise when trying to transpose a 2D numpy array with an invalid axes tuple?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = np.transpose(arr, (0, 2))
AValueError: axes don't match array
BTypeError: transpose() takes 1 positional argument but 2 were given
CIndexError: axis 2 is out of bounds for array of dimension 2
DNo error, returns transposed array
Attempts:
2 left
💡 Hint
Check the axes indices against array dimensions.
🧠 Conceptual
advanced
2:00remaining
Effect of transpose on array memory layout
Which statement best describes the effect of numpy.transpose() on the memory layout of an array?
ATranspose flattens the array into 1D
BTranspose always copies data to a new memory location
CTranspose modifies the original array in-place
DTranspose returns a view with changed strides, no data is copied
Attempts:
2 left
💡 Hint
Think about whether transpose copies data or not.
🚀 Application
expert
3:00remaining
Using transpose to swap axes in image data
You have a color image stored as a numpy array with shape (height, width, channels). Which np.transpose() call correctly swaps the height and width axes without changing the color channels?
Anp.transpose(image, (0, 2, 1))
Bnp.transpose(image, (2, 0, 1))
Cnp.transpose(image, (1, 0, 2))
Dnp.transpose(image, (2, 1, 0))
Attempts:
2 left
💡 Hint
Height is axis 0, width is axis 1, channels is axis 2.