Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

python - `AttributeError: rint` when using numpy.round

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...