Check which element in a masked array is less than or equal to a given value in Numpy

NumpyServer Side ProgrammingProgramming

<p>To check which element in a masked array is less than or equal to a given value, use the <strong>ma.MaskedArray.__le__()</strong> method. Returns with boolean type i.e. True and False. 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="just-code notranslate language-numpy" data-lang="numpy">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="just-code notranslate language-numpy" data-lang="numpy">arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]]) print(&quot;Array... &quot;, arr) print(&quot; Array type... &quot;, arr.dtype)</pre><p>Get the dimensions of the Array &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Array Dimensions... &quot;,arr.ndim)</pre><p>Create a masked array and mask some of them as invalid &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 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="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Our Masked Array Dimensions... &quot;,maskArr.ndim)</pre><p>Get the shape of the Masked Array &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Our Masked Array Shape... &quot;,maskArr.shape)</pre><p>Get the number of elements of the Masked Array &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">print(&quot; Elements in the Masked Array... &quot;,maskArr.size) </pre><p>The value to be compared &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy">val = 61 print(&quot; The given value to be compared with the masked array elements... &quot;,val)</pre><p>To check which element in a masked array is less than or equal to a given value, use the ma.MaskedArray.__le__() method. Returns with boolean type i.e. True and False. True is returned for every array element less than or equal to a given value val &minus;</p><pre class="just-code notranslate language-numpy" data-lang="numpy" style="">print(&quot; Display True for each element less than or equal to a given value val... &quot;, maskArr.__le__(val))</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([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]]) 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, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 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) # The value to be compared val = 61 print(&quot; The given value to be compared with the masked array elements... &quot;,val) # To check which element in a masked array is less than or equal to a given value, use the ma.MaskedArray.__le__() method # Returns with boolean type i.e. True and False. # True is returned for every array element less than or equal to a given value val print(&quot; Display True for each element less than or equal to a given value val... &quot;, maskArr.__le__(val))</pre><h2>Output</h2><pre class="result notranslate">Array... [[55 85 68 84] [67 33 39 53] [29 88 51 37] [56 45 99 85]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- 68 84] [67 33 -- 53] [29 88 51 --] [56 -- 99 85]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 The given value to be compared with the masked array elements... 61 Display True for each element less than or equal to a given value val... [[-- -- False False] [False True -- True] [True False True --] [True -- False False]]</pre>
raja
Updated on 22-Feb-2022 07:34:09

Advertisements