错误 TypeError: 类型 numpy.ndarray 没有定义 __round__ 方法

2024-04-27 08:58:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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] 

Tags: ofintestimportnumpyformodelis
3条回答

我在尝试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解释器是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__)。

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 float不同。

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版本的最完整文档。

一、二、二、三

从你上次打印的照片中,我可以将你的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是一个有1列的二维数组。

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

TypeError: type numpy.ndarray doesn't define round method

你试着向努比·恩达雷申请。显然,这是不支持的。

试试这个,使用numpy.round

rounded = [numpy.round(x) for x in predictions]

x是numpy数组。您也可以尝试:

rounded = [round(y) for y in x for x in predictions]

相关问题 更多 >