当前位置:   article > 正文

成功解决AttributeError: module ‘numpy‘ has no attribute ‘float‘._attributeerror: module 'numpy' has no attribute 'f

attributeerror: module 'numpy' has no attribute 'float'. `np.float` was a de

成功解决AttributeError: module 'numpy' has no attribute 'float'.

问题描述

AttributeError: module ‘numpy’ has no attribute ‘float’.
np.float was a deprecated alias for the builtin float. To avoid this error in existing code, 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.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

解决方案一:

不用改代码的,就重新安装numpy
出现这个问题是因为np.float从1.24起被删除。所用的代码是依赖于旧版本的Numpy。您可以将你的Numpy版本降级到1.23.5.

conda install numpy==1.23.5
  • 1

或者用pip安装也可以。

解决方案二:

将代码中的np.float改为float,如下:
在大多数情况下,只需将 numpy 的别名替换为内置的 Python 类型就可以解决问题。boolstrint等也类似。

import numpy as np

# Instead of numpy's float alias
x = np.float(10)

# Use the built-in float
x = float(10)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

如:

np.float = float
np.int = int
np.object = object
np.bool = bool
  • 1
  • 2
  • 3
  • 4

或者

np.float = np.float64
  • 1

原因:

出现此问题的原因:

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: numpy.org/devdocs/release/1.20.0-notes.html#deprecations
np.float 是内置 float 的已弃用别名。要消除此警告,请单独使用 float。这样做不会改变任何行为并且是安全的。如果您特别想要 numpy 标量类型,请在此处使用 np.float64。在 NumPy 1.20 中已弃用;有关更多详细信息和指导:numpy.org/devdocs/release/1.20.0-notes.html#deprecations

This was the standard python float object, but as mentioned, numpy.float has been deprecated… and removed in 1.24. You can either use float or pick one of the np.float32, np.float64, np.float128 (is that all of them?!). That second option seems reasonable to me.
这是标准的 python float 对象,但正如前面提到的,numpy.float 已被弃用…并在 1.24 中删除。您可以使用 float 或选择 np.float32、np.float64、np.float128 之一(是全部吗?!)。第二种选择对我来说似乎是合理的。

As np.float is deprecated and in my code base, np.float is in multiple places, For now I downgraded Numpy version. This worked for me: pip install numpy1.22.4
由于 np.float 已被弃用,并且在我的代码库中,np.float 存在于多个位置,现在我降级了 Numpy 版本。这对我有用: pip install numpy
1.22.4

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/448985
推荐阅读
相关标签
  

闽ICP备14008679号