Sometimes, we want to replace all elements of Python NumPy Array that are greater than some value.
In this article, we’ll look at how to replace all elements of Python NumPy Array that are greater than some value.
How to replace all elements of Python NumPy Array that are greater than some value?
To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.
For instance, we write
import numpy as np
A = np.random.rand(500, 500)
A[A > 0.5] = 5
to create a NumPy array A
with some random values.
Then we get all the values that are bigger than 0.5 and set them to 5 with
A[A > 0.5] = 5
Conclusion
To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.