{"id":38038,"date":"2022-12-16T15:44:11","date_gmt":"2022-12-16T15:44:11","guid":{"rendered":"https:\/\/www.askpython.com\/?p=38038"},"modified":"2023-02-16T19:56:27","modified_gmt":"2023-02-16T19:56:27","slug":"numpy-nanmin","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-nanmin","title":{"rendered":"NumPy nanmin \u2013 Minimum of an array along an axis ignoring any NaNs"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on\u00a0<strong>Numpy nanmin<\/strong>. In this tutorial, we will be learning about the NumPy\u00a0<code>nanmin()<\/code>\u00a0method and also seeing a lot of examples regarding the same. So let us begin!<br><br><strong><em>Also read:\u00a0<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-nanmax\" data-type=\"post\" data-id=\"37918\">NumPy nanmax \u2013 Maximum of an array along an axis ignoring any NaNs<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy nanmin?<\/h2>\n\n\n\n<p>In Python,&nbsp;<strong>NaN<\/strong>&nbsp;denotes&nbsp;<strong>Not a Number<\/strong>. If we have an array that contains some NaN values and want to find the minimum value in it, we can use the&nbsp;<code>nanmin()<\/code>&nbsp;method from NumPy.<\/p>\n\n\n\n<p>The\u00a0<code>nanmin()<\/code>\u00a0method in NumPy is a function that returns the minimum of the array elements calculated by ignoring the NaN values in the array. It can be the minimum of all the array elements, the minimum of the array elements along the rows or the minimum of the array elements along the columns. <\/p>\n\n\n\n<p>We will see the examples for each of these in the upcoming section of this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of NumPy nanmin<\/h2>\n\n\n\n<p>Let us have a look at the syntax of the&nbsp;<code>nanmin()<\/code>&nbsp;function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.nanmin(a, axis=None, out=None, keepdims=&lt;no value&gt;, initial=&lt;no value&gt;, where=&lt;no value&gt;)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>a (array_like)<\/td><td>Input data.<\/td><td>Required<\/td><\/tr><tr><td>axis<\/td><td>Axis along which the minimum of the array is to be calculated.<br>It can be axis=0 or axis=1 or axis=None which implies that the minimum of the entire array is to be returned.<\/td><td>Optional<\/td><\/tr><tr><td>out<\/td><td>An alternative output array in which to place the result. It must have the same shape as the expected output.<\/td><td>Optional<\/td><\/tr><tr><td>keepdims (bool)<\/td><td>If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.<\/td><td>Optional<\/td><\/tr><tr><td>initial<\/td><td>The maximum value of an output element.<\/td><td>Optional<\/td><\/tr><tr><td>where<\/td><td>Elements to compare for finding the minimum.<\/td><td>Optional<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">NumPy nanmin parameters<\/figcaption><\/figure>\n\n\n\n<p><strong>Returns:<\/strong><br>An array containing the minimum of the array along the specified axis, ignoring all the NaNs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of NumPy nanmin<\/h2>\n\n\n\n<p>Let\u2019s get right into the different examples of using&nbsp;<strong>numpy.nanmin()<\/strong>&nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy nanmin of a 1-d array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;np.nan, 54, 1, 3, 44]\n# using the nanmin function to calculate the maximum\nans = np.nanmin(arr)\n\nprint(&quot;arr =&quot;, arr)\nprint(&quot;Result =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr = &#x5B;nan, 54, 1, 3, 44]\nResult = 1.0\n<\/pre><\/div>\n\n\n<p>Ignoring the NaN value, the minimum value among 54, 1, 3 and 44 is 1, hence it is returned.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy nanmin of a 2-d array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;30, -9], &#x5B;8, np.nan]]\n# using the nanmin function to calculate the maximum\nans = np.nanmin(arr)\n\nprint(&quot;arr =&quot;, arr)\nprint(&quot;Result =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr = &#x5B;&#x5B;30, -9], &#x5B;8, nan]]\nResult = -9.0\n<\/pre><\/div>\n\n\n<p>Similar to the previous example, the minimum of 30, -9, and 8 is 8.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy nanmin along an axis of the array<\/h3>\n\n\n\n<p><strong>axis = 0<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;16, 4], &#x5B;np.nan, 1]]\n# using the nanmin function to calculate the maximum\nans = np.nanmin(arr, axis=0)\n\nprint(&quot;arr =&quot;, arr)\nprint(&quot;Result =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr = &#x5B;&#x5B;16, 4], &#x5B;nan, 1]]\nResult = &#x5B;16.  1.]\n<\/pre><\/div>\n\n\n<p>Here, the values in each row for a specific column are compared to find the minimum element.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = min(arr&#x5B;0]&#x5B;0], arr&#x5B;1]&#x5B;0]) = min(16, np.nan) = 16 (ignoring NaN)\nans&#x5B;1] = min(arr&#x5B;0]&#x5B;1], arr&#x5B;1]&#x5B;1]) = min(4, 1) = 1\n<\/pre><\/div>\n\n\n<p><strong>axis = 1<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\narr = &#x5B;&#x5B;16, 4], &#x5B;np.nan, 1]]\n# using the nanmin function to calculate the maximum\nans = np.nanmin(arr, axis=1)\n\nprint(&quot;arr =&quot;, arr)\nprint(&quot;Result =&quot;, ans)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr = &#x5B;&#x5B;16, 4], &#x5B;nan, 1]]\nResult = &#x5B;4. 1.]\n<\/pre><\/div>\n\n\n<p>When&nbsp;<em>axis=1<\/em>, the elements in each row are compared over all the columns to find the minimum.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = min(arr&#x5B;0]&#x5B;0], arr&#x5B;0]&#x5B;1]) = min(16, 4) = 4\nans&#x5B;1] = min(arr&#x5B;1]&#x5B;0], arr&#x5B;1]&#x5B;1]) = min(np.nan, 1) = 1 (ignoring NaN)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy nanmin of an array containing infinity<\/h3>\n\n\n\n<p>Let us now see how the&nbsp;<code>numpy.nanmin()<\/code>&nbsp;method handles infinity along with NaNs in the array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n \n# array containing +infinity\na = np.array(&#x5B;16, 3, np.nan, 7, np.inf])\n# array containing -infinity\nb = np.array(&#x5B;16, 3, np.nan, 7, np.NINF])\n# array containing +infinity and -infinity\nc = np.array(&#x5B;16, 3, np.nan, np.NINF, 7, np.inf])\n \nmin_a = np.nanmin(a)\nmin_b = np.nanmin(b)\nmin_c = np.nanmin(c)\n \nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of the array a =&quot;, min_a)\nprint(&quot;\\nb =&quot;, b)\nprint(&quot;Minimum of the array b =&quot;, min_b)\nprint(&quot;\\nc =&quot;, c)\nprint(&quot;Minimum of the array c =&quot;, min_c)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;16.  3. nan  7. inf]\nMinimum of the array a = 3.0\n\nb = &#x5B; 16.   3.  nan   7. -inf]\nMinimum of the array b = -inf\n\nc = &#x5B; 16.   3.  nan -inf   7.  inf]\nMinimum of the array c = -inf\n<\/pre><\/div>\n\n\n<p>In the above code,&nbsp;<strong>NINF<\/strong>&nbsp;denotes&nbsp;<strong>-infinity&nbsp;<\/strong>and&nbsp;<strong>inf&nbsp;<\/strong>denotes&nbsp;<strong>infinity<\/strong>. Note that,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the array contains&nbsp;<strong>positive infinity<\/strong>&nbsp;then the minimum is<strong> <\/strong>the minimum of the whole array ignoring the NaNs.<\/li>\n\n\n\n<li>If the array contains&nbsp;<strong>negative infinity<\/strong>&nbsp;then the minimum is <strong>negative infinity<\/strong>.<\/li>\n\n\n\n<li>If the array contains&nbsp;<strong>both positive and negative infinity<\/strong>, then the minimum of the array is&nbsp;<strong>-inf<\/strong>, i.e.&nbsp;<strong>negative infinity<\/strong>.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the&nbsp;<strong>Numpy nanmin<\/strong>&nbsp;method and practiced different types of examples using the same.<br>If you want to learn more about NumPy, feel free to go through our&nbsp;<a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy tutorials<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.nanmin.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy nanmin Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on\u00a0Numpy nanmin. In this tutorial, we will be learning about the NumPy\u00a0nanmin()\u00a0method and also seeing a lot of examples regarding the same. So let us begin! Also read:\u00a0NumPy nanmax \u2013 Maximum of an array along an axis ignoring any NaNs What is NumPy nanmin? In Python,&nbsp;NaN&nbsp;denotes&nbsp;Not a Number. If [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":38039,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-38038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=38038"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/38038\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/38039"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=38038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=38038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=38038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}