NumPy | place method
place(~) method sets the specified values given a boolean mask.
The input array. All arrays are treated as a flattened array.
An array of booleans that indicate whether or not to set the value. The shape must be the same as that of a.
A 1D array that contains the values to set. If v is shorter than a, then v will be repeated to ensure the shape matches up.
None - the setting is done in-place.
a = np.array([5,6,7,8])np.place(a, [True, False, False, True], 9)aarray([9, 6, 7, 9])
Notice how only values that had a corresponding value of True in the mask were set.
Consider the following 2D array:
aarray([[5, 6],[7, 8]])
Setting a single value
np.place(a, [True, False, False, True], 9)aarray([[9, 6],[7, 9]])
Setting multiple values
np.place(a, [True, False, False, True], [9,10])aarray([[ 9, 6],[ 7, 10]])