Challenge - 5 Problems
Transpose Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Transpose swaps rows and columns.
✗ Incorrect
The transpose of a 2x3 array becomes a 3x2 array where rows become columns and columns become rows.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
The tuple (1, 0, 2) reorders axes by their indices.
✗ Incorrect
The axes are reordered: axis 1 becomes axis 0, axis 0 becomes axis 1, axis 2 stays the same.
🔧 Debug
advanced2: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))
Attempts:
2 left
💡 Hint
Check the axes indices against array dimensions.
✗ Incorrect
The array has only 2 dimensions (axes 0 and 1). Axis 2 does not exist, causing ValueError.
🧠 Conceptual
advanced2:00remaining
Effect of transpose on array memory layout
Which statement best describes the effect of
numpy.transpose() on the memory layout of an array?Attempts:
2 left
💡 Hint
Think about whether transpose copies data or not.
✗ Incorrect
Transpose returns a view with modified strides, so no data copying happens unless explicitly requested.
🚀 Application
expert3: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?Attempts:
2 left
💡 Hint
Height is axis 0, width is axis 1, channels is axis 2.
✗ Incorrect
Swapping height and width means swapping axes 0 and 1, keeping axis 2 (channels) the same.