The typeerror: type numpy.ndarray doesn’t define round method is an error that occurs in the numpy library. Scroll down to know more!
About the error
The round function of Python doesn’t work with NumPy arrays. It can only handle numerical data types like integers, floats, and booleans. The array is considered another data structure. The following code shows how we may get this error by using the built-in function only.
import numpy as np
arr = np.array([1.5, 2.5, 3.5])
# Attempting to use the built-in round() function on a NumPy array
rounded = round(arr)
# This will raise the error "typeerror: type numpy.ndarray doesn't define __round__ method"
Methods of resolving the error
You can either work with the round() or around() functions of numpy to prevent the typeerror: type numpy.ndarray doesn’t define __round__ method error.
round() function of numpy
Instead of using round() directly, use numpy.round(). This function has been created for numpy arrays only. Thus, this is the most preferred method.
import numpy as np
arr = np.array([1.8, 2.2, 3.9])
# This will cause an error
#rounded = round(arr)
# This will work correctly
rounded = np.round(arr)
print(rounded)
Thus, you’ll get the output as:
[2. 2. 4.]
around() function of numpy
This function works similarly to the round() function of numpy. Rather than using round() straightaway, you can opt for the numpy.around() function as well. This will definitely fix the typeerror: type numpy.ndarray doesn’t define round method error.
import numpy as np
arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456])
rounded_arr = np.around(arr)
# [1. 3. 3. 5. 6.]
print(rounded_arr)
You can specify the number of decimal places too like np.around(arr, 3) will round off to 3 decimal places. So, in this case, you’ll get the output as:
[1. 3. 3. 5. 6.]
ceil method of numpy
If you want to round off the number to a higher value, use the Ceil method directly rather than using the round() function.
import numpy as np
# Create a NumPy array
arr = np.array([1.5, 2.5, 3.5])
# Round the elements of the array up to the nearest integer
rounded = np.ceil(arr)
# Print the rounded array
print(rounded)
In this case, you’ll get the output as [2,3,4] because this function will provide the next higher whole number value.
floor method of numpy
The floor() function will round off array numbers to a lesser value contrary to the ceil function of numpy. You can round your numpy array values with the help of this function of numpy to prevent the typeerror: type numpy.ndarray doesn’t define round method error.
import numpy as np
# Create a NumPy array
arr = np.array([1.5, 2.5, 3.5])
# Round the elements of the array down to the nearest integer
rounded = np.floor(arr)
# Print the rounded array
print(rounded)
#[1. 2. 3.]
trunc() method of numpy
This numpy method will truncate or remove the decimal part of your number and fetch the whole number part. It will provide the output as the nearest number to zero after separating the decimal part. You can use this function as well as an alternative for the round() function.
import numpy as np
# Create a NumPy array
arr = np.array([1.5, 2.5, 3.5])
# Truncate the elements of the array to the nearest integer
truncated_arr = np.trunc(arr)
# Print the truncated array
print(truncated_arr)
round() on a NumPy array in list format
Even if you change the Numpy array to list data type, you’ll still get the typeerror: type numpy.ndarray doesn’t define __round__ method error. This is because the round() function is not meant for this type of array. A numpy.round() function will only work for this numpy array.
Consider the example given below. This function uses the round() function on the given numpy array. The Python interpreter will flag this as an error, and you’ll obtain “TypeError: cannot round NumPy array” as the output of this code.
arr = np.array([1.5, 2.5, 3.5])
try:
rounded = round(arr)
except TypeError as e:
print(e)
The correct syntax
The apt way to work with the round function in Python numpy array is to use the round() function of numpy only. The given code specifies two different ways in which you can round the elements of your numpy array.
import numpy as np
# Round an array to two decimal places
arr = np.array([1.523, 2.456, 3.789])
rounded = np.round(arr, 2)
print(rounded)
# Round an array to the nearest integer
arr = np.array([1.5, 2.5, 3.5])
rounded = np.round(arr)
print(rounded)
Other tips while working with decimals in numpy
You may follow this list of tips. You can use these while rounding off the array components. These will also let you get rid of the typeerror: type numpy.ndarray doesn’t define round method error.
- You can specify decimal parameters, also. It means that you can suggest how many decimal places you want. The given example has a precision of up to 2 decimals.
np.round(arr, 2)
- If you apply the round() function on a float value, you’ll get float output only.
- This function also contains the out parameter, which tells where your output will be stored. The following example explains the same.
import numpy as np
arr = np.array([1.523, 2.456, 3.789])
out_arr = np.zeros_like(arr)
rounded = np.round(arr, 2, out=out_arr)
print(rounded)
FAQs
round()
and numpy.round()
? The round() function is used for numerical data. On the other hand, numpy.round() is specifically used for a Numpy array. This function will prevent the typeerror: type numpy.ndarray doesn’t define round method error.
You can work with numpy.trunc(), numpy.round(), numpy.floor(), numpy.around() and numpy.ceil() for Numpy arrays.
Conclusion
This article draws attention to the typeerror: type numpy.ndarray doesn’t define __round__ method error. It discusses ways through which we can fix the given error.