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

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]

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

I encountered the same error when I was trying the tutorial of Keras.

At first, I tried

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

but it showed the result like this:

[array([1.], dtype=float32), array([0.],dtype=float32), ...]

then I tried this:

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

it showed the right outputs.

I think the "numpy.round(x)" returns list of ndarray, and contains the dtype parameter. but the outputs are correct with the value. So converting each element of the list to float type will show the right outputs as same as the tutorial.

My machine is Linux Mint 17.3(ubuntu 14.04) x64, and python interpreter is python 3.5.2, anaconda3(4.1.1), numpy 1.11.2

Tags:

Python

Numpy