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

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 np
In [2]: A = np.random.rand(500, 500)
In [3]: timeit A[A > 0.5] = 5
100 loops, best of 3: 7.59 ms per loop

How to replace all elements of Python NumPy Array that are greater than a several values?

Are you looking for dual np.where i.e

A = np.array([0,1,2,3,1,-5,-6,-7])

k = np.where(A>0,1,np.where(A<0,0.01,A))

Or you can use np.select for multiple conditions .

k = np.select([A>0,A<0],[1,.01],A)

Ouptut :

[ 0.    1.    1.    1.    1.    0.01  0.01  0.01]

How to replace values in numpy array at the same time

It's better to use np.select if you've multiple conditions:

a = np.array([7, 1, 2, 0, 2, 3, 4, 0, 5])
a = np.select([a == 7, a == 2], [2, 3], a)

OUTPUT:

[2 1 3 0 3 3 4 0 5]

Replace value in array when greater than x

I wonder why this solution is not provided in the link @DonkeyKong provided:

np.where(arr>0.7, 0, arr)
#Out[282]: array([ 0.5, 0.6, 0. ])

Replace all elements of Python NumPy Array that are EQUAL to some values

Try np.isin:

array[np.isin(array, some_values)] = 0

Replacing values greater than a limit in a numpy array

Use putmask:

import numpy as np

a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
m = np.array([7,6,5,4])

# This is what you need:

np.putmask(a, a >= m, m - 1)

# a is now:

np.array([[0, 1, 2, 3],
[4, 5, 4, 3],
[6, 5, 4, 3]])

Change elements of a numpy array based on a return value of a function to which those elements are passed to

I would rewrite your function to be a bit more vectorized. First, you really don't need to loop through the entire dictionary of CSS colors for every pixel: the lookup table can be trivially precomputed. Second, you can map the five colors you want to RGBA values without using the names as an intermediary. This will make your life much easier since you'll be working with numbers instead of strings most of the time.

names = dict.fromkeys(['green', 'red', 'orange', 'brown', 'white'])
for key, name in webcolors.CSS3_HEX_TO_NAMES.items():
if name in names:
names[name] = key
lookup = np.array([webcolors.hex_to_rgb(key) + (1,) for key in names.values()])

Since the number of colors is small, you can compute an Nx5 array of distances to the colors:

distance = ((rgba[..., None, :] - lookup)**2).sum(axis=-1)

If you don't want to include the transparency in the distance, remove it from the comparison:

distance = ((rgba[..., None, :3] - lookup[..., :3])**2).sum(axis=-1)

This gives you an Nx5 array of distances (where N can be more than one dimension, because of the intentional use of ... instead of :). The minima are at

closest = distance.argmin(-1)

Now you can apply this index directly to the lookup table:

result = lookup[closest]

Here is a sample run:

>>> np.random.seed(42)
>>> rgba = np.random.randint(255, size=(10, 4))
>>> rgba
array([[102, 179, 92, 14],
[106, 71, 188, 20],
[102, 121, 210, 214],
[ 74, 202, 87, 116],
[ 99, 103, 151, 130],
[149, 52, 1, 87],
[235, 157, 37, 129],
[191, 187, 20, 160],
[203, 57, 21, 252],
[235, 88, 48, 218]])
>>> lookup = np.array([
... [0, 255, 0, 1],
... [255, 0, 0, 1],
... [92, 64, 51, 1],
... [255, 165, 0, 1],
... [255, 255, 255, 0]], dtype=np.uint8)

>>> distance = ((rgba[..., None, :3] - lookup[..., :3])**2).sum(axis=-1)
>>> distance
array([[ 24644, 63914, 15006, 32069, 55754],
[ 80436, 62586, 19014, 66381, 60546],
[ 72460, 82150, 28630, 69445, 43390],
[ 15854, 81134, 20664, 41699, 63794],
[ 55706, 57746, 11570, 50981, 58256],
[ 63411, 13941, 5893, 24006, 116961],
[ 66198, 26418, 29294, 1833, 57528],
[ 41505, 39465, 25891, 4980, 63945],
[ 80854, 6394, 13270, 14809, 96664],
[ 85418, 10448, 21034, 8633, 71138]])
>>> closest = distance.argmin(-1)
>>> closest
array([2, 2, 2, 0, 2, 2, 3, 3, 1, 3])
>>> lookup[closest]
array([[ 92, 64, 51, 1],
[ 92, 64, 51, 1],
[ 92, 64, 51, 1],
[ 0, 255, 0, 1],
[ 92, 64, 51, 1],
[ 92, 64, 51, 1],
[255, 165, 0, 1],
[255, 165, 0, 1],
[255, 0, 0, 1],
[255, 165, 0, 1]], dtype=uint8)


Related Topics



Leave a reply



Submit