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
Convert the input to a masked array of the given data-type in Numpy
To convert the input to a masked array of the given data-type, use the numpy.ma.asarray() method in Python Numpy. No copy is performed if the input is already an ndarray. If the input data is a subclass of MaskedArray, a base class MaskedArray is returned.
The first parameter is the input data, in any form that can be converted to a masked array. The functions returns the Masked array interpretation of the first parameter. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists, ndarrays and masked arrays. The order parameter suggests whether to use row-major ('C') or column-major ('FORTRAN') memory representation. Default is 'C'.
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("\nArray type...
", arr.dtype)
Get the dimensions of the Array −
print("\nArray 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("\nOur Masked Array
", maskArr)
print("\nOur Masked Array type...
", maskArr.dtype)
Get the dimensions of the Array 7minus;
print("\nOur Masked Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("\nOur Masked Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Masked Array...
",arr.size)
To convert the input to a masked array of the given data-type, use the numpy.ma.asarray() method −
print("\nMasked Array...
",np.ma.asarray(arr))
Check the type −
print("\nType...
",type(np.ma.asarray(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("\nArray type...
", arr.dtype)
# Get the dimensions of the Array
print("\nArray 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("\nOur Masked Array
", maskArr)
print("\nOur Masked Array type...
", maskArr.dtype)
# Get the dimensions of the Array
print("\nOur Masked Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("\nOur Masked Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("\nElements in the Masked Array...
",arr.size)
# To convert the input to a masked array of the given data-type, use the numpy.ma.asarray() method in Python Numpy
print("\nMasked Array...
",np.ma.asarray(arr))
# Check the type
print("\nType...
",type(np.ma.asarray(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 Masked Array... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]] Type... <class 'numpy.ma.core.MaskedArray'>
