- 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 the common filling value of two masked arrays in Numpy
To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy. If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None.
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, 76], [73, 88, 51], [62, 45, 67]]) print("Array...
", arr)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Create a masked array 1 and mask some of them as invalid. The fill value is set using the "fill_value" parameter −
maskArr1 = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999) print("
Our Masked Array 1
", maskArr1)
Create a masked array 2 and mask some of them as invalid. The fill value is set using the "fill_value" parameter −
maskArr2 = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999) print("
Our Masked Array 2
", maskArr2)
To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy. If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None −
print("
Result ( common filling value)
", np.ma.common_fill_value(maskArr1, maskArr2))
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, 76], [73, 88, 51], [62, 45, 67]]) print("Array...
", arr) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array 1 and mask some of them as invalid # The fill value is set using the "fill_value" parameter maskArr1 = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999) print("
Our Masked Array 1
", maskArr1) # Create a masked array 2 and mask some of them as invalid # The fill value is set using the "fill_value" parameter maskArr2 = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0], [0, 1, 0], [0, 1, 0]], fill_value = 9999) print("
Our Masked Array 2
", maskArr2) # To return the common filling value of two masked arrays, use the ma.common_fill_value() method in Python Numpy # If maskArray1.fill_value == maskArray2.fill_value, return the fill value, otherwise return None print("
Result ( common filling value)
", np.ma.common_fill_value(maskArr1, maskArr2))
Output
Array... [[65 68 81] [93 33 76] [73 88 51] [62 45 67]] Array Dimensions... 2 Our Masked Array 1 [[-- -- 81] [93 33 76] [73 -- 51] [62 -- 67]] Our Masked Array 2 [[65 68 --] [93 -- 76] [73 -- 51] [62 -- 67]] Result ( common filling value) 9999