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
-
Economics & Finance
Return an empty masked array of the given shape and dtype where all the data are masked in Numpy
To return an empty masked array of the given shape and dtype where all the data are masked, use the ma.masked_all() method in Python Numpy. The 1st parameter sets the shape of the required MaskedArray. The dtype parameter sets the desired output data-type for the array.
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
Return an empty masked array of the given shape and dtype where all the data are masked using the ma.masked_all(). The dtype parameter sets the desired output data-type for the array −s
arr = ma.masked_all((5, 5),dtype = np.int32)
Displaying our array −
print("Array...<br>",arr)
Get the datatype −
print("\nArray datatype...<br>",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...<br>",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...<br>",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...<br>",arr.size)
Example
# Python ma.MaskedArray - Return an empty masked array of the given shape and dtype where all the data are masked
import numpy as np
import numpy.ma as ma
# To return an empty masked array of the given shape and dtype where all the data are masked, use the ma.masked_all() method in Python Numpy
# The 1st parameter sets the shape of the required MaskedArray
# The dtype parameter sets the desired output data-type for the array
arr = ma.masked_all((5, 5),dtype = np.int32)
# Displaying our array
print("Array...<br>",arr)
# Get the datatype
print("\nArray datatype...<br>",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...<br>",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...<br>",arr.shape)
# Get the number of elements of the Array
print("\nElements in the Array...<br>",arr.size)
Output
Array... [[-- -- -- -- --] [-- -- -- -- --] [-- -- -- -- --] [-- -- -- -- --] [-- -- -- -- --]] Array datatype... int32 Array Dimensions... 2 Our Array Shape... (5, 5) Elements in the Array... 25
