Complete the code to select elements from arrays using np.choose.
import numpy as np choices = [np.array([1, 2, 3]), np.array([4, 5, 6])] index = np.array([0, 1, 0]) result = np.choose([1], choices) print(result)
The np.choose function uses the index array to select elements from the choices list of arrays.
Complete the code to create an index array for np.choose based on a condition.
import numpy as np arr = np.array([10, 20, 30, 40]) index = (arr > 25).astype([1]) print(index)
We convert the boolean condition to integers (0 or 1) to use as indices for np.choose.
Fix the error in the np.choose call to select elements correctly.
import numpy as np choices = [np.array([100, 200, 300]), np.array([400, 500, 600])] index = np.array([1, 0, 1]) result = np.choose([1], choices) print(result)
The first argument to np.choose must be the index array, not the choices list.
Fill both blanks to create a dictionary comprehension that uses np.choose to select values based on a condition.
import numpy as np values = {'a': 5, 'b': 15, 'c': 25} index = np.array([0 if v < 10 else 1 for v in values.values()]) choices = [np.array([10, 20, 30]), np.array([40, 50, 60])] result = {k: np.choose([1], choices)[i] for i, k in enumerate(values.keys()) if values[k] [2] 20} print(result)
The index array is used to select from choices. The condition < 20 filters keys with values less than 20.
Fill all three blanks to create a dictionary comprehension that uses np.choose to select values and filters keys based on a condition.
import numpy as np scores = {'x': 8, 'y': 15, 'z': 22} index = np.array([0 if s [1] 10 else 1 for s in scores.values()]) choices = [np.array([100, 200, 300]), np.array([400, 500, 600])] result = [2]: np.choose(index, choices)[i] for i, [2] in enumerate(scores.keys()) if scores[[2]] [3] 20} print(result)
The first blank uses < to check scores less than 10. The second blank is the key variable v for dictionary keys. The third blank filters scores less than 20.