- 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 input with invalid data masked and replaced by a fill value in Numpy
To return input with invalid data masked and replaced by a fill value, use the numpy.ma.fix_invalid() method in Python Numpy. 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([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) 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, 0], [0, 1, 0], [0, 1, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Array −
print("
Our Masked Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Masked Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Masked Array...
",arr.size)
To return input with invalid data masked and replaced by a fill value, use the numpy.ma.fix_invalid() method in Python Numpy:
print("
Result...
",np.ma.fix_invalid(arr))
Example
import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) 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, 0], [0, 1, 0], [0, 1, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Array print("
Our Masked Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Masked Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Masked Array...
",arr.size) # To return input with invalid data masked and replaced by a fill value, use the numpy.ma.fix_invalid() method in Python Numpy print("
Result...
",np.ma.fix_invalid(arr))
Output
Array... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- 81] [93 33 39] [73 -- 51] [62 -- 67]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 3) Elements in the Masked Array... 12 Result... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]]
- Related Articles
- Return the masked array data as a string containing the raw bytes in the array and fill the invalid entries in Numpy
- Return the default fill value for a masked array with float datatype in Numpy
- Return the default fill value for a masked array with complex datatype in Numpy
- Return the data portion of the masked array as a hierarchical Python list and fill the invalid entries
- Return a copy of self, with masked values filled with a given value in Numpy
- Return the absolute value of a masked Array in NumPy
- Return a masked array containing the same data but with a new shape in Numpy
- Set the fill value of the masked array in Numpy
- Get the fill value of the masked array in Numpy
- Return a new array of given shape and type filled with a fill value in Numpy
- Return a copy of the string with all occurrences of substring old replaced by new in Numpy
- Add a scalar value to each element and return a new masked array in NumPy
- Subtract every element from a scalar value and return a new masked Array in NumPy
- Divide masked array elements by a given scalar element and return arrays with Quotient and Remainder in NumPy
- Return the dot product of two masked arrays and set whether masked data is propagated in Numpy
