51

I am using 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'

How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nawin K Sharma
  • 704
  • 2
  • 6
  • 14
  • 14
    `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 – MattDMo Dec 18 '22 at 20:45
  • 4
    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. – tdelaney Dec 18 '22 at 21:09
  • 1
    @hpaulj: We are using this to maintain old code base. – Nawin K Sharma Dec 23 '22 at 06:08
  • 1
    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 numpy==1.22.4 – Nawin K Sharma Dec 26 '22 at 09:44

7 Answers7

46

The answer is already provided in the comments by @mattdmo and @tdelaney:

  • NumPy 1.20 (release notes) deprecated numpy.float, numpy.int, and similar aliases, causing them to issue a deprecation warning

  • NumPy 1.24 (release notes) removed these aliases altogether, causing an error when they are used

In many cases you can simply replace the deprecated NumPy types by the equivalent Python built-in type, e.g., numpy.float becomes a "plain" Python float.

For detailed 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:

...

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. For float and complex you can use float64 and complex128 if you wish to be more explicit about the precision.

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. 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. 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.
  • 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.

...

If you have dependencies that use the deprecated types, a quick workaround would be to roll back your NumPy version to 1.24 or less (as suggested in some of the other answers), while waiting for the dependency to catch up. Alternatively, you could create a patch yourself and open a pull request, or monkey patch the dependency in your own code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
djvg
  • 11,722
  • 5
  • 72
  • 103
24

In the 1.24 version:

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" to work around it.

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Serhii
  • 1,367
  • 3
  • 13
  • 31
8

Try to use simple "monkey path". Add line like

np.float = float    

or

np.int = int    

in case module 'numpy' has no attribute 'int'

np.object = object    

module 'numpy' has no attribute 'object'

np.bool = bool    

and so on... (if problem with last Numpy versions)

Vegarus
  • 101
  • 1
  • 3
  • How to do this if the problem is in the imported library (dependency?). For example, scikit-image or FFmpeg use these np.float , np.int... – tetukowski Jun 20 '23 at 23:45
  • This trick sometimes helps me, when some old libraries are searching for old Numpy aliases (np.int, np.float etc...). You may put this strings in the first lines of YOUR code and thus replace calls by the modern_Numpy_understandable calls. – Vegarus Jun 22 '23 at 08:05
  • Do you mean it must be the first line of my code before importing libraries? Before, I tried it by putting the monkey path before the main program, but after those lines of importing libraries. It didn't work, that's why I asked. Thank you. – tetukowski Jun 23 '23 at 11:20
  • 1
    At least, after importing Numpy as np. If it doesn't help, try downgrade to older Numpy version. Like people said before. – Vegarus Jun 24 '23 at 17:34
3

I removed numpy.py and then updated my NumPy installation. It worked!

Note: NumPy version 1.23.3

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Gökkaya
  • 408
  • 6
  • 20
  • 1
    It looks like `np.float` was removed in version 1.24. Having your own "numpy.py" file will cause the problem like you say. But it could also be that np.float was deprecated. We'll have to see what OP says about it. – tdelaney Dec 18 '22 at 21:11
  • pip install numpy=1.23.3 – aravinda_gn Aug 04 '23 at 10:20
3

I solved by updating my "openpyxl" using

{pip install --upgrade openpyxl}

The error came up while trying to read an excel file

Rojo
  • 2,749
  • 1
  • 13
  • 34
0

I faced the same issue when I was reading a .xlsx file. You can convert it to csv and this will resolve the issue. However for updating numpy some times you need to get the directory of numpy package:

import numpy
print(numpy.__path__)

For updating it you can use the code below:

pip install numpy --upgrade

You can also check this page: How can I upgrade NumPy?

S.B
  • 13,077
  • 10
  • 22
  • 49
0

numpy-1.24.3

Link : https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

Instead of np.float you can use any one of these

>>float

>>numpy.float64

>>numpy.double

>>numpy.float_
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44