简体   繁体   English

如何解决错误'numpy'在Python中没有属性'float'?

[英]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.floatnumpy.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 (and unicode ) using the plain version is shorter and clear, and generally a good replacement.为了为绝大多数情况提供明确的指导方针,对于boolobjectstr (和unicode )类型,使用普通版本更短更清晰,通常是一个很好的替代品。 For float and complex you can use float64 and complex128 if you wish to be more explicit about the precision.对于floatcomplex ,如果您希望更明确地说明精度,则可以使用float64complex128

For np.int a direct replacement with np.int_ or int 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 or np.int32 to specify the precision exactly. np.int64np.int32以准确指定精度。 This ensures that results cannot depend on the computer or operating system.这确保了结果不会依赖于计算机或操作系统。
  • np.int_ or int (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.

相关问题 如何解决属性错误'float' object has no attribute 'split' in python? - How to solve the Attribute error 'float' object has no attribute 'split' in python? Python错误:“ numpy.float64”对象没有属性“ append” - Python Error: 'numpy.float64' object has no attribute 'append' 如何更正Python属性错误:Float对象没有属性“ set” - How to correct Python Attribute Error: Float object has no attribute 'set' Python / Numpy AttributeError:&#39;float&#39;对象没有属性&#39;sin&#39; - Python / Numpy AttributeError: 'float' object has no attribute 'sin' numpy nanmean() 'float' object has no attribute 'dtype' 错误 - numpy nanmean () 'float' object has no attribute 'dtype' error 如何解决 numpy.ndarray' object 在 python 上没有属性“直方图” - How do I solve numpy.ndarray' object has no attribute 'histogram' on python 计算热指数时如何解决 Metpy ('numpy.ndarray' object has no attribute 'to') 错误? - How to solve Metpy ('numpy.ndarray' object has no attribute 'to') error when calculating heat index? 如何解决这个错误:AttributeError: 'numpy.ndarray' object has no attribute 'crop' - How to solve this error: AttributeError: 'numpy.ndarray' object has no attribute 'crop' "如何解决python中的这个属性错误?" - How to solve this attribute error in python? 如何解决 AttributeError: module 'numpy' has no attribute 'bool'? - How to solve AttributeError: module 'numpy' has no attribute 'bool'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM