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

前端 未结 5 1226
小蘑菇
小蘑菇 2020-12-31 04:14
import numpy

......

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

\"predictions\"          


        
5条回答
  •  一个人的身影
    2020-12-31 05:02

    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

提交回复
热议问题