Replace all elements of Python NumPy Array that are greater than some value Replace all elements of Python NumPy Array that are greater than some value python python

Replace all elements of Python NumPy Array that are greater than some value


I think both the fastest and most concise way to do this is to use NumPy's built-in Fancy indexing. If you have an ndarray named arr, you can replace all elements >255 with a value x as follows:

arr[arr > 255] = x

I ran this on my machine with a 500 x 500 random matrix, replacing all values >0.5 with 5, and it took an average of 7.59ms.

In [1]: import numpy as npIn [2]: A = np.random.rand(500, 500)In [3]: timeit A[A > 0.5] = 5100 loops, best of 3: 7.59 ms per loop


Since you actually want a different array which is arr where arr < 255, and 255 otherwise, this can be done simply:

result = np.minimum(arr, 255)

More generally, for a lower and/or upper bound:

result = np.clip(arr, 0, 255)

If you just want to access the values over 255, or something more complicated, @mtitan8's answer is more general, but np.clip and np.minimum (or np.maximum) are nicer and much faster for your case:

In [292]: timeit np.minimum(a, 255)100000 loops, best of 3: 19.6 µs per loopIn [293]: %%timeit   .....: c = np.copy(a)   .....: c[a>255] = 255   .....: 10000 loops, best of 3: 86.6 µs per loop

If you want to do it in-place (i.e., modify arr instead of creating result) you can use the out parameter of np.minimum:

np.minimum(arr, 255, out=arr)

or

np.clip(arr, 0, 255, arr)

(the out= name is optional since the arguments in the same order as the function's definition.)

For in-place modification, the boolean indexing speeds up a lot (without having to make and then modify the copy separately), but is still not as fast as minimum:

In [328]: %%timeit   .....: a = np.random.randint(0, 300, (100,100))   .....: np.minimum(a, 255, a)   .....: 100000 loops, best of 3: 303 µs per loopIn [329]: %%timeit   .....: a = np.random.randint(0, 300, (100,100))   .....: a[a>255] = 255   .....: 100000 loops, best of 3: 356 µs per loop

For comparison, if you wanted to restrict your values with a minimum as well as a maximum, without clip you would have to do this twice, with something like

np.minimum(a, 255, a)np.maximum(a, 0, a)

or,

a[a>255] = 255a[a<0] = 0


I think you can achieve this the quickest by using the where function:

For example looking for items greater than 0.2 in a numpy array and replacing those with 0:

import numpy as npnums = np.random.rand(4,3)print np.where(nums > 0.2, 0, nums)