{"id":21965,"date":"2023-09-27T11:49:58","date_gmt":"2023-09-27T06:19:58","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=21965"},"modified":"2023-09-27T11:50:00","modified_gmt":"2023-09-27T06:20:00","slug":"numpy-max","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-max\/","title":{"rendered":"numpy.max() in Python: Get Maximum Value in Array"},"content":{"rendered":"\n<p>Using <strong>numpy.max()<\/strong> function we can find the maximum element along a specified axis in a NumPy array. It is useful in many situations such as whenever we are doing error detection or quality control we can identify outliers in a dataset by finding the maximum value in a dataset. Also in the statistical analysis for calculating percentiles, finding the maximum value might play an important role. Another example could be when we compare two arrays element-wise for performing operations such as element-wise maximum we need to find the maximum value.<\/p>\n\n\n\n<p>In this article, we\u2019ll look at three ways to find the maximum element from the array using&nbsp;<strong>numpy.max()<\/strong>&nbsp;function. Let\u2019s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing numpy.max() Function<\/h2>\n\n\n\n<p>The <strong>numpy.max() <\/strong>function in <a href=\"https:\/\/codeforgeek.com\/python\/\">Python&#8217;s<\/a> NumPy library is used to find the maximum value in a <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy<\/a> array. It returns the maximum value among all the elements in the array.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.max(a, axis=None, out=None, keepdims=False, initial=&lt;no value&gt;, where=True)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>a:<\/strong> The input array from which we want to find the maximum value.<\/li>\n\n\n\n<li><strong>axis (optional):<\/strong> If specified, it allows us to find the maximum value along a particular axis of the input array, resulting in a reduced dimension array.<\/li>\n\n\n\n<li><strong>out (optional):<\/strong> An alternative output array in which to place the result.<\/li>\n\n\n\n<li><strong>keepdims (optional):<\/strong> If True, the dimensions of the output array will match those of the input array with reduced dimensions set to 1.<\/li>\n\n\n\n<li><strong>initial (optional):<\/strong> The initial value from which the maximum is determined.<\/li>\n\n\n\n<li><strong>where (optional):<\/strong> A boolean array that specifies where to compute the maximum.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Calculate Maximum of Array in NumPy<\/h2>\n\n\n\n<p><strong>There are three ways through which we can find the maximum element from the array by using numpy.max() function<\/strong><strong>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Maximum element from the entire array<\/li>\n\n\n\n<li>Maximum element from array rows<\/li>\n\n\n\n<li>Maximum element from array columns<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Maximum Element from the Entire Array<\/h3>\n\n\n\n<p>Here by using the&nbsp;<strong>numpy.max()<\/strong>&nbsp;function we will find the maximum element from the whole array.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr1 = np.array(&#x5B;21,22,23,24])\nprint(np.max(arr1))\n<\/pre><\/div>\n\n\n<p>In the code above to create an array and to use the&nbsp;<strong>max()<\/strong>&nbsp;function on an array, we first imported the&nbsp;<strong>Numpy<\/strong>&nbsp;library as&nbsp;<strong>np<\/strong>&nbsp;then we created a <strong>1D<\/strong> array of values 21,22,23,24 and saved it into a variable called&nbsp;<strong>arr1<\/strong>. Then, at last, we applied the&nbsp;<strong>numpy.max()<\/strong>&nbsp;function on arr1.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"721\" height=\"59\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1807.png\" alt=\"Example 1: Maximum element from the whole array\" class=\"wp-image-22078\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1807.png 721w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1807-300x25.png 300w\" sizes=\"(max-width: 721px) 100vw, 721px\" \/><\/figure>\n\n\n\n<p>In the output above we have seen that after applying the&nbsp;<strong>numpy.max()<\/strong>&nbsp;function on the entire array&nbsp;<strong>arr1&nbsp;<\/strong>we got the result 24 which is the maximum value.<\/p>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr2 = np.array(&#x5B;&#x5B;21,22,23],&#x5B;24,25,26]])\nprint(np.max(arr2))\n<\/pre><\/div>\n\n\n<p>In this example, we created a <strong>2D<\/strong> array and applied the&nbsp;<strong>numpy.max()&nbsp;<\/strong>function on it.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"818\" height=\"64\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1808.png\" alt=\"Example 2: Maximum element from the whole array\" class=\"wp-image-22079\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1808.png 818w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1808-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1808-768x60.png 768w\" sizes=\"(max-width: 818px) 100vw, 818px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2. Maximum Element from Array Rows<\/h3>\n\n\n\n<p>Here we will provide&nbsp;<strong>axis=1<\/strong>&nbsp;as a parameter in the&nbsp;<strong>numpy.max()<\/strong>&nbsp;function for getting the maximum element from the array rows.<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr2 = np.array(&#x5B;&#x5B;21,22,23],&#x5B;24,25,26]])\nprint(np.max(arr2,axis=1))\n<\/pre><\/div>\n\n\n<p>In the above code, we have passed <strong>axis=1<\/strong>\u00a0as an additional argument to the\u00a0<strong>numpy.max()<\/strong>\u00a0function to get the maximum element row-wise from <strong>arr2<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"882\" height=\"66\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1809.png\" alt=\"Maximum element from array rows\" class=\"wp-image-22080\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1809.png 882w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1809-300x22.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1809-768x57.png 768w\" sizes=\"(max-width: 882px) 100vw, 882px\" \/><\/figure>\n\n\n\n<p>In the output above we got maximum elements row-wise in the<strong> 2D <\/strong>array <strong>arr2<\/strong>. The first row contains values 21,22,23 so we got 23 as the maximum and the second row contains values 24,25,26 so we got 26 as the maximum from the second row.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Maximum Element from Array Columns<\/h3>\n\n\n\n<p>Here we will provide&nbsp;<strong>axis=0<\/strong>&nbsp;as a parameter in the&nbsp;<strong>numpy.max()<\/strong>&nbsp;function for getting the maximum element from the array columns.<\/p>\n\n\n\n<p><strong>Example :<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr2 = np.array(&#x5B;&#x5B;21,22,23],&#x5B;24,25,26]])\nprint(np.max(arr2,axis=0))\n<\/pre><\/div>\n\n\n<p>In the above code, we have passed <strong>axis=0<\/strong>\u00a0as an additional argument to the <strong>numpy.max()<\/strong>\u00a0function to get the maximum element column-wise from <strong>arr2<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"849\" height=\"67\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1810.png\" alt=\"Maximum element from array columns\" class=\"wp-image-22081\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1810.png 849w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1810-300x24.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1810-768x61.png 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/figure>\n\n\n\n<p>In the output above we got maximum elements column-wise in the <strong>2D <\/strong>array <strong>arr2<\/strong>.The first column contains the value 21,24 so we got 24 as the maximum, the second column contains the value 22,25 so we got 25 as the maximum and in last the third column contains the value 23,26 so we got 26 as the maximum.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The <strong>numpy.max()<\/strong> function is a versatile tool for finding the maximum value in arrays. In this tutorial, we have discussed&nbsp;<strong>numpy.max()<\/strong>&nbsp;function provided by Python\u2019s&nbsp;NumPy&nbsp;library and also explored three ways to find the maximum element in the array using&nbsp;<strong>numpy.max()<\/strong>&nbsp;function with examples. After reading this tutorial, we hope you can easily find the maximum element from the array in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/26810112\/what-does-numpy-max-function-do\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/26810112\/what-does-numpy-max-function-do<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using numpy.max() function we can find the maximum element along a specified axis in a NumPy array. It is useful in many situations such as whenever we are doing error detection or quality control we can identify outliers in a dataset by finding the maximum value in a dataset. Also in the statistical analysis for [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22082,"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-21965","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\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.max-in-Python-Maximum-Element-in-a-NumPy-Array.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"Using numpy.max() function we can find the maximum element along a specified axis in a NumPy array. It is useful in many situations such as whenever we are doing error detection or quality control we can identify outliers in a dataset by finding the maximum value in a dataset. Also in the statistical analysis for&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21965","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\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=21965"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21965\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22082"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=21965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=21965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=21965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}