使用numpy.take键入转换错误

Jai*_*ime 6 python numpy lookup-tables

我有一个存储65536个uint8值的查找表(LUT):

lut = np.random.randint(256, size=(65536,)).astype('uint8')
Run Code Online (Sandbox Code Playgroud)

我想使用此LUT转换uint16s 数组中的值:

arr = np.random.randint(65536, size=(1000, 1000)).astype('uint16')
Run Code Online (Sandbox Code Playgroud)

我想要进行转换,因为最后一个数组可能会变得非常大.当我尝试它时,会发生以下情况:

>>> 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
Run Code Online (Sandbox Code Playgroud)

我不明白发生了什么.我知道,没有一个out参数,返回的是相同的D型的lut,所以uint8.但为什么不能uint8被投入uint16?如果你问numpy:

>>> np.can_cast('uint8', 'uint16')
True
Run Code Online (Sandbox Code Playgroud)

显然以下工作:

>>> 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)
Run Code Online (Sandbox Code Playgroud)

但这也有效:

>>> 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)
Run Code Online (Sandbox Code Playgroud)

这真的没有意义,因为现在int32s被强制转换为uint16s,这绝对不是一件安全的事情:

>>> np.can_cast('int32', 'uint16')
False
Run Code Online (Sandbox Code Playgroud)

我的代码工作,如果我设置lut在的D型到任何东西uint16,uint32,uint64,int32int64,但失败了uint8,int8int16.

我错过了什么,或者这只是简单地打破了numpy?

解决方案也是受欢迎的...因为LUT不是那么大,我想它的类型与数组相匹配并不是那么糟糕,即使它占用了两倍的空间,但是这样做感觉不对. ..

有没有办法告诉numpy不担心铸造安全?

lum*_*lum 2

有趣的问题。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;
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下outarrbutdtype是其中之一lut,即uint8。因此它尝试转换arruint8,但失败了。我不得不说,我不确定为什么它需要这样做,只是指出它确实如此......出于某种原因,take似乎假设您希望输出数组dtypelut.

顺便说一句,在许多情况下,调用PyArray_FromArray实际上会创建一个新数组,并且替换不会到位。例如,如果您调用takewithmode='raise'(默认值,以及示例中发生的情况),或者每当lut.dtype != arr.dtype. 好吧,至少应该如此,而且我无法解释为什么,当您转换lutint32输出数组时仍然如此uint16这对我来说是个谜 - 也许它与NPY_ARRAY_UPDATEIFCOPY标志有关(另请参阅此处)。

底线:

  1. numpy 的行为确实很难理解......也许其他人会提供一些关于它为什么这样做的见解
  2. 我不会尝试arr就地处理 - 无论如何,在大多数情况下似乎都会在后台创建一个新数组。我只是简单地选择arr = lut.take(arr)- 顺便说一句,这最终将释放 . 之前使用的一半内存arr