Challenge - 5 Problems
Matrix Transpose Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Transpose on 2D NumPy Array
What is the output of the following code that transposes a 2D NumPy array?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = arr.T print(result)
Attempts:
2 left
💡 Hint
Remember that transpose flips rows and columns.
✗ Incorrect
The transpose of a 2x3 matrix becomes a 3x2 matrix where rows become columns.
❓ data_output
intermediate2:00remaining
Shape After Transpose of 3D Array
Given a 3D NumPy array with shape (2, 3, 4), what is the shape after applying arr.transpose(1, 0, 2)?
NumPy
import numpy as np arr = np.zeros((2, 3, 4)) result = arr.transpose(1, 0, 2) print(result.shape)
Attempts:
2 left
💡 Hint
Transpose rearranges axes in the order you specify.
✗ Incorrect
The axes are reordered from (0,1,2) to (1,0,2), so shape changes from (2,3,4) to (3,2,4).
🔧 Debug
advanced2:00remaining
Identify the Error in Transpose Usage
What error does this code raise when trying to transpose a 2D array with an invalid axis order?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
result = arr.transpose(0, 2)
print(result)
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = arr.transpose(0, 2) print(result)
Attempts:
2 left
💡 Hint
Check the number of dimensions in the array and the axes used.
✗ Incorrect
The array has 2 dimensions (axes 0 and 1). Using axis 2 causes ValueError because axes don't match array.
🚀 Application
advanced2:00remaining
Using Transpose to Swap Rows and Columns in Data
You have a dataset as a NumPy array with shape (5, 10), where rows are samples and columns are features. You want to switch rows and columns to have features as rows and samples as columns. Which code correctly does this?
Attempts:
2 left
💡 Hint
All these methods can swap axes 0 and 1 for 2D arrays.
✗ Incorrect
For 2D arrays, .T, np.transpose with axes (1,0), and np.swapaxes(0,1) all swap rows and columns.
🧠 Conceptual
expert2:00remaining
Effect of Transpose on Memory Layout
Which statement best describes the effect of transposing a NumPy array on its memory layout?
Attempts:
2 left
💡 Hint
Think about whether transpose copies data or just changes how data is accessed.
✗ Incorrect
Transpose returns a view with modified strides, so no data is copied and memory layout changes logically.