While working with the Numpy python library, we might come across the error “AttributeError: module ‘numpy’ has no attribute ‘int’ Error”.
AttributeError: module 'numpy' has no attribute 'int'
This error occurs when we try to access the int
attribute of the Numpy module, which doesn’t exist.
Similarly, we might get the same error for object
and float
attributes too. For example:
AttributeError: module 'numpy' has no attribute 'object'
AttributeError: module 'numpy' has no attribute 'float'
In this article, we will discuss the reasons why this error occurs and find out the solution to resolve this issue.
Table of Contents
Cause of “AttributeError: module numpy has no attribute int ” Error
The main cause of the “AttrubuteError module ‘numpy’ has no attribute ‘int'” occurs because from Numpy version 1.24.0, the methods like numpy.int()
, numpy.object()
, numpy.float()
are deprecated.
So, in the latest version, if we try to access these methods, it will throw us the Attribute error in python. For example:
import numpy as np
num = np.int(5)
Output:
AttributeError: module 'numpy' has no attribute 'int'
This error occurs because the ‘int
‘ attributes don’t exist in the Numpy module.
Note: Even though
int
is deprecated in Numpy, you can still use other data types such as int8, int16, int32, etc.
Now, the reason why these methods were deprecated is that before Python did not have int()
or float()
methods. So, we had to import these methods from the numpy library to do mathematical calculations.
However, in the latest version of Python , methods like int(),
float(),
str()
, and object()
comes as an in-built python function. So now we don’t need numpy library to use these functions in our python programs.
Here is the list of numpy attributes that are now deprecated:
- np.object()
- np.int()
- np.float()
- np.complex()
- np.str(), and
- np.bool()
Solution to AttributeError: module ‘numpy’ has no attribute ‘int’.
The solution to this error is very simple, we have to just replace the np.int()
numpy method with the python’s in-built int()
function.
Example:
num = 3.17
print(num) #output : 3.17
print(int(num)) #output : 3
Here, the int()
function returns an integer object from any given number. We do not have to import numpy library anymore.
Now, if you are getting same “Attribute error” for np.object()
, np.float()
or np.bool()
like this:
AttributeError: module 'numpy' has no attribute 'object'
AttributeError: module 'numpy' has no attribute 'float'
AttributeError: module 'numpy' has no attribute 'bool'
You can resolve these errors using the in-built python aliases methods like object()
, float()
and bool()
.
List of deprecated Numpy methods and it’s Python’s builtin Replacements
Here is a list from the official numpy document which shows the Python equivalent for the deprecated numpy methods:
Deprecated Name | Replaced To | Numpy Scalar Type |
---|---|---|
numpy.bool | bool | numpy.bool_ |
numpy.int | int | numpy.int_ (default), numpy.int64 , or numpy.int32 |
numpy.float | float | numpy.float64 , numpy.float_ , numpy.double (equivalent) |
numpy.complex | complex | numpy.complex128 , numpy.complex_ , numpy.cdouble (equivalent) |
numpy.object | object | numpy.object_ |
numpy.str | str | numpy.str_ |
numpy.long | int | numpy.int_ (C long ), numpy.longlong (largest integer type) |
numpy.unicode | str | numpy.unicode_ |
The first column (Deprecated Name) is the aliases that have been deprecated.
The second column (Replaced To) is the in-built python functions that we have to use instead of the deprecated ones.
The Last column (Numpy Scalar Type) lists alternative NumPy names which may occasionally be preferential.
Check this documentation for more information.
Let’s see an example of using numpy.int() and numpy.float() aliases int()
, float()
function :
#int
print(int(4.17)) #output : 4
#float
print(float(4.17)) #output : 4.17
We can also test the numpy.str() alias str()
like this
my_str = str(4.17)
print(my_str) #output : '4.17'
print(type(my_str)) #output: <class 'str'>
Conclusion:
In conclusion, we can say that the “numpy has no attribute int” error can be easily fixed by just using the python in-built aliases like int(). We just have to replace the np.int()
method with int()
in our python program.