

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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...\n", arr) print("\nArray type...\n", arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",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("\nOur Masked Array\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype)
Get the dimensions of the Array −
print("\nOur Masked Array Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Masked Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Masked Array...\n",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("\nResult...\n",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...\n", arr) print("\nArray type...\n", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",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("\nOur Masked Array\n", maskArr) print("\nOur Masked Array type...\n", maskArr.dtype) # Get the dimensions of the Array print("\nOur Masked Array Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Masked Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Masked Array...\n",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("\nResult...\n",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 Questions & Answers
- 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 masked array data as a string containing the raw bytes in the array and fill the invalid entries in Numpy
- Return the data portion of the masked array as a hierarchical Python list and fill the invalid entries
- 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 the absolute value of a masked Array in NumPy
- Return a copy of the string with all occurrences of substring old replaced by new in Numpy
- Return a copy of self, with masked values filled with a given value in Numpy
- Create an array class with possibly masked values and fill in the masked values in Numpy
- Reset the fill value of the masked array to default in Numpy
- Divide masked array elements by a given scalar element and return arrays with Quotient and Remainder in NumPy
- Return a masked array containing the same data but with a new shape in Numpy
- Return a new array of given shape filled with a fill value and a different output type in Numpy