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
Get the number of elements of the Masked Array in Numpy
To get the number of elements of the Masked Array, use the ma.MaskedArray.size attribute in Numpy. The array.size returns a standard arbitrary precision Python integer. This may not be the case with other methods of obtaining the same value, which returns an instance of np.int_), and may be relevant if the value is used further in calculations that may overflow a fixed size integer type.
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 using the numpy.array() method −
arr = np.array([[35, 85], [67, 33]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)
print("\nArray itemsize...<br>", arr.itemsize)
Get the dimensions of the Array −
print("Array Dimensions...<br>",arr.ndim)
Get the total bytes consumed −
print("Array nbytes...<br>",arr.nbytes)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)
Get the itemsize of the Masked Array −
print("\nOur Masked Array itemsize...<br>", maskArr.itemsize)
Get the dimensions of the Masked Array −
print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)
Get the shape of the Masked Array −
print("\nOur Masked Array Shape...<br>",maskArr.shape)
Get the number of elements of the Masked Array, use the ma.MaskedArray.size attribute in Numpy −
print("\nElements in the Masked Array...<br>",maskArr.size)
Example
import numpy as np
import numpy.ma as ma
arr = np.array([[35, 85], [67, 33]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)
print("\nArray itemsize...<br>", arr.itemsize)
# Get the dimensions of the Array
print("Array Dimensions...<br>",arr.ndim)
# Get the total bytes consumed
print("Array nbytes...<br>",arr.nbytes)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)
# Get the itemsize of the Masked Array
print("\nOur Masked Array itemsize...<br>", maskArr.itemsize)
#Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)
# Get the shape of the Masked Array
print("\nOur Masked Array Shape...<br>",maskArr.shape)
# To get the number of elements of the Masked Array, use the ma.MaskedArray.size attribute in Numpy
print("\nElements in the Masked Array...<br>",maskArr.size)
Output
Array... [[35 85] [67 33]] Array type... int64 Array itemsize... 8 Array Dimensions... 2 Array nbytes... 32 Our Masked Array [[35 85] [67 --]] Our Masked Array type... int64 Our Masked Array itemsize... 8 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 2) Elements in the Masked Array... 4
