close

[Solved] Error “TypeError: type numpy.ndarray doesn’t define __round__ method”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Error “TypeError: type numpy.ndarray doesn’t define round method” in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Error “TypeError: type numpy.ndarray doesn’t define round method” Error Occurs?

Today I get the following error Error “TypeError: type numpy.ndarray doesn’t define round method” in python.

How To Solve Error “TypeError: type numpy.ndarray doesn’t define round method” Error ?

  1. How To Solve Error “TypeError: type numpy.ndarray doesn't define round method” Error ?

    To Solve Error “TypeError: type numpy.ndarray doesn't define round method” Error You tried applying round to numpy.ndarray. Apparently, this isn't supported.

  2. Error “TypeError: type numpy.ndarray doesn't define round method”

    To Solve Error “TypeError: type numpy.ndarray doesn't define round method” Error You tried applying round to numpy.ndarray. Apparently, this isn't supported.

Solution 1


What is model? From what module? It looks like predictions is a 2d array. What is predictions.shape? The error indicates that the x in [x for x in predictions] is an array. It may be a single element array, but it is never the less an array. You could try [x.shape for x in predictions] to see the shape of each element (row) of predictions.

I haven’t had much occasion to use round, but evidently the Python function delegates the action to a .__round__ method (much as + delegates to __add__).

In [932]: round?
Docstring:
round(number[, ndigits]) -> number

Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
Type:      builtin_function_or_method
In [933]: x=12.34
In [934]: x.__round__?
Docstring:
Return the Integral closest to x, rounding half toward even.
When an argument is passed, work like built-in round(x, ndigits).
Type:      builtin_function_or_method
In [935]: y=12
In [936]: y.__round__?
Docstring:
Rounding an Integral returns itself.
Rounding with an ndigits argument also returns an integer.
Type:      builtin_function_or_method

Python integers have a different implementation than python floats.

Python lists and strings don’t have definition for this, so round([1,2,3]) will return an AttributeError: 'list' object has no attribute '__round__'.

Same goes for a ndarray. But numpy has defined a np.round function, and a numpy array has a .round method.

In [942]: np.array([1.23,3,34.34]).round()
Out[942]: array([  1.,   3.,  34.])
In [943]: np.round(np.array([1.23,3,34.34]))
Out[943]: array([  1.,   3.,  34.])

help(np.around) gives the fullest documentation of the numpy version(s).

===================

From your last print I can reconstruct part of your predictions as:

In [955]: arr  = np.array([[ 0.79361773], [ 0.10443521], [ 0.90862566]])
In [956]: arr
Out[956]: 
array([[ 0.79361773],
       [ 0.10443521],
       [ 0.90862566]])
In [957]: for x in arr:
     ...:     print(x, end=' ')
     ...:     
[ 0.79361773] [ 0.10443521] [ 0.90862566] 

arr.shape is (3,1) – a 2d array with 1 column.

np.round works fine, without needing the iteration:

In [958]: np.round(arr)
Out[958]: 
array([[ 1.],
       [ 0.],
       [ 1.]])

the iteration produces your error.

In [959]: [round(x) for x in arr]    
TypeError: type numpy.ndarray doesn't define __round__ method

Solution 2

TypeError: type numpy.ndarray doesn’t define round method

You tried applying round to numpy.ndarray. Apparently, this isn’t supported.

Try this, use numpy.round:

rounded = [numpy.round(x) for x in predictions]

x is numpy array. You can also try this:

rounded = [round(y) for y in x for x in predictions]

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read