如何解决错误:'numpy' has no attribute 'float' in Python?
我使用的是numpy==1.24.0
。 在运行这个示例代码行时:
import numpy as np
num = np.float(3)
我得到了这个错误:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/ubuntu/.local/lib/python3.8/site-packages/numpy/__init__.py", line 284, in __getattr__
raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'float'
请帮助我解决这个问题。
np.float
是内置的float
的一个废弃的别名。要消除这个警告,请使用float
本身。这样做不会修改任何行为,是安全的。如果你特别想要numpy的标量类型,在这里使用np.float64
。在NumPy 1.20中被弃用;更多细节和指导。numpy.org/devdocs/release/1.20.0-notes.html#deprecations
- MattDMo 2022-12-18
float
对象,但如前所述,numpy.float
已被废弃......并在 1.24 中被移除。你可以使用float
,或者从np.float32
、np.float64
、np.float128
中挑选一个(是所有的吗?)在我看来,第二个选项是合理的。
- tdelaney 2022-12-18
np.float
呢?
- hpaulj 2022-12-19
答案已经在@mattdmo和@tdelaney的评论中提供了。
numpy 1.20 (release notes) 废弃了
numpy.float
、numpy.int
和类似的别名,导致他们发布了一个废弃警告numpy 1.24 (release notes)完全删除了这些别名,当它们被使用时,会产生错误。
在许多情况下,你可以简单地用等价的Python内置类型来替换被废弃的numpy类型,例如,numpy.float
变成了"普通"Python float
。
关于如何处理各种废弃类型的详细指南,请仔细查看1.20版本的发行说明中的表格和指南。
...
To give a clear guideline for the vast majority of cases, for the types
bool
,object
,str
(andunicode
) using the plain version is shorter and clear, and generally a good replacement. Forfloat
andcomplex
you can usefloat64
andcomplex128
if you wish to be more explicit about the precision.For
np.int
a direct replacement withnp.int_
orint
is also good and will not change behavior, but the precision will continue to depend on the computer and operating system. If you want to be more explicit and review the current use, you have the following alternatives:
np.int64
ornp.int32
to specify the precision exactly. This ensures that results cannot depend on the computer or operating system.np.int_
orint
(the default), but be aware that it depends on the computer and operating system.- The C types:
np.cint
(int
),np.int_
(long
),np.longlong
.np.intp
which is 32bit on 32bit machines 64bit on 64bit machines. This can be the best type to use for indexing....
如果你的依赖关系使用已废弃的类型,一个快速的解决方法是将你的numpy版本回滚到<1.24
(正如其他一些答案中所建议的),同时等待依赖关系赶上。另外,你可以自己创建一个补丁,并打开一个拉动请求,或者在你自己的代码中对依赖关系进行修补。
在1.24的版本中:
The deprecation for the aliases np.object, np.bool, np.float, np.complex, np.str, and np.int is expired (introduces NumPy 1.20). Some of these will now give a FutureWarning in addition to raising an error since they will be mapped to the NumPy scalars in the future.
pip install "numpy<1.24"
来解决这个问题。
In [1]: import numpy as np
In [2]: np.__version__
Out[2]: '1.23.5'
In [3]: np.float(3)
<ipython-input-3-8262e04d58e1>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
np.float(3)
Out[3]: 3.0
我删除了numpy.py,然后更新了我的numpy,就成功了!
注意:numpy的版本=1.23.3
np.float
在1.24版本中被删除了。拥有自己的"numpy.py"文件会像你说的那样导致这个问题。但也可能是np.float被弃用了。我们得看看OP是怎么说的。
- tdelaney 2022-12-18