本文介绍了`AttributeError的:rint`使用numpy.round时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a numpy array that looks like this:

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

我要在阵列中四舍五入的数字到小数点后两位,或三个。我试着用numpy.around和numpy.round,但他们两人给我以下错误:

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

我用 numpy.around(X,小数= 2)
numpy.round(X,小数= 2)

我是不是做错了什么?是否有任何其他的方式来将一个大阵有效地做到这一点?

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

推荐答案

您不能圆numpy的数组,其对象,这可以用 astype 改变,只要您的数组可以安全地转换为浮点数:

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])

您会收到字符串类似的错误,UNI code,无效的,和char类型数组。

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

这篇关于`AttributeError的:rint`使用numpy.round时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:01