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.
为什么总是报告此错误:
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
如果我不使用“round”,它会正确地打印小数。这个“round”应该是Python内置函数。为什么和努比有关?
编辑:
for x in predictions:
print(x, end=' ')
输出为:
[ 0.79361773] [ 0.10443521] [ 0.90862566] [ 0.10312044] [ 0.80714297]
[ 0.23282401] [ 0.1730803] [ 0.55674052] [ 0.94095331] [ 0.11699325]
[ 0.1609294]
我在尝试Keras教程时遇到了同样的错误。
起初,我试过
但结果是这样的:
然后我试着说:
它显示了正确的输出。
我认为“numpy.round(x)”返回ndarray的列表,并包含dtype参数。但是输出值是正确的。因此,将列表中的每个元素转换为float类型将显示与教程相同的正确输出。
我的机器是Linux Mint 17.3(ubuntu 14.04)x64,python解释器是python 3.5.2,anaconda3(4.1.1),numpy 1.11.2
什么是
model
?从哪个模块?看起来predictions
是一个二维数组。什么是predictions.shape
?错误表明[x for x in predictions]
中的x
是一个数组。它可能是一个单元素数组,但它永远都是一个数组。您可以尝试[x.shape for x in predictions]
查看predictions
的每个元素(行)的形状。我没有太多的机会使用
round
,但是很明显Python函数将操作委托给了.__round__
方法(相当于+
将操作委托给了__add__
)。Python整数的实现与Python float不同。
Python列表和字符串对此没有定义,因此
round([1,2,3])
将返回AttributeError: 'list' object has no attribute '__round__'
。对于
ndarray
来说也是如此。但是numpy
定义了np.round
函数,numpy数组有一个.round
方法。help(np.around)
提供了numpy版本的最完整文档。一、二、二、三
从你上次打印的照片中,我可以将你的
predictions
的一部分重建为:arr.shape
是一个有1列的二维数组。np.round
工作正常,无需迭代:迭代会产生错误。
你试着向努比·恩达雷申请。显然,这是不支持的。
试试这个,使用
numpy.round
:x是numpy数组。您也可以尝试:
相关问题 更多 >
编程相关推荐