{"id":37828,"date":"2022-11-30T06:44:04","date_gmt":"2022-11-30T06:44:04","guid":{"rendered":"https:\/\/www.askpython.com\/?p=37828"},"modified":"2022-11-30T06:44:48","modified_gmt":"2022-11-30T06:44:48","slug":"numpy-amin","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-amin","title":{"rendered":"NumPy amin &#8211; Return the Minimum of Array Elements using Numpy"},"content":{"rendered":"\n<p>Hello and welcome to this tutorial on <strong>Numpy amin<\/strong>. In this tutorial, we will be learning about the <strong>NumPy amin() <\/strong>method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-subtract\" data-type=\"post\" data-id=\"37502\">Numpy.subtract(): How to Use Subtract Numbers with NumPy in Python?<\/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 amin?<\/h2>\n\n\n\n<p>The amin method in NumPy is a function that returns the minimum of the array elements. 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 amin<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.amin(a, axis=None, out=None)\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. It can be axis=0  or axis=1 or axis=None which implies that the minimum of the flattened 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><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong> The minimum element in <em>a<\/em>. If <em>axis=None<\/em>, then the output is a scalar, otherwise, the output is an array.<\/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.amin()<\/h2>\n\n\n\n<p>Let\u2019s get right into the different examples of using numpy.amin() function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using numpy.amin() when the array is 1-dimensional<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;10, 3, 25]\n\nans = np.amin(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;10, 3, 25]\nMinimum of a = 3\n<\/pre><\/div>\n\n\n<p>Comparing all the elements in the given array, the minimum of 10, 3 and 25 is 3. Hence, 3 are returned.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using numpy.amin() when the array contains negative numbers<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;-8, 6], &#x5B;-5, -12]]\n\nans = np.amin(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;&#x5B;-8, 6], &#x5B;-5, -12]]\nMinimum of a = -12\n<\/pre><\/div>\n\n\n<p>Comparing all the values in the array, -12 is the minimum element here.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using numpy.amin() when the array contains NaN values<\/h3>\n\n\n\n<p>In Python, NaN stands for Not a Number.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;26, np.nan, 8, np.nan, -4]\n\nans = np.amin(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;26, nan, 8, nan, -4]\nMinimum of a = nan\n<\/pre><\/div>\n\n\n<p>If the input contains NaNs, then the NumPy <code>amin()<\/code> method always returns <strong>nan<\/strong> as output, irrespective of the other elements present in the input array. <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using numpy.amin() when the array is 2-dimensional<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\n\nans = np.amin(a)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\nMinimum of a = 3\n<\/pre><\/div>\n\n\n<p>In the case of a 2-dimensional array, when no axis is mentioned, the array is first flattened row-wise and then its minimum is calculated. <br>In the above example, the flattened array will be [16, 3, 48, 66] and the minimum element in it is 3, hence it is returned by the <strong>amin()<\/strong> method.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Using numpy.amin() to find the minimum along a given axis<\/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\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\n# minimum along axis=0\nans = np.amin(a, axis=0)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\nMinimum of a = &#x5B;16  3]\n<\/pre><\/div>\n\n\n<p>Here, the elements are compared column-wise and their minimum is stored in the output. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = minimum(a&#x5B;0]&#x5B;0], a&#x5B;1]&#x5B;0]) = minimum(16, 48) = 16\nans&#x5B;1] = minimum(a&#x5B;0]&#x5B;1], a&#x5B;1]&#x5B;1]) = minimum(3, 66) = 3\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\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\n# minimum along axis=1\nans = np.amin(a, axis=1)\nprint(&quot;a =&quot;, a)\nprint(&quot;Minimum of a =&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=\"\">\na = &#x5B;&#x5B;16, 3], &#x5B;48, 66]]\nMinimum of a = &#x5B; 3 48]\n<\/pre><\/div>\n\n\n<p>Here, the elements are compared row-wise and their minimum is stored in the output. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nans&#x5B;0] = minimum(a&#x5B;0]&#x5B;0], a&#x5B;0]&#x5B;1]) = minimum(16, 3) = 3\nans&#x5B;1] = minimum(a&#x5B;1]&#x5B;0], a&#x5B;1]&#x5B;1]) = minimum(48, 66) = 48\n<\/pre><\/div>\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 amin<\/strong> method and practiced different types of examples using the same. &nbsp;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.amin.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy amin Official Documentation<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello and welcome to this tutorial on Numpy amin. In this tutorial, we will be learning about the NumPy amin() method and also seeing a lot of examples regarding the same. So let us begin! Also read: Numpy.subtract(): How to Use Subtract Numbers with NumPy in Python? What is NumPy amin? The amin method in [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":37830,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37828","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\/37828","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=37828"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37828\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/37830"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}