`AttributeError: rint` when using numpy.round

后端 未结 3 1279
一生所求
一生所求 2020-12-07 00:10

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 00:50

    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 "", line 1, in 
      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.

提交回复
热议问题