[英]How to solve error 'numpy' has no attribute 'float' in Python?
I am using numpy==1.24.0
.我正在使用
numpy==1.24.0
。 On running this sample code line:在运行此示例代码行时:
import numpy as np
num = np.float(3)
I am getting this error:我收到此错误:
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'
Please help me on this.请帮我解决这个问题。
The answer is already provided in the comments by @mattdmo and @tdelaney : @mattdmo和@tdelaney的评论中已经提供了答案:
numpy 1.20 (release notes) deprecated numpy.float
, numpy.int
, and similar aliases, causing them to issue a deprecation warning numpy 1.20 (发行说明)弃用
numpy.float
、 numpy.int
和类似的别名,导致它们发出弃用警告
numpy 1.24 (release notes) removed these aliases altogether, causing an error when they are used numpy 1.24 (发行说明)完全删除了这些别名,在使用时导致错误
For guidelines on how to deal with various deprecated types, have a closer look at the table and guideline in the release notes for 1.20 :有关如何处理各种弃用类型的指南,请仔细查看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.为了为绝大多数情况提供明确的指导方针,对于
bool
、object
、str
(和unicode
)类型,使用普通版本更短更清晰,通常是一个很好的替代品。 Forfloat
andcomplex
you can usefloat64
andcomplex128
if you wish to be more explicit about the precision.对于
float
和complex
,如果您希望更明确地说明精度,则可以使用float64
和complex128
。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.对于
np.int
,直接替换为np.int_
或int
也很好,不会改变行为,但精度将继续取决于计算机和操作系统。 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.np.int64
或np.int32
以准确指定精度。 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.np.int_
或int
(默认值),但请注意,这取决于计算机和操作系统。- The C types:
np.cint
(int
),np.int_
(long
),np.longlong
.C 类型:
np.cint
(int
)、np.int_
(long
)、np.longlong
。np.intp
which is 32bit on 32bit machines 64bit on 64bit machines.np.intp
在 32 位机器上是 32 位,在 64 位机器上是 64 位。 This can be the best type to use for indexing.这可能是用于索引的最佳类型。
...
...
I removed numpy.py then updated my numpy and it worked!我删除了 numpy.py 然后更新了我的 numpy 并且它起作用了!
Note: numpy version=1.23.3注意:numpy 版本=1.23.3
In the 1.24 version:在 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).
别名 np.object、np.bool、np.float、np.complex、np.str 和 np.int 的弃用已过期(引入 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.
其中一些现在除了引发错误外还会给出 FutureWarning,因为它们将来会映射到 NumPy 标量。
pip install "numpy<1.24"
to fix it. 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.