Complete the code to add 5 to each element of the numpy array in-place.
import numpy as np arr = np.array([1, 2, 3, 4]) arr [1]= 5 print(arr)
The += operator adds the value to each element of the array in-place, saving memory.
Complete the code to multiply each element of the numpy array by 3 in-place.
import numpy as np arr = np.array([2, 4, 6]) arr [1]= 3 print(arr)
The *= operator multiplies each element by the given value in-place, saving memory.
Fix the error in the code to subtract 2 from each element of the numpy array in-place.
import numpy as np arr = np.array([10, 20, 30]) arr [1]= 2 print(arr)
The -= operator subtracts the value from each element in-place, which is the correct way to modify the array without creating a new one.
Fill both blanks to divide each element of the numpy array by 4 in-place and then print the array.
import numpy as np arr = np.array([8, 16, 24]) arr [1]= [2] print(arr)
The /= operator divides each element by the given value in-place. Here, dividing by 4 modifies the array without extra memory.
Fill all three blanks to create a numpy array, add 10 in-place, and then multiply by 2 in-place.
import numpy as np arr = np.array([1]) arr [2]= 10 arr [3]= 2 print(arr)
First, we create an array with values 5, 10, 15. Then we add 10 to each element in-place using +=. Finally, we multiply each element by 2 in-place using *=.