{"id":19306,"date":"2021-06-26T19:26:55","date_gmt":"2021-06-26T19:26:55","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19306"},"modified":"2022-07-25T15:52:29","modified_gmt":"2022-07-25T15:52:29","slug":"numpy-argmax","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-argmax","title":{"rendered":"Python np.argmax() function"},"content":{"rendered":"\n<p>NumPy (np) is one of the most popular libraries for mathematical and scientific calculations. It provides a lot of functions to work with multidimensional arrays. In this article, we will focus on <strong>Python np.argmax() function<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python np.argmax() function<\/h2>\n\n\n\n<p>As the name suggests, the <strong>argmax()<\/strong> function returns the index of the maximum value in the NumPy array. If there are multiple indices with the same max values, the first index will be returned.<\/p>\n\n\n\n<p><strong>argmax() syntax:<\/strong><\/p>\n\n\n\n<p>np.argmax(<em>a<\/em><strong>,&nbsp;<\/strong><em>axis=None<\/em><strong>,&nbsp;<\/strong><em>out=None<\/em><strong>,&nbsp;<\/strong><em>*<\/em><strong>,&nbsp;<\/strong><em>keepdims=&lt;no&nbsp;value&gt;<\/em><strong>)<\/strong><\/p>\n\n\n\n<p>The first argument is the input array. If there is no axis provided, the array is <strong>flattened<\/strong> and then the index of max value is returned.<\/p>\n\n\n\n<p>If we specify the <em><strong>axis<\/strong><\/em>, it returns the index value along the given axis. <\/p>\n\n\n\n<p>The third argument is used to pass an array argument to store the result, it should be of the correct shape and data type to work properly.<\/p>\n\n\n\n<p>If <strong><em>keepdims<\/em><\/strong> is passed as True, the axes which are reduced are left in the result as dimensions with size one.<\/p>\n\n\n\n<p>Let&#8217;s look at some examples of using argmax() function to understand the usage of different arguments properly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Find the index of maximum value using np.argmax()<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; import numpy as np\n&gt;&gt;&gt; arr = np.array(&#x5B;&#x5B;4,2,3], &#x5B;1,6,2]])\n&gt;&gt;&gt; arr\narray(&#x5B;&#x5B;4, 2, 3],\n       &#x5B;1, 6, 2]])\n&gt;&gt;&gt; np.ndarray.flatten(arr)\narray(&#x5B;4, 2, 3, 1, 6, 2])\n&gt;&gt;&gt; np.argmax(arr)\n4\n<\/pre><\/div>\n\n\n<p>The np.argmax() returns 4 because the array is first flattened and then the index of max value is returned. So in this case, the max value is 6 and its index in the flattened array is 4.<\/p>\n\n\n\n<p>But, we want the index value in a normal array, not the flattened one. So, we have to use the <strong><em>argmax()<\/em><\/strong> with the <strong><em>unravel_index()<\/em><\/strong> function to get the index value in the proper format.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; np.unravel_index(np.argmax(arr), arr.shape)\n(1, 1)\n&gt;&gt;&gt;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Finding the index of max value along an axis<\/h3>\n\n\n\n<p>If you want the index of max values along different axes, pass the axis parameter value. If we pass axis=0, the index of max values along the column is returned. For axis=1, the index of max values along the row is returned.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; arr\narray(&#x5B;&#x5B;4, 2, 3],\n       &#x5B;1, 6, 2]])\n&gt;&gt;&gt; np.argmax(arr, axis=0)\narray(&#x5B;0, 1, 0])\n&gt;&gt;&gt; np.argmax(arr, axis=1)\narray(&#x5B;0, 1])\n<\/pre><\/div>\n\n\n<p>For axis = 0, the first column values are 4 and 1. So the max value index is 0. Similarly, for the second column, the values are 2 and 6, so the max value index is 1. For the third column, values are 3 and 2, so the max value index is 0. That&#8217;s why we are getting the output as an array([0, 1, 0]).<\/p>\n\n\n\n<p>For axis = 1, the first row values are (4, 2, 3), so the max value index is 0. For the second row, values are (1, 6, 2), so the max value index is 1. Hence the output array([0, 1]).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Using np.argmax() with multiple maximum values<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt; import numpy as np\n&gt;&gt;&gt; arr = np.arange(6).reshape(2,3)\n&gt;&gt;&gt; arr\narray(&#x5B;&#x5B;0, 1, 2],\n       &#x5B;3, 4, 5]])\n&gt;&gt;&gt; arr&#x5B;0]&#x5B;1] = 5\n&gt;&gt;&gt; arr\narray(&#x5B;&#x5B;0, 5, 2],\n       &#x5B;3, 4, 5]])\n&gt;&gt;&gt; np.argmax(arr)\n1\n&gt;&gt;&gt; arr&#x5B;0]&#x5B;2] = 5\n&gt;&gt;&gt; arr\narray(&#x5B;&#x5B;0, 5, 5],\n       &#x5B;3, 4, 5]])\n&gt;&gt;&gt; np.argmax(arr)\n1\n&gt;&gt;&gt; np.argmax(arr, axis=0)\narray(&#x5B;1, 0, 0])\n&gt;&gt;&gt; np.argmax(arr, axis=1)\narray(&#x5B;1, 2])\n&gt;&gt;&gt; \n<\/pre><\/div>\n\n\n<p>We are using <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-arange-method-in-python\" data-type=\"post\" data-id=\"3529\">arange() function<\/a> to create a 2d array with some default values. Then we are changing one of the values to have multiple indexes with the max value. It&#8217;s clear from the output that the first index of the max value is returned when there are multiple places with the max value.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>NumPy argmax() function is easy to understand, just remember that the array is flattened before finding the index of the max value. Also, the axis argument is very helpful in finding the indices of the max values along rows and columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" data-type=\"post\" data-id=\"7694\">NumPy Tutorial<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/nan-in-numpy-and-pandas\" data-type=\"post\" data-id=\"9335\">NaN in NumPy<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\">NumPy Arrays<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-reshape-function\" data-type=\"post\" data-id=\"6588\">NumPy reshape() function<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/sorting-techniques-in-numpy\">NumPy Sorting Array<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.argmax.html\" target=\"_blank\" rel=\"noopener\">Official Docs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>NumPy (np) is one of the most popular libraries for mathematical and scientific calculations. It provides a lot of functions to work with multidimensional arrays. In this article, we will focus on Python np.argmax() function. Python np.argmax() function As the name suggests, the argmax() function returns the index of the maximum value in the NumPy [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":32537,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-19306","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\/19306","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19306"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19306\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/32537"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}