TypeError: type numpy.ndarray doesn't define __round__ method

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# TypeError: type numpy.ndarray doesn't define __round__ method

The NumPy "TypeError: type numpy.ndarray doesn't define __round__ method" occurs when you try to pass a NumPy array to the native Python round() function.

To solve the error, pass the NumPy array to the numpy.round() method instead.

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([1.3, 2.6, 3.4, 4.7, 5.9]) rounded_arr = round(arr) # โ›”๏ธ TypeError: type numpy.ndarray doesn't define __round__ method print(rounded_arr)

type error type numpy ndarray doesnt define round method

The issue in the code sample is that the native Python round() doesn't handle NumPy arrays.

# Use the numpy.round() method to solve the error

You can solve the error by passing the NumPy array to the numpy.round() method.

main.py
import numpy as np arr = np.array([1.3, 2.6, 3.4, 4.7, 5.9]) rounded_arr = np.round(arr) # ๐Ÿ‘‡๏ธ [1. 3. 3. 5. 6.] print(rounded_arr)

solve error by using numpy round method

The code for this article is available on GitHub

The numpy.round() method takes a NumPy array as a parameter and evenly rounds to the given number of decimals.

If the number of decimal places is not supplied, it is assumed to be 0.

# Rounding the NumPy array to N decimal places

Here is an example that rounds the elements of the NumPy array to 2 decimal places.

main.py
import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.round(arr, 2) # ๐Ÿ‘‡๏ธ [1.35 2.68 3.46 4.71 5.95] print(rounded_arr)

round elements of numpy array to n decimal places

The code for this article is available on GitHub

# Rounding a two-dimensional NumPy array with numpy.round()

You can also use the numpy.round() method to round a two-dimensional NumPy array.

main.py
import numpy as np arr = np.array([ [1.345, 2.678], [3.456, 4.713], [5.945, 6.496] ]) rounded_arr = np.round(arr) # [[1. 3.] # [3. 5.] # [6. 6.]] print(rounded_arr)

rounding two dimensional numpy array

The code for this article is available on GitHub

If you get an error when using the code sample above, try using a list comprehension.

main.py
import numpy as np arr = np.array([ [1.345, 2.678], [3.456, 4.713], [5.945, 6.496] ]) rounded_arr = [np.round(x) for x in arr] # [array([1., 3.]), array([3., 5.]), array([6., 6.])] print(rounded_arr)

try using list comprehension

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we pass the current NumPy array to the numpy.round() method and return the result.

# There is also a numpy.around() method

There is also a numpy.around() method that is used to round a NumPy array to the given number of decimals.

The numpy.around method is an alias of numpy.round so it does the same.

main.py
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)

numpy around is same as numpy round

The code for this article is available on GitHub

You can also pass your preferred number of decimal places as the second argument to around() (it defaults to 0).

main.py
import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.around(arr, 2) # [1.35 2.68 3.46 4.71 5.95] print(rounded_arr)

pass preferred number of decimals to around

# If you only want to round up, use numpy.ceil

If only want to round the number in an array up, use the numpy.ceil() method.

main.py
import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.ceil(arr) # ๐Ÿ‘‡๏ธ [2. 3. 4. 5. 6.] print(rounded_arr)

rounding up with numpy ceil

The code for this article is available on GitHub

The numpy.ceil() method returns the ceiling of the supplied array, element-wise.

The ceil of the scalar x is the smallest integer i, such that i >= x.

# If you only want to round down, use numpy.floor

If you only want to round the numbers in an array down, use the numpy.floor() method.

main.py
import numpy as np arr = np.array([1.3456, 2.6789, 3.4567, 4.7123, 5.9456]) rounded_arr = np.floor(arr) # ๐Ÿ‘‡๏ธ [1. 2. 3. 4. 5.] print(rounded_arr)

only rounding down with numpy floor

The numpy.floor() method returns the floor of the supplied array, element-wise.

The floor of the number x is the largest integer i, such that i <= x.

I've also written an article on how to round a float to N decimal places in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev