Attributeerror module numpy has no attribute int [SOLVED]

In this article, we will look at the solutions for the Attributeerror module numpy has no attribute int error. Also, we will know the causes of this error.

What is Attributeerror module numpy has no attribute int?

The error AttributeError module ‘numpy’ has no attribute ‘int’ occurs when we use the aliases of built-in types such as np.int and np.float is deprecated.

To fix the error, we should use the native classes of Python int and float instead of Numpy aliases.

In the latest version of NumPy, the following attributes have been deprecated:

  • np.object
  • np.bool
  • np.float
  • np.complex
  • np.str
  • np.int

Hence, every time we used these attributes we got the attribute error.

Let’s see how this error occurs. Take a look at the following code:

import numpy as np

# AttributeError: module 'numpy' has no attribute 'int'.
num = np.int(5.14)

This will output the error since we used the numpy.int class rather than the built-in int() class of python.

Moving on are the solutions to fix the error…

How to fix the Attributeerror module numpy has no attribute int

Here are the following solutions you can consider to fix Attributeerror module numpy has no attribute int.

Use the native Python classes instead

To fix this, you need to replace the attributes above with Python built-in types or NumPy scalar types.

The following table shows you how to handle the deprecated types:

DeprecatedPython typesNumPy scalar types for precision
numpy.boolboolnumpy.bool_
numpy.intintnumpy.int_ (default), numpy.int64, or numpy.int32
numpy.floatfloatnumpy.float64numpy.float_numpy.double (equivalent)
numpy.complexcomplexnumpy.complex128numpy.complex_numpy.cdouble (equivalent)
numpy.objectobjectnumpy.object_
numpy.strstrnumpy.str_
numpy.longintnumpy.int_ (C long), numpy.longlong (largest integer type)
numpy.unicodestrnumpy.unicode_

So let’s get started from int() classes.

The int class returns an integer object constructed from the provided number or string argument.

For instance, the following code is presented:

num = int(5.10) # result 5
print(num)

num = int('5') # result 5
print(num)

Output:

5
5

When it comes to float as shown in the table, we’re going to use the native float() class to convert a value to a floating-point number.

num = float('5.14')

print(num)  # 5.14

print(type(num)) # <class 'float'>

The float() class returns a floating-point number constructed from the provided number or string.

Output:

5.14

Along with string, we will str class to convert a value to a string.

my_str = str(1234)

print(my_str)  # '1234'

print(type(my_str))  # <class 'str'>

Output:

1234

Downgrade NumPy version

Another way to fix the error is to downgrade Numpy version to the latest version prior to 1.24.

Since the aliases were depracated in version 1.24, therefore if we downgrade to 1.23.x we can still use them.

Take note: This is not recommended to use since in the latest version there are a lot of new features we can utilize.

However, in line with this error use the following command to downgrade Numpy.

pip install "numpy<1.24.0"

#or with pip3
pip install "numpy<1.24.0"

After downgrading to version 1.23.X of your NumPy, you can use the NumPy aliases.

Conclusion

The AttributeError: module ‘numpy’ has no attribute ‘int’ error can be frustrating to deal with, especially if you are new to Python development.

However, understanding the causes of the error and following the above steps can help you fix the issue quickly and efficiently.

We hope that this article has provided you with the information you need to fix this error and continue working with Python.

If you are finding solutions to some errors you’re encountering we also have AttributeError: ‘NoneType’ object has no attribute ‘format’.

Leave a Comment