Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Mask using floating point equality in Numpy
To mask using floating point equality, use the numpy.ma.masked_values() method in Python Numpy. Return a MaskedArray, masked where the data in array x are approximately equal to value, determined using isclose. The default tolerances for masked_values are the same as those for isclose.
A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.
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([[71, 55, 82.8], [82.8, 33, 39], [73, 82.3, 51], [90, 77, 82.8]])
print("Array...
", arr)
Get the type pf array −
print("\nArray type...
", arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...
",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("\nNumber of Elements in the Array...
",arr.size)
To mask using floating point equality, use the numpy.ma.masked_values() method in Python Numpy −
print("\nResult...
",np.ma.masked_values(arr, 82.8))
Example
import numpy as np
import numpy.ma as ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[71, 55, 82.8], [82.8, 33, 39], [73, 82.3, 51], [90, 77, 82.8]])
print("Array...
", arr)
# Get the type pf array
print("\nArray type...
", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...
",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("\nNumber of Elements in the Array...
",arr.size)
# To mask using floating point equality, use the numpy.ma.masked_values() method in Python Numpy
print("\nResult...
",np.ma.masked_values(arr, 82.8))
Output
Array... [[71. 55. 82.8] [82.8 33. 39. ] [73. 82.3 51. ] [90. 77. 82.8]] Array type... float64 Array Dimensions... 2 Our Array Shape... (4, 3) Number of Elements in the Array... 12 Result... [[71.0 55.0 --] [-- 33.0 39.0] [73.0 82.3 51.0] [90.0 77.0 --]]
