Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`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?

like image 330
Abhishek Thakur Avatar asked Oct 15 '13 17:10

Abhishek Thakur


People also ask

How do you round all values in a NumPy array?

To round elements of the array to the nearest integer, use the numpy. rint() method in Python Numpy. For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value. The out is a location into which the result is stored.

What does NP RINT do?

The numpy. rint() is a mathematical function that rounds elements of the array to the nearest integer. Parameters : array : [array_like] Input array.


1 Answers

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.

like image 76
Daniel Avatar answered Sep 20 '22 16:09

Daniel