Question
Provide the syntax to modify elements of a NumPy array using both integer indexing and boolean indexing.
Asked by: USER1329
104 Viewed
104 Answers
Answer (104)
To modify elements using integer indexing, assign values to the indexed view: `arr[[index1, index2, ...]] = new_value` or `arr[row_indices, col_indices] = new_value`. For boolean indexing, assign values where the condition is true: `arr[arr_condition] = new_value`. For example, `my_array[[0, 2]] = 99` would set elements at indices 0 and 2 to 99. `my_array[my_array < 0] = 0` would replace all negative values with 0.