numpy.take 的类型转换错误
我有一个查找表(LUT),里面存储了65536个uint8
类型的值:
lut = np.random.randint(256, size=(65536,)).astype('uint8')
我想用这个查找表来转换一个uint16
类型数组里的值:
arr = np.random.randint(65536, size=(1000, 1000)).astype('uint16')
而且我想在原地进行转换,因为这个数组可能会非常大。当我尝试这样做时,发生了以下情况:
>>> np.take(lut, arr, out=arr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\numpy\core\fromnumeric.py", line 103, in take
return take(indices, axis, out, mode)
TypeError: array cannot be safely cast to required type
我不太明白发生了什么。我知道,如果没有out
参数,返回的类型和lut
是一样的,所以是uint8
。但是为什么uint8
不能转换成uint16
呢?如果你问numpy:
>>> np.can_cast('uint8', 'uint16')
True
显然,下面的代码可以正常工作:
>>> lut = lut.astype('uint16')
>>> np.take(lut, arr, out=arr)
array([[173, 251, 218, ..., 110, 98, 235],
[200, 231, 91, ..., 158, 100, 88],
[ 13, 227, 223, ..., 94, 56, 36],
...,
[ 28, 198, 80, ..., 60, 87, 118],
[156, 46, 118, ..., 212, 198, 218],
[203, 97, 245, ..., 3, 191, 173]], dtype=uint16)
但这个也可以:
>>> lut = lut.astype('int32')
>>> np.take(lut, arr, out=arr)
array([[ 78, 249, 148, ..., 77, 12, 167],
[138, 5, 206, ..., 31, 43, 244],
[ 29, 134, 131, ..., 100, 107, 1],
...,
[109, 166, 14, ..., 64, 95, 102],
[152, 169, 102, ..., 240, 166, 148],
[ 47, 14, 129, ..., 237, 11, 78]], dtype=uint16)
这真的让人困惑,因为现在int32
类型的值被转换成uint16
,这显然是不安全的操作:
>>> np.can_cast('int32', 'uint16')
False
我的代码在把lut
的类型设置为uint16
、uint32
、uint64
、int32
或int64
时可以正常工作,但在uint8
、int8
和int16
时就失败了。
我是不是漏掉了什么,还是说这在numpy里就是个bug?
如果有其他解决办法也欢迎分享……因为查找表并不大,我想让它的类型和数组匹配,即使这样会占用两倍的空间,但这样做总感觉不太对……
有没有办法告诉numpy不要担心转换的安全性?
1 个回答
这个问题挺有意思的。numpy.take(lut, ...)
会变成 lut.take(...)
,你可以在这里查看它的源代码:
https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/item_selection.c#L28
我认为异常是在 第105行 抛出的:
obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags);
if (obj == NULL) {
goto fail;
}
在你的情况下,out
是 arr
,但是 dtype
是 lut
的类型,也就是 uint8
。所以它试图把 arr
转换成 uint8
,这就失败了。我得说我不太明白为什么需要这样做,只是指出它确实是这样……出于某种原因,take
似乎假设你希望输出数组的类型和 lut
一样。
顺便说一下,在很多情况下,调用 PyArray_FromArray
实际上会创建一个新数组,而不是在原地替换。比如说,如果你用 mode='raise'
调用 take
(这是默认情况,也是你例子中的情况),或者每当 lut.dtype != arr.dtype
的时候。好吧,至少应该是这样,我也无法解释为什么,当你把 lut
转换成 int32
时,输出数组仍然是 uint16
!这对我来说是个谜——也许这和 NPY_ARRAY_UPDATEIFCOPY 标志有关(你也可以在 这里 查看)。
总结一下:
- numpy 的行为确实很难理解……也许其他人能提供一些关于它为什么这样做的见解。
- 我不会尝试在原地处理
arr
——在大多数情况下,似乎反正会在后台创建一个新数组。我会直接用arr = lut.take(arr)
,顺便说一下,这样最终会释放一半之前arr
使用的内存。