Difference between numpy.rint and numpy.round
Question:
What is the difference between numpy.rint and numpy.round/numpy.around? They both seem to perform the same function:
>>> a
array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2. ])
>>> np.round(a)
array([-2., -2., -0., 0., 2., 2., 2.])
>>> np.rint(a)
array([-2., -2., -0., 0., 2., 2., 2.])
Answers:
This is the difference:
A = np.array([-1.72, -1.58, -0.2, 0.2, 1.5, 1.7, 2.0])
np.round(A,1)
array([-1.7, -1.6, -0.2, 0.2, 1.5, 1.7, 2. ])
np.rint(A)
array([-2., -2., -0., 0., 2., 2., 2.])
You basically use np.round()
when you want to round to any decimal place, like here I have done it for one decimal, so I got -1.7
for -1.72
instead of -2
which I got in np.rint()
One possible reason for having np.rint(x)
when np.round(x,0)
can get our job done is the speed of computation which the former offers us. When I ran both the snippets of code and recorded the timing for the operation this is what I got
%%timeit
np.round(A,0)
5.16 µs ± 495 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
np.rint(A)
1.06 µs ± 28.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
We can very well see that np.rint()
gets the job done approximately five times faster than np.round()
.
What is the difference between numpy.rint and numpy.round/numpy.around? They both seem to perform the same function:
>>> a
array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2. ])
>>> np.round(a)
array([-2., -2., -0., 0., 2., 2., 2.])
>>> np.rint(a)
array([-2., -2., -0., 0., 2., 2., 2.])
This is the difference:
A = np.array([-1.72, -1.58, -0.2, 0.2, 1.5, 1.7, 2.0])
np.round(A,1)
array([-1.7, -1.6, -0.2, 0.2, 1.5, 1.7, 2. ])
np.rint(A)
array([-2., -2., -0., 0., 2., 2., 2.])
You basically use np.round()
when you want to round to any decimal place, like here I have done it for one decimal, so I got -1.7
for -1.72
instead of -2
which I got in np.rint()
One possible reason for having np.rint(x)
when np.round(x,0)
can get our job done is the speed of computation which the former offers us. When I ran both the snippets of code and recorded the timing for the operation this is what I got
%%timeit
np.round(A,0)
5.16 µs ± 495 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%%timeit
np.rint(A)
1.06 µs ± 28.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
We can very well see that np.rint()
gets the job done approximately five times faster than np.round()
.