Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between numpy.round and numpy.around

So, I was searching for ways to round off all the numbers in a numpy array. I found 2 similar functions, numpy.round and numpy.around. Both take seemingly same arguments for a beginner like me.

So what is the difference between these two in terms of:

  • General difference
  • Speed
  • Accuracy
  • Being used in practice
like image 477
DuttaA Avatar asked Mar 12 '18 06:03

DuttaA


People also ask

What does Numpy round do?

The numpy. round_() is a mathematical function that rounds an array to the given number of decimals.

How do you round 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 is a correct method to round decimals in Numpy?

Ceil. The ceil() function rounds off decimal to nearest upper integer.

What is the difference between Numpy and array?

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension. The Python core library provided Lists.


2 Answers

They are the exact same function:

def round_(a, decimals=0, out=None):
    """
    Round an array to the given number of decimals.
    Refer to `around` for full documentation.
    See Also
    --------
    around : equivalent function
    """
    return around(a, decimals=decimals, out=out)
like image 160
Blender Avatar answered Oct 12 '22 20:10

Blender


The main difference is that round is a ufunc of the ndarray class, while np.around is a module-level function.

Functionally, both of them are equivalent as they do the same thing - evenly round floats to the nearest integer. ndarray.round calls around from within its source code.

like image 44
cs95 Avatar answered Oct 12 '22 20:10

cs95