代码之家  ›  专栏  ›  技术社区  ›  Jan-Bert

TypeError numpy dtype int类型错误

  •  1
  • Jan-Bert  · 技术社区  · 6 年前

    我有下面的python(3)脚本。我有numpy1.13.1(一个旧版本,但是我的问题是 dtype 应该有用)。 根据 this 数据类型存在于 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
    

    你看我换衣服的时候 数据类型 int int32 int64 它失败了。可能除了 float 失败。我想这不应该这样! 我可以加一个小补丁 int(np.ceil(...)) 结果就是我想要的。

    1. numpy reference ceil ).

    谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   user3483203    6 年前

    这个失败的原因仅仅是因为 dtype 由numpy ufuncs治疗。它不仅覆盖输出数据类型,而且 计算的数据类型 ceil ufunc不支持涉及整数的计算,因此失败:

    From the docs kwargs :

    数据类型

    推荐文章