numpy.rint() in Python (original) (raw)
Last Updated : 08 Mar, 2024
The numpy.rint() is a mathematical function that rounds elements of the array to the nearest integer.
Syntax : numpy.rint(x[, out]) = ufunc ‘rint’)
Parameters :
array : [array_like] Input array.Return : An array with all array elements being rounded off, having same type and shape as input.
Code #1 : Working
import
numpy as np
in_array
=
[.
5
,
1.5
,
2.5
,
3.5
,
4.5
,
10.1
]
print
(
"Input array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values : \n"
, rintoff_values)
in_array
=
[.
53
,
1.54
, .
71
]
print
(
"\nInput array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values : \n"
, rintoff_values)
in_array
=
[.
5538
,
1.33354
, .
71445
]
print
(
"\nInput array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values : \n"
, rintoff_values)
Output:
Input array : [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]
Rounded values : [ 0. 2. 2. 4. 4. 10.]
Input array : [0.53, 1.54, 0.71]
Rounded values : [ 1. 2. 1.]
Input array : [0.5538, 1.33354, 0.71445]
Rounded values : [ 1. 1. 1.]
Code #2 : Working
import
numpy as np
in_array
=
[
1
,
4
,
7
,
9
,
12
]
print
(
"Input array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values : \n"
, rintoff_values)
in_array
=
[
133
,
344
,
437
,
449
,
12
]
print
(
"\nInput array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values upto 2: \n"
, rintoff_values)
in_array
=
[
133
,
344
,
437
,
449
,
12
]
print
(
"\nInput array : \n"
, in_array)
rintoff_values
=
np.rint(in_array)
print
(
"\nRounded values upto 3: \n"
, rintoff_values)
Output:
Input array : [1, 4, 7, 9, 12]
Rounded values : [ 1. 4. 7. 9. 12.]
Input array : [133, 344, 437, 449, 12]
Rounded values upto 2: [ 133. 344. 437. 449. 12.]
Input array : [133, 344, 437, 449, 12]
Rounded values upto 3: [ 133. 344. 437. 449. 12.]
Similar Reads
- numpy.rate() in Python numpy_financial.pmt() function in Python is part of the numpy-financial library and is used for calculating the payment amount required for a loan or an investment, assuming that payments are constant and the interest rate remains unchanged throughout the term. This function is particularly useful f 5 min read
- numpy.trunc() in Python The numpy.trunc() is a mathematical function that returns the truncated value of the elements of array. The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. Syntax : numpy.tr 2 min read
- numpy.remainder() in Python numpy.remainder() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.remainder(arr1, arr2, /, out=No 2 min read
- numpy.reciprocal() in Python The numpy.reciprocal() is a mathematical function that is used to calculate reciprocal of all the elements in the input array. Syntax :numpy.reciprocal(x, /, out=None, *, where=True) Parameters : x[array_like]: Input array or object whose elements needed to test. out [ndarray, optional]: A location 2 min read
- numpy.trim_zeros() in Python numpy.trim_zeros function is used to trim the leading and/or trailing zeros from a 1-D array or sequence. Syntax: numpy.trim_zeros(arr, trim) Parameters: arr : 1-D array or sequence trim : trim is an optional parameter with default value to be 'fb'(front and back) we can either select 'f'(front) and 2 min read
- numpy.irr() in Python numpy.irr(values) : This financial function helps user to compute IRR Value i.e. Internal Rate of Return ie. “average†periodically compounded rate of return. Parameters : values : [array-like] Input cash flows per time period. net “deposits†are negative and net “withdrawals†are positiveReturn : I 1 min read
- numpy.mod() in Python numpy.mod() is another function for doing mathematical operations in numpy.It returns element-wise remainder of division between two array arr1 and arr2 i.e. arr1 % arr2 .It returns 0 when arr2 is 0 and both arr1 and arr2 are (arrays of) integers. Syntax : numpy.mod(arr1, arr2, /, out=None, *, where 2 min read
- numpy.base_repr() in Python numpy.base_repr(number, base=2, padding=0) function is used to return a string representation of a number in the given base system. For example, decimal number 10 is represented as 1010 in binary whereas it is represented as 12 in octal. Syntax : numpy.base_repr(number, base=2, padding=0) Parameters 3 min read
- Python NumPy Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. Besides its obvious scientific uses, Numpy can also be used as an efficient 6 min read
- numpy.alen() in Python numpy.alen() function is used to return the length of the first dimension of the input array. Syntax : numpy.alen(arr) Parameters : arr : [array_like] Input array. Return : [int]Length of the first dimension of arr. Code #1 : # Python program explaining # alen() function import numpy as geek # input 1 min read