import numpy
......
# Prediction
predictions = model.predict(X_test)
# round predictions
rounded = [round(x) for x in predictions]
print(rounded)
\"predictions\"
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]
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
You're using a function that uses Numpy
to store values. Instead of being a regular Python list, it is actually a Numpy
array. This is generally because with machine learning, Numpy
does a much better job at storing massive amounts of data compared to an ordinary list in Python. You can refer to the following documentation to convert to a regular list which you can then preform a comprehension:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html
Edit:
What happens if you try:
for x in predictions:
for y in x.:
print(y, end=' ')
This was driving me nuts too. I had stored a reference to a scipy function with type <class 'scipy.interpolate.interpolate.interp1d'>
. This was returning a single value of type <class 'numpy.ndarray'>
containing a single float. I had assumed this was actually a float and it propagated back up through my library code until round
produced the same error described above.
It was a case of debugging the call stack to check what actual type was being passed on after each function return. I then cast the return value from my original function call along the lines of result = float(interp1d_reference(x))
. Then my code behaved as I had expected/wanted.
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