Python `AttributeError: rint` 使用 numpy.round 时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19387608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:37:54  来源:igfitidea点击:

`AttributeError: rint` when using numpy.round

pythonarraysnumpy

提问by Abhishek Thakur

I have a numpy array that looks like this:

我有一个看起来像这样的 numpy 数组:

[[41.743617 -87.626839]
 [41.936943 -87.669838]
 [41.962665 -87.65571899999999]]

I want to round the numbers in the array to two decimal places, or three. I tried using numpy.around and numpy.round, but both of them give me the following error:

我想将数组中的数字四舍五入到两位小数或三位。我尝试使用 numpy.around 和 numpy.round,但它们都给了我以下错误:

  File "/Library/Python/2.7/site-packages/numpy-1.8.0.dev_3084618_20130514-py2.7-macosx-10.8-intel.egg/numpy/core/fromnumeric.py", line 2452, in round_
    return round(decimals, out)
AttributeError: rint

i used numpy.around(x, decimals = 2)and numpy.round(x,decimals=2)

我用过numpy.around(x, decimals = 2)numpy.round(x,decimals=2)

Am I doing something wrong? Is there any other way to do this efficiently for a large array?

难道我做错了什么?对于大型阵列,还有其他方法可以有效地做到这一点吗?

采纳答案by Daniel

You cannot round numpy arrays that are objects, this can be changed with astypeas long as your array can be safely converted to floats:

您不能对作为对象的 numpy 数组进行舍入astype,只要您的数组可以安全地转换为浮点数,就可以更改它:

>>> a = np.random.rand(5).astype(np.object)
>>> a
array([0.5137250555772075, 0.4279757819721647, 0.4177118178603122,
       0.6270676923544128, 0.43733218329094947], dtype=object)

>>> np.around(a,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2384, in around
    return round(decimals, out)
AttributeError: rint

>>> np.around(a.astype(np.double),3)
array([ 0.514,  0.428,  0.418,  0.627,  0.437])

You will receive similar errors with string, unicode, void, and char type arrays.

对于字符串、unicode、void 和 char 类型数组,您将收到类似的错误。

回答by Sam Tubb

You could do something like this:

你可以这样做:

numbers=[22.2,99.123,1213.1230]
newnumbers=[]
for n in numbers:
    newnumbers.append(round(n))
#for comparison
print numbers
print newnumbers 

回答by Claudiu

numpy.aroundshould work on a list of lists:

numpy.around应该处理列表列表:

>>> import numpy as np
>>> arr = [[41.743617, -87.626839],
           [41.936943, -87.669838],
           [41.962665, -87.65571899999999]]
>>>
>>> np.around(arr, decimals=2)
array([[ 41.74, -87.63],
       [ 41.94, -87.67],
       [ 41.96, -87.66]])
>>> np.round(arr, decimals=2)
array([[ 41.74, -87.63],
       [ 41.94, -87.67],
       [ 41.96, -87.66]])

However, note that it doesn't work on python longs. In fact it gives the same error you reported:

但是,请注意它不适用于 python longs。实际上,它给出了您报告的相同错误:

>>> np.round(3892438942893489234899848939)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/csaftoiu/work/venv/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2401, in round_
    return _wrapit(a, 'round', decimals, out)
  File "/Users/csaftoiu/work/venv/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 38, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
AttributeError: rint

What seems to be happening is that numpy can't convert some of the numbers in your python list to one of its data types. If it's a long then it's not a problem because it's already rounded, but you'll have to work around it.

似乎正在发生的事情是 numpy 无法将 Python 列表中的某些数字转换为其数据类型之一。如果它很长,那么这不是问题,因为它已经四舍五入了,但是您必须解决它。