问题描述
我有一个看起来像这样的 numpy 数组:
[[41.743617 -87.626839][41.936943 -87.669838][41.962665 -87.65571899999999]]
我想将数组中的数字四舍五入到两位小数或三位.我尝试使用 numpy.around 和 numpy.round,但它们都给我以下错误:
文件/Library/Python/2.7/site-packages/numpy-1.8.0.dev_3084618_20130514-py2.7-macosx-10.8-intel.egg/numpy/core/fromnumeric.py",第2452行, 在圆形_返回轮(小数,出)属性错误:rint
我使用了 numpy.around(x,decimals = 2)
和 numpy.round(x,decimals=2)
我做错了吗?对于大型阵列,还有其他方法可以有效地做到这一点吗?
你不能对作为对象的 numpy 数组进行舍入,这可以通过 astype
改变,只要你的数组可以安全地转换为浮点数:
对于字符串、unicode、void 和 char 类型的数组,您会收到类似的错误.
I have a numpy array that looks like this:
[[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:
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)
Am I doing something wrong? Is there any other way to do this efficiently for a large array?
You cannot round numpy arrays that are objects, this can be changed with astype
as long as your array can be safely converted to floats:
>>> 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.
这篇关于`AttributeError: rint` 使用 numpy.round 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!