{"id":17416,"date":"2023-02-27T16:35:07","date_gmt":"2023-02-27T11:05:07","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=17416"},"modified":"2023-02-27T16:35:14","modified_gmt":"2023-02-27T11:05:14","slug":"numpy-argmax","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-argmax\/","title":{"rendered":"Numpy Argmax \u2013 Explained with Examples"},"content":{"rendered":"\n<p>One of the important metrics that is ubiquitous in analysing data is the maximum. Knowing this measure is critical in multiple aspects of machine learning. One such aspect would be to foresee the bias in the results that a maximum value could inflict.<\/p>\n\n\n\n<p>But what if one would like to go beyond finding the maximum value such as finding its index in the given dataset or even finding the indices of all the maximum values along an axis? Does Python have a function that could lend a helping hand? Seems that there is!<\/p>\n\n\n\n<p>Enter the <em>argmax( ) <\/em>function. This function successfully returns the indices of the maximum values along the specified axis. Hailing from the <em>numpy <\/em>library, one ought to import this library first before putting the function into use. It can be done using the below code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>This article sets out to explore the <em>argmax( ) <\/em>function in depth.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/codeforgeek.com\/python-string-interpolation\/\" data-type=\"post\" data-id=\"12072\">Python String Interpolation<\/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\">Syntax of the argmax( ) function<\/h2>\n\n\n\n<p>Given below is the syntax of the <em>argmax( ) <\/em>function containing its mandatory and optional constructs required for its proper functioning.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.argmax(a, axis=None, keepdims=&lt;no value&gt;)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>a \u2013 <\/em><\/strong>input array for which the indices of the maximum values are to be found along an axis<\/li>\n\n\n\n<li><strong><em>axis \u2013 <\/em><\/strong>Set to \u2018None\u2019 by default, it is used to specify the axis along which the indices of the maximum values are to be returned<\/li>\n\n\n\n<li><strong><em>keepdims \u2013<\/em> <\/strong>Set to \u2018No Value\u2019 by default, it is used to leave the reduced axes as dimensions with size one &#038; broadcast the result correctly against the input array<\/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\">Use cases for the argmax( ) function<\/h2>\n\n\n\n<p>To comprehend the results of this function, it is a prerequisite to understand the logic upon which it works. For any given array, indices are those which indicate the location at which each entity within the array resides.<\/p>\n\n\n\n<p>Normal practice is to start from the left and move towards the right while numbering the indices. This leaves the leftmost value with zero (0) as its index and gets incremented by one as it moves towards the right. <\/p>\n\n\n\n<p>Similarly while moving downwards instead of sideways, the same logic is applied. The topmost value is set with zero (0) as its index and gets incremented by one as it moves downwards. To move sideways from left to right, the axis is to be set to one (1), whereas if one would like to move downwards then the axis value to be set is zero (0).<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/codeforgeek.com\/scraping-amazon-product-data-python-beautifulsoup\/\" data-type=\"post\" data-id=\"12501\">Scraping Amazon Product Data using Python BeautifulSoup<\/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\">Using on 1-Dimensional array<\/h2>\n\n\n\n<p>Let\u2019s get started with a 1-Dimensional array such as the one given below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar1 = np.array(&#x5B;-1, 3, 67, 9])\n<\/pre><\/div>\n\n\n<p>Now, let\u2019s find the index of its maximum value using the <em>argmax( ) <\/em>function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.argmax(ar1)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Index-of-Maximum-Value-in-\u2018ar1.jpg\" alt=\"Index Of Maximum Value In \u2018ar1\" class=\"wp-image-44478\"\/><figcaption class=\"wp-element-caption\">Index of Maximum Value in \u2018ar1&#8242;<\/figcaption><\/figure>\n\n\n\n<p>It could be seen from the above result that the index \u20182\u2019 of the maximum value \u201867\u2019 is returned owing to its position from the leftmost value in the array. The index of the leftmost value is zero &#038; \u201867\u2019 being third in position from the leftmost value gets \u20182\u2019 as its index.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using on N-Dimensional array<\/h2>\n\n\n\n<p>The axis option cannot be fully exercised in a 1-Dimensional array since it has only one row. Let\u2019s find the indices of maximum values along both axes for the below N-Dimensional array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nar2 = np.array(&#x5B;&#x5B;0, 23, 4, -9],\n               &#x5B;5, 7, 89, 3],\n               &#x5B;-4, 10, 9, 78]])\n<\/pre><\/div>\n\n\n<p>Now, it\u2019s time to find the indices of maximum values sideways (i.e) axis=1.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.argmax(ar2, axis=1)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Index-of-maximum-values-across-each-row.jpg\" alt=\"Index Of Maximum Values Across Each Row\" class=\"wp-image-44484\"\/><figcaption class=\"wp-element-caption\">Index of Maximum Values Across Each Row<\/figcaption><\/figure>\n\n\n\n<p>The index of \u201823\u2019 in the first row is \u20181\u2019 and those of \u201889\u2019 in the second row &amp; \u201878\u2019 in the third row are \u20182\u2019 and \u20183\u2019 respectively. The same has been returned in the result as shown above. One can also find the indices of the maximum values along each column by setting axis=0.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/02\/Index-of-maximum-values-along-each-column.jpg\" alt=\"Index Of Maximum Values Along Each Column\" class=\"wp-image-44485\"\/><figcaption class=\"wp-element-caption\">Index of Maximum Values Along Each Column<\/figcaption><\/figure>\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>Now that we have reached the end of this article, hope it has elaborated on how to use the <strong><em>argmax( ) <\/em>function<\/strong> from the <em>numpy <\/em>library. Here\u2019s another article that details the workings of the <em>linalg.tensorinv( ) <\/em>function from the <em>numpy<\/em> library within Python. There are numerous other enjoyable and equally informative articles in Codeforgeek that might be of great help to those who are looking to level up in Python. <em>Carpe diem!<\/em><\/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.argmax.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.argmax.html<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of the important metrics that is ubiquitous in analysing data is the maximum. Knowing this measure is critical in multiple aspects of machine learning. One such aspect would be to foresee the bias in the results that a maximum value could inflict. But what if one would like to go beyond finding the maximum [&hellip;]<\/p>\n","protected":false},"author":73,"featured_media":17418,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-17416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png.webp",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png-150x150.webp",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png-300x200.webp",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png-768x512.webp",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png-1024x683.webp",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png.webp",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/02\/Argmax-Python.png.webp",1200,800,false]},"uagb_author_info":{"display_name":"Ninad Pathak","author_link":"https:\/\/codeforgeek.com\/author\/ninad\/"},"uagb_comment_info":0,"uagb_excerpt":"One of the important metrics that is ubiquitous in analysing data is the maximum. Knowing this measure is critical in multiple aspects of machine learning. One such aspect would be to foresee the bias in the results that a maximum value could inflict. But what if one would like to go beyond finding the maximum&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/17416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/73"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=17416"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/17416\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/17418"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=17416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=17416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=17416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}