【问题标题】:KeyError with np.uint32 and numpy.uint32带有 np.uint32 和 numpy.uint32 的 KeyError
【发布时间】:2017-06-22 14:04:54
【问题描述】:

首先我得到的错误是:

import numpy as np    
dtype_range = {np.bool_: (False, True),
           np.bool8: (False, True),
           np.uint8: (0, 255),
           np.uint16: (0, 65535),
           np.int8: (-128, 127),
           np.int16: (-32768, 32767),
           np.int64: (-2**63, 2**63 - 1),
           np.uint64: (0, 2**64 - 1),
           np.int32: (-2**31, 2**31 - 1),
           np.uint32: (0, 2**32 - 1),
           np.float32: (-1, 1),
           np.float64: (-1, 1)}

dtype_range[image.dtype.type]

>>>KeyError: <type 'numpy.uint32'>

疯狂的是,当我这样做时

print image.dtype.type
>>><type 'numpy.uint32'>

现在的主要问题是,这发生在 skimage.io 的标准库中,确切地说是 dtype.py,我无法更改该源代码。所以我想知道在传递我的论点image 以使其发挥作用时,我该如何改变或改变什么?我的猜测是命名空间有问题,因为它是npnumpy?但是我如何影响我的数据以将其保存为np.uint32 而不是numpy.uint32??

我正在使用 Python 2.7 顺便说一句。

我们将不胜感激。

编辑:

我将图像投射到uint8 by

image = image.astype(uint8)

现在这并没有给我一个错误,即使

print image.astype(uint8).dtype.type
>>><type 'numpy.uint8'>

当然是你所期望的。

这解决了我的问题。但是,如果有人知道这里发生了什么,我会很高兴回答只是为了理解这个问题。无论如何谢谢:)

编辑:

现在这是从 IPython 控制台的输出中复制的:

 image = np.array([35, 37, 39, 36, 34, 31, 33, 32, 32, 33, 31, 33, 30, 34, 36, 37, 36,
   32, 33, 30, 28, 30, 28, 28, 29, 30, 29, 31, 30, 31, 36, 33, 34, 31,
   34, 35, 34, 32, 29, 26, 25, 27, 25, 26, 25, 27, 30, 30, 28, 26, 28,
   30, 32, 34, 36, 36, 36, 32, 36, 37, 34, 34, 35, 33, 33, 30, 33, 36,
   36, 36, 33, 33, 39, 38, 34, 32, 32, 29, 28, 29, 30, 32, 32, 28, 30,
   32, 34, 30, 28, 32, 34, 34, 35, 33, 35, 33, 33, 35, 37, 39], dtype=uint32)

【问题讨论】:

  • 您的复制粘贴有问题。 print 语句应该打印&lt;type 'numpy.uint32'&gt; 而不仅仅是numpy.uint32。否则你的错误是你用字符串而不是类型来索引你的dict。
  • 是的,你是对的,我实际上并没有在源代码中打印它,而是在 spyder 的 Ipython 控制台中执行它,这就是为什么我的输出看起来不同的原因。我把它改成了正确的形式。

标签: python numpy scikit-image uint32


【解决方案1】:

我无法使用 Python 2.7.12 (Spyder 3.1.4) 和 NumPy 1.11.1 重现您的错误。当我运行这个 sn-p 时:

import numpy as np

image = np.array([[35, 37, 39, 36, 34, 31], 
                  [33, 32, 32, 33, 31, 33], 
                  [30, 34, 36, 37, 36, 32],
                  [33, 30, 28, 30, 28, 28]], dtype=np.uint32)

dtype_range = {np.bool_: (False, True),
               np.bool8: (False, True),
               np.uint8: (0, 255),
               np.uint16: (0, 65535),
               np.int8: (-128, 127),
               np.int16: (-32768, 32767),
               np.int64: (-2**63, 2**63 - 1),
               np.uint64: (0, 2**64 - 1),
               np.int32: (-2**31, 2**31 - 1),
               np.uint32: (0, 2**32 - 1),
               np.float32: (-1, 1),
               np.float64: (-1, 1)}

print(image.dtype.type)
print(dtype_range[image.dtype.type])

我得到了这个输出:

<type 'numpy.uint32'>
(0, 4294967295L)

我的猜测是,当您在代码中的某处将image 转换为uint32 时,就会出现问题。你能分享image 并给我们看完整的代码吗?

【讨论】:

  • 我添加了image的一部分,因为整个东西很大,但是我检查了,错误仍然出现,还有image的一小部分
  • 我在您的代码中将dtype=uint32 替换为dtype=np.uint32 并且没有出现错误
  • 你在哪里换的?
  • 我刚刚编辑了我的答案。能否确认运行修改后的代码是否报错?
  • 好吧,我从另一个函数中获取了我的变量 image,所以我不能只在脚本中将 dtype 从 uint 更改为 np.uint。或者我​​可以吗?
猜你喜欢
  • 2013-07-27
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 2020-11-23
  • 1970-01-01
相关资源
最近更新 更多