错误“TypeError:type numpy.ndarray没有定义__round__方法”

时间:2016-12-25 07:07:36

标签: python numpy

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',它会正确打印小数。这个“圆”应该是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] 

5 个答案:

答案 0 :(得分:6)

什么是model?从什么模块?看起来predictions是一个二维数组。什么是predictions.shape?该错误表示x中的[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版本的最完整文档。

===================

从上次打印开始,我可以将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列的二维数组。

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

答案 1 :(得分:5)

  

TypeError:类型numpy.ndarray未定义 round 方法

你试过将圆形应用于numpy.ndarray。显然,这不受支持。

试试这个,使用 String uri = "@drawable/menu_howtouse"; int imageResource = this.context.getResources().getIdentifier(uri, null, this.PACKAGE_NAME); Drawable res = this.context.getResources().getDrawable(imageResource); holder.iconImageView.setImageDrawable(res);

numpy.round

x是numpy数组。你也可以试试这个:

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

答案 2 :(得分: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

答案 3 :(得分:1)

您正在使用使用Numpy存储值的函数。它不是常规的Python列表,而是一个Numpy数组。这通常是因为通过机器学习,与Python中的普通列表相比,Numpy在存储大量数据方面做得更好。您可以参考以下文档转换为常规列表,然后您可以执行理解:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html

编辑:

如果你尝试会发生什么:

for x in predictions:
   for y in x.:
    print(y, end=' ')

答案 4 :(得分:0)

这也让我发疯。我已经存储了对类型为A <- test.df %>% filter(type == "A") B <- test.df %>% filter(type == "B") C <- test.df %>% filter(type == "C") D <- test.df %>% filter(type == "D") ggplot(A) + geom_raster(aes(x =day, y = comp, fill = value)) + scale_fill_gradientn(colours = rainbow(10)) + coord_equal() + ylab("A") ggplot(B) + geom_raster(aes(x =day, y = comp, fill = value)) + scale_fill_gradientn(colours = rainbow(10)) + coord_equal() + ylab("B") ggplot(C) + geom_raster(aes(x =day, y = comp, fill = value)) + scale_fill_gradientn(colours = rainbow(10)) + coord_equal() + ylab("C") ggplot(D) + geom_raster(aes(x =day, y = comp, fill = value)) + scale_fill_gradientn(colours = rainbow(10)) + coord_equal() + ylab("D") 的scipy函数的引用。这将返回包含单个浮点数的类型<class 'scipy.interpolate.interpolate.interp1d'>的单个值。我以为这实际上是一个浮点数,并且会通过我的库代码传播回去,直到<class 'numpy.ndarray'>产生与上述相同的错误。

这是调试调用堆栈以检查每个函数返回后传递的实际类型的情况。然后,我沿round的行转换了原始函数调用的返回值。然后我的代码按我期望的方式运行。