• 14
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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

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.

  • 45
Reply Report
      • 1
    • @SethMMorton There is clearly a reason that the array is cast as object instead of float. I would be hesitant to suggesting changing overall dtypes without understanding more of the OP's code.
      • 1
    • I'm not sure there was clearly a reason... it could have been a mistake. We don't know how the array was created. If there was a reason, I agree with you. I was just suggesting this in case the OP didn't know.
    • @SethMMorton That is a fair enough point, I may be a bit overcautious when it comes to disclaimers attached to statements on SO.

You could do something like this:

numbers=[22.2,99.123,1213.1230]
newnumbers=[]
for n in numbers:
    newnumbers.append(round(n))
#for comparison
print numbers
print newnumbers 
  • 0
Reply Report
      • 2
    • i have a 2-D array with around 500k rows and I want to round to a specific number of decimal places, not completely round them all

numpy.around should work on a list of lists:

>>> import numpy as np
>>> arr = [[41.743617, -87.626839],
           [41.936943, -87.669838],
           [41.962665, -87.65571899999999]]
>>>
>>> np.around(arr, decimals=2)
array([[ 41.74, -87.63],
       [ 41.94, -87.67],
       [ 41.96, -87.66]])
>>> np.round(arr, decimals=2)
array([[ 41.74, -87.63],
       [ 41.94, -87.67],
       [ 41.96, -87.66]])

However, note that it doesn't work on python longs. In fact it gives the same error you reported:

>>> np.round(3892438942893489234899848939)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/csaftoiu/work/venv/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2401, in round_
    return _wrapit(a, 'round', decimals, out)
  File "/Users/csaftoiu/work/venv/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 38, in _wrapit
    result = getattr(asarray(obj),method)(*args, **kwds)
AttributeError: rint

What seems to be happening is that numpy can't convert some of the numbers in your python list to one of its data types. If it's a long then it's not a problem because it's already rounded, but you'll have to work around it.

  • 0
Reply Report

Trending Tags