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

Abh*_*kur 25 python arrays numpy

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

[[41.743617 -87.626839]
 [41.936943 -87.669838]
 [41.962665 -87.65571899999999]]
Run Code Online (Sandbox Code Playgroud)

我想将数组中的数字四舍五入到两位小数,或三位.我尝试使用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
Run Code Online (Sandbox Code Playgroud)

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

难道我做错了什么?有没有其他方法可以有效地为大型阵列做到这一点?

Dan*_*iel 35

您不能将作为对象的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])
Run Code Online (Sandbox Code Playgroud)

您将收到与string,unicode,void和char类型数组类似的错误.