Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the np.choose() function do in NumPy?
easy
A. Sorts an array in ascending order
B. Selects elements from multiple arrays based on an index array
C. Calculates the mean of an array
D. Generates random numbers

Solution

  1. Step 1: Understand the purpose of np.choose()

    np.choose() uses an index array to pick elements from multiple arrays at corresponding positions.
  2. Step 2: Compare with other options

    Sorting, mean calculation, and random number generation are unrelated to np.choose().
  3. Final Answer:

    Selects elements from multiple arrays based on an index array -> Option B
  4. Quick Check:

    np.choose() = Select elements [OK]
Hint: Remember: np.choose picks from arrays using index array [OK]
Common Mistakes:
  • Confusing np.choose() with sorting functions
  • Thinking np.choose() calculates statistics
  • Assuming np.choose() generates random values
2. Which of the following is the correct syntax to use np.choose() with an index array idx and option arrays a, b, c?
easy
A. np.choose(idx, [a, b, c])
B. np.choose([a, b, c], idx)
C. np.choose(a, b, c, idx)
D. np.choose(idx, a, b, c)

Solution

  1. Step 1: Recall np.choose() parameter order

    The first argument is the index array, the second is a list of arrays to choose from.
  2. Step 2: Match syntax with options

    Only np.choose(idx, [a, b, c]) matches the correct order and structure.
  3. Final Answer:

    np.choose(idx, [a, b, c]) -> Option A
  4. Quick Check:

    Index first, then list of arrays [OK]
Hint: Index array first, then list of arrays in np.choose() [OK]
Common Mistakes:
  • Swapping index array and options
  • Passing options as separate arguments instead of a list
  • Using incorrect argument order
3. What is the output of the following code?
import numpy as np
idx = np.array([0, 2, 1, 0])
a = np.array([10, 20, 30, 40])
b = np.array([100, 200, 300, 400])
c = np.array([1000, 2000, 3000, 4000])
result = np.choose(idx, [a, b, c])
print(result)
medium
A. [10 2000 300 40]
B. [10 300 200 40]
C. [100 3000 2000 400]
D. [1000 300 200 40]

Solution

  1. Step 1: Understand index array and options

    Index array is [0, 2, 1, 0]. Arrays are a=[10,20,30,40], b=[100,200,300,400], c=[1000,2000,3000,4000].
  2. Step 2: Select elements using np.choose()

    For each position, pick from arrays based on index:
    pos0: idx=0 -> a[0]=10
    pos1: idx=2 -> c[1]=2000
    pos2: idx=1 -> b[2]=300
    pos3: idx=0 -> a[3]=40
    So result = [a[0], c[1], b[2], a[3]] = [10, 2000, 300, 40]
  3. Step 3: Check options

    [10 300 200 40] incorrectly uses 300 and 200 instead of 2000 and 300. [10 2000 300 40] matches our result.
  4. Final Answer:

    [10 2000 300 40] -> Option A
  5. Quick Check:

    np.choose picks arrays by index, then elements by position [OK]
Hint: Index picks array, position picks element [OK]
Common Mistakes:
  • Mixing up index meaning (array vs element)
  • Swapping elements from wrong arrays
  • Confusing np.choose with fancy indexing
4. The following code raises an error. What is the most likely cause?
import numpy as np
idx = np.array([0, 1, 2])
a = np.array([1, 2])
b = np.array([3, 4])
c = np.array([5, 6])
result = np.choose(idx, [a, b, c])
print(result)
medium
A. np.choose() requires index array to be boolean
B. Index array contains values outside the range of options
C. Index array and option arrays have incompatible shapes causing mismatch
D. np.choose() cannot handle more than two option arrays

Solution

  1. Step 1: Check index array and options

    Index array is [0,1,2], which is valid for 3 options (a,b,c).
  2. Step 2: Check shapes of option arrays

    a, b, c each have shape (2,), but index array has length 3. np.choose expects index and options to have compatible shapes.
  3. Step 3: Identify cause of error

    Index array length 3 vs option arrays length 2 causes shape mismatch error.
  4. Final Answer:

    Index array and option arrays have incompatible shapes causing mismatch -> Option C
  5. Quick Check:

    Shape mismatch causes np.choose error [OK]
Hint: Ensure index and option arrays have compatible shapes [OK]
Common Mistakes:
  • Assuming index values cause error
  • Thinking np.choose needs boolean index
  • Believing np.choose limits number of options
5. You have three arrays representing scores from three different tests:
test1 = np.array([80, 90, 70])
test2 = np.array([85, 88, 75])
test3 = np.array([78, 92, 72])
And an index array indicating the best test for each student:
best = np.array([1, 2, 0])
Using np.choose(), how do you create an array of the best scores for each student?
hard
A. np.choose(best, [test1, test3, test2])
B. np.choose(best, [test3, test1, test2])
C. np.choose(best, [test2, test3, test1])
D. np.choose(best, [test1, test2, test3])

Solution

  1. Step 1: Understand index array meaning

    Index array best = [1, 2, 0] means:
    For student 0, best test is index 1 -> test2
    For student 1, best test is index 2 -> test3
    For student 2, best test is index 0 -> test1
  2. Step 2: Match option arrays order to index

    To pick correctly, option arrays must be in order [test1, test2, test3] so index 0 picks test1, index 1 picks test2, index 2 picks test3.
  3. Step 3: Verify np.choose usage

    Using np.choose(best, [test1, test2, test3]) picks correct scores per student.
  4. Final Answer:

    np.choose(best, [test1, test2, test3]) -> Option D
  5. Quick Check:

    Order options to match index meaning [OK]
Hint: Order option arrays to match index values [OK]
Common Mistakes:
  • Assuming option arrays order matches test numbers
  • Not matching index values to option array order
  • Using wrong order causing incorrect selection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard