1

我正在使用一个需要 NumPy 的包。到目前为止,它工作正常。但是今天,由于扩展了我的代码,我需要最新版本的 NumPy。旧版本是 17.something,我安装了最新版本。之后我面临下面提到的问题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.
4

3 回答 3

1

将您的numpy版本降级到: 1.16

pip install numpy==1.16

为我工作。

于 2020-04-20T22:18:15.990 回答
1

在 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

于 2020-01-18T17:46:15.653 回答
0

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

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

num值需要作为整数传递。

于 2020-12-10T05:32:58.353 回答