- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return each element of the masked array rounded to the nearest given number of decimals in NumPy
To return each element rounded to the given number of decimals, use the ma.MaskedArray.around() method in Numpy. Set the number of decimal places to round using the "decimals" parameter.
The decimals parameter is the number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
The out parameter is an alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary. See Output type determination for more details.
The around() method returns an array of the same type as a, containing the rounded values. Unless out was specified, a new array is created. A reference to the result is returned.
Steps
At first, import the required library −
import numpy as np import numpy.ma as ma
Create an array with int elements using the numpy.array() method −
arr = np.array([[55.50, 85.35, 68.78, 84], [67.96, 33.35, 39.76, 53.20]]) print("Array...
", arr) print("
Array type...
", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...
",arr.ndim)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [0, 1, 0, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Get the shape of the Masked Array −
print("
Our Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("
Elements in the Masked Array...
",maskArr.size)
To return each element rounded to the given number of decimals, use the ma.MaskedArray.around() method in Numpy. Set the number of decimal places to round using the "decimals" parameter −
print("
Result...
", np.around(maskArr, decimals = 1))
Example
import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[55.50, 85.35, 68.78, 84], [67.96, 33.35, 39.76, 53.20]]) print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [0, 1, 0, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",maskArr.size) # To return each element rounded to the given number of decimals, use the ma.MaskedArray.around() method in Numpy. # Set the number of decimal places to round using the "decimals" parameter print("
Result...
", np.around(maskArr, decimals = 1))
Output
Array... [[55.5 85.35 68.78 84. ] [67.96 33.35 39.76 53.2 ]] Array type... float64 Array Dimensions... 2 Our Masked Array [[-- -- 68.78 84.0] [67.96 -- 39.76 53.2]] Our Masked Array type... float64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 4) Elements in the Masked Array... 8 Result... [[-- -- 68.8 84.0] [68.0 -- 39.8 53.2]]
- Related Articles
- Return each element of the masked array rounded in Numpy
- Return the variance of the masked array elements along given axis in Numpy
- Return the transpose of the masked array in NumPy
- Return the length of the masked array in Numpy
- Return range of values from the masked array for each row in Numpy
- Return a copy of the masked array in NumPy
- Return the mask of a masked array in Numpy
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Return the standard deviation of the masked array elements along given axis in NumPy
- Return the average of the masked array elements in Numpy
- Return the variance of the masked array elements in Numpy
- Raise each and every element of a masked array to a given scalar value in NumPy
- Raise a given scalar value to each and every element of a masked array in NumPy
- Add a scalar value to each element and return a new masked array in NumPy
- Return the absolute value of a masked Array in NumPy
