Challenge - 5 Problems
NumPy Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding NumPy array slicing output
What is the output of this code snippet?
SciPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) result = arr[1:, :2] print(result)
Attempts:
2 left
💡 Hint
Remember that arr[1:, :2] means rows from index 1 to end, columns from index 0 to 1.
✗ Incorrect
The slice arr[1:, :2] selects rows starting from index 1 (second row) to the end, and columns from index 0 up to but not including 2 (first two columns). So it returns [[4,5],[7,8]].
❓ data_output
intermediate2:00remaining
Shape and size of a reshaped NumPy array
Given the code below, what is the shape and size of the array 'reshaped'?
SciPy
import numpy as np arr = np.arange(12) reshaped = arr.reshape(3, 4) print(reshaped.shape, reshaped.size)
Attempts:
2 left
💡 Hint
The reshape changes the shape but not the total number of elements.
✗ Incorrect
The array is reshaped into 3 rows and 4 columns, so shape is (3,4). The size is total elements, which is 12.
🔧 Debug
advanced2:00remaining
Identify the error in NumPy array creation
What error does this code raise?
SciPy
import numpy as np arr = np.array([1, 2, 3], dtype='float32', shape=(2,2))
Attempts:
2 left
💡 Hint
Check the parameters accepted by np.array function.
✗ Incorrect
The np.array function does not accept a 'shape' argument. This causes a TypeError.
🧠 Conceptual
advanced2:00remaining
Broadcasting behavior in NumPy arrays
Which option correctly describes the output shape of the operation below?
SciPy
import numpy as np a = np.ones((3,1)) b = np.ones((1,4)) c = a + b print(c.shape)
Attempts:
2 left
💡 Hint
Broadcasting expands arrays to compatible shapes for element-wise operations.
✗ Incorrect
Array 'a' has shape (3,1), 'b' has shape (1,4). Broadcasting expands them to (3,4) for addition.
🚀 Application
expert3:00remaining
Find the number of unique rows in a 2D NumPy array
Given the array below, how many unique rows does it contain?
SciPy
import numpy as np arr = np.array([[1, 2], [3, 4], [1, 2], [5, 6], [3, 4]]) unique_rows = np.unique(arr, axis=0) print(len(unique_rows))
Attempts:
2 left
💡 Hint
np.unique with axis=0 finds unique rows, not elements.
✗ Incorrect
The unique rows are [1,2], [3,4], and [5,6], so total 3 unique rows.