添加numpy数组时避免溢出

2024-04-20 12:58:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想添加数据类型为uint8的numpy数组。我知道这些数组中的值可能大到足以发生溢出。所以我得到了如下信息:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
a += b

现在,a是[150 250 44]。但是,我不希望出现溢出,而是希望uint8所允许的最大值太大。所以我想要的结果是[150 250 255]

我可以用下面的代码得到这个结果:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
c = np.zeros((1,3), dtype=np.uint16)
c += a
c += b
c[c>255] = 255
a = np.array(c, dtype=np.uint8)

问题是,我的数组非常大,因此创建第三个数据类型更大的数组可能是内存问题。是否有一种快速且更节省内存的方法来实现所描述的结果?


Tags: 方法内存代码numpy信息npzeros数组
3条回答

您可以通过创建第三个dtype uint8数组,再加上一个bool数组(它们一起比一个uint16数组更节省内存)

^{}对于避免临时数组很有用。

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
c = 255 - b  # a temp uint8 array here
np.putmask(a, c < a, c)  # a temp bool array here
a += b

然而,正如@moarningsun正确指出的那样,bool数组占用的内存量与uint8数组相同,因此这并不一定有用。可以通过避免在任何给定时间有多个临时数组来解决这个问题:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
b = 255 - b  # old b is gone shortly after new array is created
np.putmask(a, b < a, b)  # a temp bool array here, then it's gone
a += 255 - b  # a temp array here, then it's gone

这种方法用内存消耗换取CPU。


另一种方法是预先计算所有可能的结果,即O(1)额外的内存(即独立于数组的大小):

c = np.clip(np.arange(256) + np.arange(256)[..., np.newaxis], 0, 255).astype(np.uint8)
c
=> array([[  0,   1,   2, ..., 253, 254, 255],
          [  1,   2,   3, ..., 254, 255, 255],
          [  2,   3,   4, ..., 255, 255, 255],
          ..., 
          [253, 254, 255, ..., 255, 255, 255],
          [254, 255, 255, ..., 255, 255, 255],
          [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

c[a,b]
=> array([150, 250, 255], dtype=uint8)

如果数组很大,这种方法是最节省内存的。同样,它在处理时间上也很昂贵,因为它用较慢的2dim数组索引代替了超快速的整数加法。

解释它的工作原理

上面的c数组的构造使用了numpy广播技巧。添加一个形状数组(N,)和形状数组(1,N)广播都是(N,N)样的,因此结果是所有可能和的NxN数组。然后,我们剪辑它。我们得到一个2dim数组,它满足每个i,j的c[i,j]=min(i+j,255)

然后剩下的就是使用花哨的索引来获取正确的值。使用您提供的输入,我们可以访问:

c[( [100, 200, 250] , [50, 50, 50] )]

第一个索引数组引用第一个维度,第二个索引数组引用第二个维度。因此,结果是一个与索引数组((N,))形状相同的数组,由值[ c[100,50] , c[200,50] , c[250,50] ]组成。

这里有一个方法:

>>> a = np.array([100, 200, 250], dtype=np.uint8)
>>> b = np.array([50, 50, 50], dtype=np.uint8)
>>> a+=b; a[a<b]=255
>>> a
array([150, 250, 255], dtype=uint8)

怎么样

>>> a + np.minimum(255 - a, b)
array([150, 250, 255], dtype=uint8)

通常,使用获取数据类型的最大值

np.iinfo(np.uint8).max

相关问题 更多 >