0
0
NumPydata~20 mins

Matrix transpose operations in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Matrix Transpose Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[1 4 2]
 [3 5 6]]
B
[[1 2 3]
 [4 5 6]]
C
[[1 4]
 [2 5]
 [3 6]]
D
[[1 2]
 [3 4]
 [5 6]]
Attempts:
2 left
💡 Hint
Remember that transpose flips rows and columns.
data_output
intermediate
2: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)
A(3, 2, 4)
B(2, 3, 4)
C(4, 3, 2)
D(3, 4, 2)
Attempts:
2 left
💡 Hint
Transpose rearranges axes in the order you specify.
🔧 Debug
advanced
2: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)
ANo error, prints transposed array
BIndexError: axis 2 is out of bounds for array of dimension 2
CTypeError: transpose() takes no arguments
DValueError: axes don't match array
Attempts:
2 left
💡 Hint
Check the number of dimensions in the array and the axes used.
🚀 Application
advanced
2: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?
AAll of the above
Bnp.transpose(data, (1, 0))
Cnp.swapaxes(data, 0, 1)
Ddata.T
Attempts:
2 left
💡 Hint
All these methods can swap axes 0 and 1 for 2D arrays.
🧠 Conceptual
expert
2:00remaining
Effect of Transpose on Memory Layout
Which statement best describes the effect of transposing a NumPy array on its memory layout?
ATranspose always creates a new copy of the array with contiguous memory.
BTranspose returns a view with changed strides, no data is copied.
CTranspose modifies the original array in-place to swap rows and columns.
DTranspose converts the array to a list of lists.
Attempts:
2 left
💡 Hint
Think about whether transpose copies data or just changes how data is accessed.