Challenge - 5 Problems
Array Shapes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the shape of the resulting array?
Consider the following code that creates a NumPy array. What is the shape of
arr after running this code?Data Analysis Python
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the array has.
✗ Incorrect
The array has 2 rows and 3 columns, so its shape is (2, 3).
❓ data_output
intermediate2:00remaining
What is the number of dimensions of this array?
Given the code below, what is the value of
arr.ndim?Data Analysis Python
import numpy as np arr = np.array([[[1], [2]], [[3], [4]]]) print(arr.ndim)
Attempts:
2 left
💡 Hint
Count how many levels of nested lists are inside the array.
✗ Incorrect
The array has three levels of nested lists, so it is 3-dimensional.
🔧 Debug
advanced2:00remaining
Why does this reshape operation fail?
The code below tries to reshape an array but raises an error. What is the reason?
Data Analysis Python
import numpy as np arr = np.arange(10) arr_reshaped = arr.reshape(3, 4)
Attempts:
2 left
💡 Hint
Check how many elements are in the original array and the new shape.
✗ Incorrect
The original array has 10 elements, but the new shape requires 12 elements (3*4), so reshape fails.
🚀 Application
advanced2:00remaining
How to flatten a 3D array into 1D?
Given a 3D NumPy array, which code correctly converts it into a 1D array?
Data Analysis Python
import numpy as np arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
Attempts:
2 left
💡 Hint
Flatten means to make all elements in one dimension.
✗ Incorrect
The flatten() method returns a copy of the array collapsed into one dimension.
🧠 Conceptual
expert3:00remaining
What is the shape of the array after this operation?
Consider this code snippet. What is the shape of
arr_new after execution?Data Analysis Python
import numpy as np arr = np.arange(24).reshape(2, 3, 4) arr_new = arr.swapaxes(1, 2)
Attempts:
2 left
💡 Hint
swapaxes swaps two axes positions in the shape tuple.
✗ Incorrect
Original shape is (2, 3, 4). Swapping axes 1 and 2 changes shape to (2, 4, 3).