错误“类型错误:类型numpy.ndarray没有定义__round__法”
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.
为什么总是报告这个错误:错误“类型错误:类型numpy.ndarray没有定义__round__法”
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
如果我不使用“圆”,它打印小数正确。这个“轮”应该是Python内置函数。为什么它与numpy有什么关系?
编辑:
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]
什么是model
?从什么模块?它看起来像predictions
是一个二维数组。什么是predictions.shape
?该错误表明在[x for x in predictions]
是一个数组。它可能是一个单一的元素数组,但它永远不会是一个数组。您可以尝试[x.shape for x in predictions]
查看每个元素(行)predictions
的形状。
我没有太多机会使用round
,但显然Python函数代表作用的.__round__
方法(多+
委托给__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整数与python浮点数有不同的实现。
Python列表和字符串没有为此定义,所以round([1,2,3])
将返回一个AttributeError: 'list' object has no attribute '__round__'
。
同样为ndarray
。但是numpy
定义了np.round
函数,而numpy数组具有.round
方法。
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)
给出的numpy的版本(S)的最大文档。
===================
从你最后一次打印,我可以重建你的predictions
的一部分:
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
是(3,1)
- 具有1列的2d阵列。
np.round
正常工作,而无需迭代:
In [958]: np.round(arr)
Out[958]:
array([[ 1.],
[ 0.],
[ 1.]])
迭代产生的错误。
In [959]: [round(x) for x in arr]
TypeError: type numpy.ndarray doesn't define __round__ method
让我先阅读。 – user697911
您使用使用Numpy
存储值的功能。它不是一个常规的Python列表,它实际上是一个Numpy
数组。这通常是因为通过机器学习,Numpy
与Python中的普通列表相比,在存储海量数据方面做得更好。您可以参考下面的文档转换为常规列表,然后可以大跳理解:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html
编辑:
会发生什么,如果你尝试:
for x in predictions:
for y in x.:
print(y, end=' ')
'x'是单个数字。 – user697911
请参阅我的补充。 – user697911
@ user697911请看我的评论,因为numpy有一个奇怪的列表清单方式。这实际上应该打开这个值,以便你可以四舍五入。 – rb612
TypeError: type numpy.ndarray doesn't define round method
您试过申请轮到numpy.ndarray。显然,这不被支持。
尝试此,使用numpy.round
:
rounded = [numpy.round(x) for x in predictions]
x是numpy的阵列。您也可以试试这个:
rounded = [round(y) for y in x for x in predictions]
但不是'圆'适用于一个小数'x',这不是一个数组? – user697911
你确定x是单精度小数吗?打印它来证明它。 – gzc
“预测”是一个numpy数组,但'x'是单个值。 – user697911
我在尝试教程Keras时遇到了同样的错误。
起初,我试图
rounded = [numpy.round(x) for x in predictions]
,但它显示的结果是这样的:
[array([1.], dtype=float32), array([0.],dtype=float32), ...]
然后我尝试这样做:
rounded = [float(numpy.round(x)) for x in predictions]
它显示正确的输出。
我认为“numpy.round(x)”返回ndarray的列表,并且包含dtype参数。但输出与数值正确。因此,将列表中的每个元素转换为float类型将显示与教程相同的正确输出。
我的机器是Linux Mint的17.3(ubuntu的14.04)x64和python解释是蟒3.5.2,anaconda3(4.1.1),numpy的1.11.2
你能提供一些解释吗? – thumbtackthief
我认为'predictions'是一个二维数组,可能与形状(11,1)。 – hpaulj