【发布时间】:2019-02-24 01:22:15
【问题描述】:
您好,我有一个非常简单的问题。但我不明白是什么导致了问题。
我有以下 python(3) 脚本。我有 numpy 1.13.1(一个旧的,但我对dtype 的问题应该可以解决)。
根据this,dtype 存在于ufunc,因为 1.6
import numpy as np
M=1001
N = 2 ** np.ceil(np.log2(M))
N
Out[252]: 1024.0
2 ** np.ceil(np.log2(M),dtype=int)
Traceback (most recent call last):
File "<ipython-input-253-4b982a04c884>", line 1, in <module>
2 ** np.ceil(np.log2(M),dtype=int)
TypeError: No loop matching the specified signature and casting
was found for ufunc ceil
2 ** np.ceil(np.log2(M),dtype=float)
Out[254]: 1024.0
2 ** np.ceil(np.log2(M),dtype=np.float64)
Out[256]: 1024.0
2 ** np.ceil(np.log2(M),dtype=np.float32)
Out[257]: 1024.0
2 ** np.ceil(np.log2(M),dtype=np.int64)
Traceback (most recent call last):
File "<ipython-input-258-9902fa43f3ac>", line 1, in <module>
2 ** np.ceil(np.log2(M),dtype=np.int64)
TypeError: No loop matching the specified signature and casting
was found for ufunc ceil
2 ** np.ceil(np.log2(M),dtype=np.int32)
Traceback (most recent call last):
File "<ipython-input-259-8a2f2834384f>", line 1, in <module>
2 ** np.ceil(np.log2(M),dtype=np.int32)
TypeError: No loop matching the specified signature and casting
was found for ufunc ceil
正如您所见,当我将 dtype 更改为 int、int32 或 int64 时,它会失败。可能除了float 之外的任何东西都失败了。我想这不应该那样做!
我可以添加一个小修复,以便int(np.ceil(...)) 结果是我想要的。
我想知道是什么导致了这个问题?因为我没有在 numpy 参考手册中阅读任何关于此问题的内容(numpy reference ceil)。
如果可能的话,可以按照我开始的方式解决这个问题
谢谢
【问题讨论】:
-
想想
ceil在做什么,为int做这件事是没有意义的。当您指定dtype时,您不仅指定了返回类型,还指定了计算所使用的 dtype。计算完成后,您可以随意将结果更改为整数,但您当前看到的结果是预期的。一些更好的讨论here
标签: python python-3.x numpy int ceil