0
0
NumPydata~20 mins

Why reshaping arrays matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reshaping a 1D array to 2D
What is the output of this code when reshaping a 1D array into a 2D array with 2 rows?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
reshaped = arr.reshape(2, 2)
print(reshaped)
A
[[1 2 3]
 [4]]
B
[[1 3]
 [2 4]]
C[1 2 3 4]
D
[[1 2]
 [3 4]]
Attempts:
2 left
💡 Hint
Think about how reshape arranges elements row-wise.
data_output
intermediate
2:00remaining
Shape after reshaping a 3D array
Given a numpy array with shape (2, 3, 4), what is the shape after reshaping it to (3, 8)?
NumPy
import numpy as np
arr = np.zeros((2, 3, 4))
reshaped = arr.reshape(3, 8)
print(reshaped.shape)
A(2, 12)
B(6, 4)
C(3, 8)
D(4, 6)
Attempts:
2 left
💡 Hint
Total elements must remain the same after reshape.
🔧 Debug
advanced
2:00remaining
Identify the error in reshaping
What error does this code raise and why?
NumPy
import numpy as np
arr = np.arange(10)
reshaped = arr.reshape(3, 4)
print(reshaped)
AValueError: cannot reshape array of size 10 into shape (3,4)
BTypeError: reshape argument must be tuple
CIndexError: index out of range
DNo error, prints a 3x4 array
Attempts:
2 left
💡 Hint
Check if total elements match the new shape.
🚀 Application
advanced
2:00remaining
Why reshape is important for matrix multiplication
You have a 1D array of length 6 representing a 2x3 matrix. Which reshaping is needed before multiplying with a 3x2 matrix?
NumPy
import numpy as np
A = np.array([1, 2, 3, 4, 5, 6])
B = np.array([[1, 0], [0, 1], [1, 1]])
# What reshape should be applied to A before np.dot?
AA.reshape(2, 3)
BA.reshape(3, 2)
CA.reshape(6, 1)
DA.reshape(1, 6)
Attempts:
2 left
💡 Hint
Matrix multiplication requires matching inner dimensions.
🧠 Conceptual
expert
3:00remaining
Effect of reshaping on data interpretation
Why is reshaping arrays important when working with image data in machine learning?
AIt converts color images to grayscale.
BIt changes the data layout to match model input requirements without altering pixel values.
CIt compresses the image data to reduce file size.
DIt normalizes pixel values to a 0-1 range automatically.
Attempts:
2 left
💡 Hint
Think about how models expect input shapes.