问题 添加numpy数组时避免溢出
我想用数据类型uint8添加numpy数组。我知道这些数组中的值可能足够大,可以发生溢出。所以我得到类似的东西:
a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
a += b
现在,是 [150 250 44]
。但是,我希望uint8的值太大而不是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)
问题是,我的数组非常大,因此创建具有更大数据类型的第三个数组可能是内存问题。是否有一种快速且更有效的内存方式来实现所描述的结果?
12820
2018-04-13 17:15
起源
答案:
您可以通过创建第三个dtype uint8数组和一个bool数组来实现此目的 (它们一起比一个uint16阵列的内存效率更高)。
np.putmask
对于避免临时数组很有用。
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-array索引替换了超快整数加法。
解释它的工作原理
建设 c
上面的数组使用了一个numpy广播技巧。添加一个形状数组 (N,)
和形状的数组 (1,N)
广播两者都是 (N,N)
类似,因此结果是所有可能总和的NxN数组。然后,我们剪辑它。我们得到一个满足以下条件的2dim数组: c[i,j]=min(i+j,255)
对于每个我,j。
然后剩下的是使用花式索引抓取正确的值。使用您提供的输入,我们访问:
c[( [100, 200, 250] , [50, 50, 50] )]
第一个索引数组指第一个暗淡,第二个指向第二个暗淡。因此,结果是一个与索引数组形状相同的数组((N,)
),由值组成 [ c[100,50] , c[200,50] , c[250,50] ]
。
7
2018-04-13 17:25
答案:
您可以通过创建第三个dtype uint8数组和一个bool数组来实现此目的 (它们一起比一个uint16阵列的内存效率更高)。
np.putmask
对于避免临时数组很有用。
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-array索引替换了超快整数加法。
解释它的工作原理
建设 c
上面的数组使用了一个numpy广播技巧。添加一个形状数组 (N,)
和形状的数组 (1,N)
广播两者都是 (N,N)
类似,因此结果是所有可能总和的NxN数组。然后,我们剪辑它。我们得到一个满足以下条件的2dim数组: c[i,j]=min(i+j,255)
对于每个我,j。
然后剩下的是使用花式索引抓取正确的值。使用您提供的输入,我们访问:
c[( [100, 200, 250] , [50, 50, 50] )]
第一个索引数组指第一个暗淡,第二个指向第二个暗淡。因此,结果是一个与索引数组形状相同的数组((N,)
),由值组成 [ c[100,50] , c[200,50] , c[250,50] ]
。
7
2018-04-13 17:25
这是一种方式:
>>> 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)
1
2018-04-13 18:32
您可以使用Numba真正实现它,例如:
import numba
@numba.jit('void(u1[:],u1[:])', locals={'temp': numba.uint16})
def add_uint8_inplace_clip(a, b):
for i in range(a.shape[0]):
temp = a[i] + b[i]
a[i] = temp if temp<256 else 255
add_uint8_inplace_clip(a, b)
或者使用Numexpr,例如:
import numexpr
numexpr.evaluate('where((a+b)>255, 255, a+b)', out=a, casting='unsafe')
Numexpr upcasts uint8
至 int32
在内部,然后把它放回去 uint8
阵列。
1
2018-04-13 19:30
def non_overflowing_sum(a, b)
c = np.uint16(a)+b
c[np.where(c>255)] = 255
return np.uint8( c )
它也交换内存,但我发现更优雅,临时uint16在返回后转换后被释放
1
2018-06-02 15:11
这样做
>>> a + np.minimum(255 - a, b)
array([150, 250, 255], dtype=uint8)
一般来说,获取数据类型的最大值
np.iinfo(np.uint8).max
0
2018-04-13 17:23
有 numpy中的一个函数 为了这:
numpy.nan_to_num(x)[source]
将nan替换为零,使用有限数字替换inf。
返回一个数组或标量替换非数字(NaN),零,(正)无穷大,数字非常大,负无穷大,数字非常小(或负数)。
新的数组与x的形状相同,x中的元素的dtype具有最高的精度。
如果x不精确,则NaN被零替换,并且无穷大(-infinity)被适合输出dtype的最大(最小或最负)浮点值替换。如果x不精确,则返回x的副本。
我不确定它是否适用于uint8,因为在输出中提到浮点,但对于其他读者,它可能是有用的
0
2018-02-28 16:05