0
0
NumPydata~20 mins

np.choose() for conditional selection in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.choose() Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[10 90 50]
B[40 80 50]
C[10 80 60]
D[40 90 60]
Attempts:
2 left
💡 Hint
Remember that np.choose() selects elements from each array in choices based on the indices in index.
data_output
intermediate
2: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)
A
[[1 2]
 [3 4]] with shape (2, 2)
B
[[1 6]
 [7 4]] with shape (2, 2)
C
[[5 6]
 [7 8]] with shape (2, 2)
D
[[5 2]
 [3 8]] with shape (2, 2)
Attempts:
2 left
💡 Hint
Each element in index selects from the corresponding position in the arrays inside choices.
🔧 Debug
advanced
2: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)
AValueError: shape mismatch: objects cannot be broadcast to a single shape
BIndexError: index 2 is out of bounds for axis 0 with size 2
CTypeError: unhashable type: 'numpy.ndarray'
DValueError: invalid entry in choice array
Attempts:
2 left
💡 Hint
Check if all arrays in choices have the same shape.
🚀 Application
advanced
2: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?
A
index = np.where(scores &lt; 60, 0, np.where(scores &lt;= 80, 1, 2))
grades = np.choose(index, [0, 1, 2])
B
index = np.array([0 if s &lt; 60 else 1 if s &lt;= 80 else 2 for s in scores])
grades = np.choose(index, [np.array([0]*5), np.array([1]*5), np.array([2]*5)])
C
index = np.array([0 if s &lt; 60 else 1 if s &lt;= 80 else 2 for s in scores])
grades = np.choose(index, [0, 1, 2])
D
index = np.select([scores &lt; 60, (scores &gt;= 60) &amp; (scores &lt;= 80), scores &gt; 80], [0, 1, 2])
grades = np.choose(index, [0, 1, 2])
Attempts:
2 left
💡 Hint
Remember that np.choose() expects arrays of the same shape in choices.
🧠 Conceptual
expert
2: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?
AAn array of shape (2,2) where each element is selected from the corresponding array in choices based on index value at that position.
BA 3D array stacking all choices arrays along a new axis indexed by index array.
CA flattened 1D array combining all selected elements from choices arrays ignoring original shape.
DRaises a ValueError because index array cannot be multidimensional.
Attempts:
2 left
💡 Hint
Think about how np.choose() uses the index array to pick elements from each array in choices.