【问题标题】:Raise TypeError (TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer)引发 TypeError(TypeError:<class 'numpy.float64'> 类型的对象不能安全地解释为整数)
【发布时间】:2020-01-18 15:21:51
【问题描述】:

我正在使用一个需要 NumPy 的包。直到现在它工作正常。但是今天,由于扩展了我的代码,我需要最新版本的 NumPy。旧版本是 17.something,我安装了最新版本。之后我面临下面提到的问题The detailed link of the question on Github

 File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\core\function_base.py", line 119, in linspace
  raise TypeError(
TypeError: object of type <class 'numpy.float64'> cannot be safely interpreted as an integer.

【问题讨论】:

  • 你能分享你的代码吗?
  • 确定有问题的表达式,以及应该是整数但实际上来自浮点 dtype 数组的变量(例如索引)。较新的 numpy 加强了过去的一些自动转换。
  • @AMC 代码来自一个名为 cosmoTransitions 的包(由 4-5 个不同的代码组成)https://github.com/clwainwright/CosmoTransitions
  • 这个错误是什么时候出现的?
  • 看起来这是由于 1.18 的变化。在以前的版本(例如 1.17)中,对 num 中的 linspace 使用浮点数会产生弃用警告(可能会被忽略)。最近,这已更改为错误。最简单的修复可能是恢复到早期的 numpy 版本。使用第三方库时,使用最新的 numpy 并不总是最好的主意。解决不兼容需要一段时间。

标签: python numpy package numpy-ufunc


【解决方案1】:

将您的 numpy 版本降级到:1.16

pip install numpy==1.16

为我工作。

【讨论】:

    【解决方案2】:

    在 1.17 的 numpy 中

    In [216]: np.linspace(0,1,10.)                                                                   
    Out[216]: 
    array([0.        , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
           0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.        ])
    

    更新到 1.18

    In [2]: np.linspace(0,10,10.)                                                                    
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    /usr/local/lib/python3.6/dist-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
        116     try:
    --> 117         num = operator.index(num)
        118     except TypeError:
    
    TypeError: 'float' object cannot be interpreted as an integer
    
    During handling of the above exception, another exception occurred:
    
    TypeError                                 Traceback (most recent call last)
    <ipython-input-2-1e9a5a5e4a05> in <module>
    ----> 1 np.linspace(0,10,10.)
    
    <__array_function__ internals> in linspace(*args, **kwargs)
    
    /usr/local/lib/python3.6/dist-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype, axis)
        119         raise TypeError(
        120             "object of type {} cannot be safely interpreted as an integer."
    --> 121                 .format(type(num)))
        122 
        123     if num < 0:
    
    TypeError: object of type <class 'float'> cannot be safely interpreted as an integer.
    

    那个num = operator.index(num) 测试曾经在deprecation 警告函数中,num = _index_deprecate(num),现在它引发了一个错误。

    https://github.com/numpy/numpy/blob/v1.17.0/numpy/core/function_base.py#L37-L179

    【讨论】:

      【解决方案3】:

      我偶然发现了这个错误,不需要降级。

      np.linspace(0,1,int(10.)) 
      

      num 值需要作为整数传递。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-03
        • 2020-06-03
        相关资源
        最近更新 更多