Challenge - 5 Problems
Array Reshaping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how reshape arranges elements row-wise.
✗ Incorrect
The reshape(2, 2) changes the 1D array into 2 rows and 2 columns, filling rows first.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Total elements must remain the same after reshape.
✗ Incorrect
The original array has 2*3*4=24 elements. Reshaping to (3,8) keeps 24 elements.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check if total elements match the new shape.
✗ Incorrect
The array has 10 elements but reshape(3,4) requires 12 elements, causing ValueError.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
Matrix multiplication requires matching inner dimensions.
✗ Incorrect
A must be reshaped to 2 rows and 3 columns to multiply with B (3x2).
🧠 Conceptual
expert3:00remaining
Effect of reshaping on data interpretation
Why is reshaping arrays important when working with image data in machine learning?
Attempts:
2 left
💡 Hint
Think about how models expect input shapes.
✗ Incorrect
Reshaping adjusts the shape to fit model input layers but does not change pixel data.