Answer a question

import numpy

......

# Prediction
predictions = model.predict(X_test)
# round predictions
rounded = [round(x) for x in predictions]
print(rounded)

"predictions" is a list of decimals between [0,1] with sigmoid output. 

Why does it always report this error:

  File "/home/abigail/workspace/ml/src/network.py", line 41, in <listcomp>
    rounded = [round(x) for x in predictions]
TypeError: type numpy.ndarray doesn't define __round__ method

If i don't use the 'round', it prints decimals correctly. This "round" should be the Python built-in function. Why does it have anything to do with numpy?

Edited:

for x in predictions:
    print(x, end=' ')

The output is:

    [ 0.79361773] [ 0.10443521] [ 0.90862566] [ 0.10312044] [ 0.80714297] 
[ 0.23282401] [ 0.1730803] [ 0.55674052] [ 0.94095331] [ 0.11699325] 
[ 0.1609294] 

Answers

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
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐