Return the addresses of the data and mask areas of a masked array in Numpy

NumpyServer Side ProgrammingProgramming

<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 &minus;</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 &minus;</p><pre class="result notranslate">arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) print(&quot;Array... &quot;, arr) print(&quot; Array type... &quot;, arr.dtype)</pre><p>Get the dimensions of the Array &minus;</p><pre class="result notranslate">print(&quot; Array Dimensions... &quot;,arr.ndim) </pre><p>Create a masked array and mask some of them as invalid &minus;</p><pre class="result notranslate">maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [0, 1, 0]]) print(&quot; Our Masked Array &quot;, maskArr) print(&quot; Our Masked Array type... &quot;, maskArr.dtype)</pre><p>Get the dimensions of the Masked Array &minus;</p><pre class="result notranslate">print(&quot; Our Masked Array Dimensions... &quot;,maskArr.ndim) </pre><p>Get the shape of the Masked Array &minus;</p><pre class="result notranslate">print(&quot; Our Masked Array Shape... &quot;,maskArr.shape)</pre><p>Get the number of elements of the Masked Array &minus;</p><pre class="result notranslate">print(&quot; Elements in the Masked Array... &quot;,maskArr.size) </pre><p>To return the addresses of the data and mask areas of a masked array, use the ma.MaskedArray.ids() method &minus;</p><pre class="result notranslate">print(&quot; Resultant Array... &quot;,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(&quot;Array... &quot;, arr) print(&quot; Array type... &quot;, arr.dtype) # Get the dimensions of the Array print(&quot; Array Dimensions... &quot;,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(&quot; Our Masked Array &quot;, maskArr) print(&quot; Our Masked Array type... &quot;, maskArr.dtype) # Get the dimensions of the Masked Array print(&quot; Our Masked Array Dimensions... &quot;,maskArr.ndim) # Get the shape of the Masked Array print(&quot; Our Masked Array Shape... &quot;,maskArr.shape) # Get the number of elements of the Masked Array print(&quot; Elements in the Masked Array... &quot;,maskArr.size) # To return the addresses of the data and mask areas of a masked array, use the ma.MaskedArray.ids() method print(&quot; Resultant Array... &quot;,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>
raja
Updated on 17-Feb-2022 11:25:23

Advertisements