简体   繁体   English

"替换大于某个值的 Python NumPy 数组的所有元素"

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

I have a 2D NumPy array and would like to replace all values in it greater than or equal to a threshold T with 255.0.我有一个 2D NumPy 数组,想用 255.0 替换其中大于或等于阈值 T 的所有值。 To my knowledge, the most fundamental way would be:据我所知,最基本的方法是:

shape = arr.shape
result = np.zeros(shape)
for x in range(0, shape[0]):
    for y in range(0, shape[1]):
        if arr[x, y] >= T:
            result[x, y] = 255
  1. What is the most concise and pythonic way to do this?最简洁和最pythonic的方法是什么?

  2. Is there a faster (possibly less concise and/or less pythonic) way to do this?是否有更快(可能不那么简洁和/或不那么 Pythonic)的方式来做到这一点?

This will be part of a window/level adjustment subroutine for MRI scans of the human head.这将是人体头部 MRI 扫描的窗口/水平调整子程序的一部分。 The 2D numpy array is the image pixel data. 2D numpy 数组是图像像素数据。

I think both the fastest and most concise way to do this is to use NumPy's built-in Fancy indexing.我认为最快和最简洁的方法是使用 NumPy 的内置 Fancy 索引。 If you have an ndarray named arr , you can replace all elements >255 with a value x as follows:如果你有一个名为arrndarray ,你可以用值x替换所有>255元素,如下所示:

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.我用 500 x 500 随机矩阵在我的机器上运行它,用 5 替换所有 >0.5 的值,平均需要 7.59 毫秒。

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

Since you actually want a different array which is arr where arr < 255 , and 255 otherwise, this can be done simply:由于您实际上想要一个不同的数组,即arr where arr < 255 ,否则为255 ,这可以简单地完成:

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:如果您只想访问超过 255 的值或更复杂的值,@mtitan8 的答案更笼统,但np.clipnp.minimum (或np.maximum )对您的情况更好更快:

In [292]: timeit np.minimum(a, 255)
100000 loops, best of 3: 19.6 µs per loop

In [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 (ie, modify arr instead of creating result ) you can use the out parameter of np.minimum :如果您想就地进行(即修改arr而不是创建result ),您可以使用np.minimumout参数:

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.) out=名称是可选的,因为参数与函数定义的顺序相同。)

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 :对于就地修改,布尔索引加快了很多(无需单独制作然后修改副本),但仍然不如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 loop

In [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为了比较,如果你想用最小值和最大值来限制你的值,如果没有clip你必须这样做两次,比如

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

or,或者,

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

I think you can achieve this the quickest by using the where function:我认为您可以通过使用where函数最快地实现这一点:

For example looking for items greater than 0.2 in a numpy array and replacing those with 0:例如,在 numpy 数组中查找大于 0.2 的项目并将其替换为 0:

import numpy as np

nums = np.random.rand(4,3)

print np.where(nums > 0.2, 0, nums)

Another way is to use np.place which does in-place replacement and works with multidimentional arrays:另一种方法是使用np.place进行就地替换并使用多维数组:

import numpy as np

# create 2x3 array with numbers 0..5
arr = np.arange(6).reshape(2, 3)

# replace 0 with -10
np.place(arr, arr == 0, -10)

You can consider using numpy.putmask :您可以考虑使用numpy.putmask

np.putmask(arr, arr>=T, 255.0)

Here is a performance comparison with the Numpy's builtin indexing:这是与 Numpy 的内置索引的性能比较:

In [1]: import numpy as np
In [2]: A = np.random.rand(500, 500)

In [3]: timeit np.putmask(A, A>0.5, 5)
1000 loops, best of 3: 1.34 ms per loop

In [4]: timeit A[A > 0.5] = 5
1000 loops, best of 3: 1.82 ms per loop

You can also use & , |您也可以使用& , | (and/or) for more flexibility: (和/或)以获得更大的灵活性:

values between 5 and 10: A[(A>5)&(A<10)] 5 到 10 之间的值: A[(A>5)&(A<10)]

values greater than 10 or smaller than 5: A[(A<5)|(A>10)]大于 10 或小于 5 的值: A[(A<5)|(A>10)]

Lets us assume you have a numpy array that has contains the value from 0 all the way up to 20 and you want to replace numbers greater than 10 with 0让我们假设您有一个numpy数组,其中包含从 0 一直到 20 的值,并且您想用 0 替换大于 10 的数字

import numpy as np

my_arr = np.arange(0,21) # creates an array
my_arr[my_arr > 10] = 0 # modifies the value

Note this will however modify the original array to avoid overwriting the original array try using arr.copy() to create a new detached copy of the original array and modify that instead.请注意,这将修改原始数组以避免覆盖原始数组尝试使用arr.copy()创建原始数组的新分离副本并修改它。

import numpy as np

my_arr = np.arange(0,21)
my_arr_copy = my_arr.copy() # creates copy of the orignal array

my_arr_copy[my_arr_copy > 10] = 0 

np.where() works great! np.where() 效果很好!

np.where(arr > 255, 255, arr)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM