
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Get Current Shape of Masked Array in NumPy
To get the shape of the Masked Array, use the ma.MaskedArray.shape attribute in Numpy. The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it.
As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required.
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...
", arr) print("
Array type...
", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...
",arr.ndim)
Get the total bytes consumed −
print("Array nbytes...
",arr.nbytes)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the itemsize of the Masked Array −
print("
Our Masked Array itemsize...
", maskArr.itemsize)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Get the shape of the Masked Array, use the ma.MaskedArray.shape attribute in Numpy −
print("
Our Masked Array Shape...
",maskArr.shape)
Example
import numpy as np import numpy.ma as ma arr = np.array([[35, 85], [67, 33]]) print("Array...
", arr) print("
Array type...
", arr.dtype) print("
Array itemsize...
", arr.itemsize) # Get the dimensions of the Array print("Array Dimensions...
",arr.ndim) # Get the total bytes consumed print("Array nbytes...
",arr.nbytes) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the itemsize of the Masked Array print("
Our Masked Array itemsize...
", maskArr.itemsize) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # To get the shape of the Masked Array, use the ma.MaskedArray.shape attribute in Numpy print("
Our Masked Array Shape...
",maskArr.shape)
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)