

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Return the addresses of the data and mask areas of a masked array in Numpy
<p>To return the addresses of the data and mask areas of a masked array, use the <strong>ma.MaskedArray.ids()</strong> method in Python Numpy.</p><p>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.</p><p>NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.</p><h2>Steps</h2><p>At first, import the required library −</p><pre class="result notranslate">import numpy as np import numpy.ma as ma</pre><p>Create an array with int elements using the numpy.array() method −</p><pre class="result notranslate">arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) print("Array... ", arr) print(" Array type... ", arr.dtype)</pre><p>Get the dimensions of the Array −</p><pre class="result notranslate">print(" Array Dimensions... ",arr.ndim) </pre><p>Create a masked array and mask some of them as invalid −</p><pre class="result notranslate">maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [0, 1, 0]]) print(" Our Masked Array ", maskArr) print(" Our Masked Array type... ", maskArr.dtype)</pre><p>Get the dimensions of the Masked Array −</p><pre class="result notranslate">print(" Our Masked Array Dimensions... ",maskArr.ndim) </pre><p>Get the shape of the Masked Array −</p><pre class="result notranslate">print(" Our Masked Array Shape... ",maskArr.shape)</pre><p>Get the number of elements of the Masked Array −</p><pre class="result notranslate">print(" Elements in the Masked Array... ",maskArr.size) </pre><p>To return the addresses of the data and mask areas of a masked array, use the ma.MaskedArray.ids() method −</p><pre class="result notranslate">print(" Resultant Array... ",maskArr.ids())</pre><h2>Example</h2><pre class="demo-code notranslate language-numpy" data-lang="numpy">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(" Array type... ", arr.dtype) # Get the dimensions of the Array print(" Array Dimensions... ",arr.ndim) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [0, 1, 0]]) print(" Our Masked Array ", maskArr) print(" Our Masked Array type... ", maskArr.dtype) # Get the dimensions of the Masked Array print(" Our Masked Array Dimensions... ",maskArr.ndim) # Get the shape of the Masked Array print(" Our Masked Array Shape... ",maskArr.shape) # Get the number of elements of the Masked Array print(" Elements in the Masked Array... ",maskArr.size) # To return the addresses of the data and mask areas of a masked array, use the ma.MaskedArray.ids() method print(" Resultant Array... ",maskArr.ids())</pre><h2>Output</h2><pre class="result notranslate">Array... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- 81] [-- 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 Resultant Array... (93885016023728, 93885016183488)</pre>
- Related Questions & Answers
- Return the mask of a masked array in Numpy
- Return the mask of a masked array or full boolean array of False in Numpy
- Return the mask of a masked array when mask is equal to nomask
- Return the underlying data as a view of the masked array in Numpy
- Return a copy of the masked array in NumPy
- Return the transpose of the masked array in NumPy
- Return the length of the masked array in Numpy
- Return the absolute value of a masked Array in NumPy
- Copy and return all the elements of a masked array in Numpy
- Return the average of the masked array elements in Numpy
- Return the variance of the masked array elements in Numpy
- Return the pickle of the masked array as a string in NumPy
- Return an empty masked array of the given shape and dtype where all the data are masked in Numpy
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Swap the bytes of the masked array data in Numpy
Advertisements