0
0
NumPydata~20 mins

reshape() for changing dimensions in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reshape 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?
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
A
[[1 2 3]
 [4 5 6]]
B
[[1 2]
 [3 4]
 [5 6]]
C
[[1 2 3 4]
 [5 6 0 0]]
D[1 2 3 4 5 6]
Attempts:
2 left
💡 Hint
Remember reshape changes the shape but keeps all elements in order.
data_output
intermediate
2:00remaining
Shape after reshaping with -1
Given this code, what is the shape of the reshaped array?
import numpy as np
arr = np.arange(12)
reshaped = arr.reshape(3, -1)
print(reshaped.shape)
NumPy
import numpy as np
arr = np.arange(12)
reshaped = arr.reshape(3, -1)
print(reshaped.shape)
A(3, 4)
B(4, 3)
C(6, 2)
D(2, 6)
Attempts:
2 left
💡 Hint
The -1 lets numpy calculate the missing dimension automatically.
🔧 Debug
advanced
2:00remaining
Error when reshaping incompatible size
What error does this code raise?
import numpy as np
arr = np.arange(10)
reshaped = arr.reshape(3, 4)
NumPy
import numpy as np
arr = np.arange(10)
reshaped = arr.reshape(3, 4)
ANo error, outputs a 3x4 array
BValueError: cannot reshape array of size 10 into shape (3,4)
CIndexError: index out of bounds
DTypeError: reshape() takes exactly 1 argument
Attempts:
2 left
💡 Hint
Check if total elements match new shape size.
🚀 Application
advanced
2:00remaining
Reshape for image data
You have a flat array of 3072 elements representing a 32x32 RGB image (3 color channels). Which reshape call correctly converts it to (32, 32, 3)?
Aarr.reshape(32, 3, 32)
Barr.reshape(3, 32, 32)
Carr.reshape(32, 32, 3)
Darr.reshape(3072, 1)
Attempts:
2 left
💡 Hint
Think about height, width, and color channels order.
🧠 Conceptual
expert
2:00remaining
Effect of reshape on data order
When reshaping a numpy array, which statement is true about the order of elements in memory?
AReshape randomly shuffles elements to fill the new shape.
BReshape changes the order of elements in memory to fit the new shape.
CReshape reverses the order of elements when changing dimensions.
DReshape keeps the order of elements the same; only the shape changes.
Attempts:
2 left
💡 Hint
Think about whether reshape rearranges data or just views it differently.