Complete the code to select elements greater than 5 using np.where().
import numpy as np arr = np.array([3, 7, 1, 9, 4]) result = np.where(arr [1] 5) print(result)
The np.where() function returns indices where the condition is true. Here, we want indices where elements are greater than 5.
Complete the code to replace values less than 4 with 0 using np.where().
import numpy as np arr = np.array([2, 5, 3, 7, 1]) new_arr = np.where(arr [1] 4, 0, arr) print(new_arr)
The condition arr < 4 selects elements less than 4. np.where() replaces those with 0, else keeps original.
Fix the error in the code to select elements equal to 10 using np.where().
import numpy as np arr = np.array([10, 5, 10, 3, 10]) indices = np.where(arr [1] 10) print(indices)
In Python, == is used to check equality. = is assignment and causes error here.
Fill both blanks to create a new array where values greater than 3 are replaced by 1, else 0.
import numpy as np arr = np.array([2, 4, 1, 5, 3]) new_arr = np.where(arr [1] 3, [2], 0) print(new_arr)
The condition arr > 3 selects elements greater than 3. These are replaced by 1, else 0.
Fill all three blanks to create a new array where values less than 5 are replaced by 0, values equal to 5 by 5, else by 10.
import numpy as np arr = np.array([3, 5, 7, 2, 5]) new_arr = np.where(arr [1] 5, 0, np.where(arr [2] 5, [3], 10)) print(new_arr)
The first condition checks for values less than 5 to replace with 0. The nested np.where() checks for values equal to 5 to replace with 5, else 10.