Challenge - 5 Problems
np.choose() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.choose() with simple conditions
What is the output of this code snippet using
np.choose() for conditional selection?NumPy
import numpy as np choices = [np.array([10, 20, 30]), np.array([40, 50, 60]), np.array([70, 80, 90])] index = np.array([0, 2, 1]) result = np.choose(index, choices) print(result)
Attempts:
2 left
💡 Hint
Remember that
np.choose() selects elements from each array in choices based on the indices in index.✗ Incorrect
The
index array has values [0, 2, 1]. For each position, np.choose() picks the element from the corresponding array in choices at that position. So, for position 0, it picks from choices[0][0] = 10; for position 1, from choices[2][1] = 80; for position 2, from choices[1][2] = 60.❓ data_output
intermediate2:00remaining
Result shape and values from np.choose() with 2D arrays
Given the following code, what is the shape and content of
result?NumPy
import numpy as np choices = [np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])] index = np.array([[1, 0], [0, 1]]) result = np.choose(index, choices) print(result) print(result.shape)
Attempts:
2 left
💡 Hint
Each element in
index selects from the corresponding position in the arrays inside choices.✗ Incorrect
For position (0,0), index=1 selects choices[1][0,0]=5; (0,1) index=0 selects choices[0][0,1]=2; (1,0) index=0 selects choices[0][1,0]=3; (1,1) index=1 selects choices[1][1,1]=8. The shape matches the input arrays (2,2).
🔧 Debug
advanced2:00remaining
Identify the error in np.choose() usage
What error does this code raise when run?
NumPy
import numpy as np choices = [np.array([1, 2, 3]), np.array([4, 5])] index = np.array([0, 1, 0]) result = np.choose(index, choices) print(result)
Attempts:
2 left
💡 Hint
Check if all arrays in
choices have the same shape.✗ Incorrect
The arrays in
choices have different shapes: [1,2,3] has length 3, but [4,5] has length 2. np.choose() requires all arrays to have the same shape to broadcast properly, so it raises a ValueError about shape mismatch.🚀 Application
advanced2:00remaining
Using np.choose() to categorize data
You have an array of scores:
scores = np.array([55, 70, 85, 40, 90]). You want to assign grades: 0 for scores < 60, 1 for scores between 60 and 80, and 2 for scores above 80. Which code correctly uses np.choose() to assign grades?Attempts:
2 left
💡 Hint
Remember that
np.choose() expects arrays of the same shape in choices.✗ Incorrect
Option B creates an index array with values 0,1,2 for each score. The choices are arrays of the same shape filled with 0,1,2 respectively.
np.choose() then selects the correct grade for each score. Other options misuse np.choose() by passing scalars or incorrect index arrays.🧠 Conceptual
expert2:00remaining
Understanding np.choose() behavior with multidimensional index
Given
choices as a list of 3 arrays each of shape (2,2), and index as a (2,2) array with values 0,1,2, what does np.choose(index, choices) return?Attempts:
2 left
💡 Hint
Think about how
np.choose() uses the index array to pick elements from each array in choices.✗ Incorrect
np.choose() uses the index array to select elements from the corresponding arrays in choices at each position. Since index and choices arrays have matching shapes, the output shape matches them, with elements chosen accordingly.