什么是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
我认为'predictions'是一个二维数组,可能与形状(11,1)。 – hpaulj