python - 使用 numpy.take 类型转换错误

标签 python numpy lookup-tables

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

lut = np.random.randint(256, size=(65536,)).astype('uint8')

我想使用这个 LUT 来转换 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 的数据类型设置为 uint16uint32uint64 中的任何值,我的代码就可以工作、int32int64,但对于 uint8int8int16 失败。

我是不是遗漏了什么,或者这只是在 numpy 中损坏了?

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

有没有办法告诉 numpy 不用担心转换安全?

最佳答案

有趣的问题。 numpy.take(lut, ...) 被转换为 lut.take(...) 其来源可以在这里查看:

https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/item_selection.c#L28

我相信抛出异常 at line 105 :

obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags);
if (obj == NULL) {
    goto fail;
}

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

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

底线:

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

关于python - 使用 numpy.take 类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14782135/

相关文章:

Python - 使用unittest测试类方法

python Pandas : groupby output duplicate

algorithm - 搜索列表

ruby-on-rails - 我想将我的数据库查找表映射到散列,好主意吗?

python - 配置错误 --- gobject ---- 找不到 python header

python - 在 Python 中模拟整数溢出

python - 使用 matplotlib 和 imshow 平滑二维直方图

python - 如何比较具有不同索引的两个数据帧并打印出重复的行?

python - 如何在Python中有效存储非常大的列表

python - 用python制作查找表,写入可以在C程序中读取的二进制文件