{"id":859,"date":"2012-10-29T16:51:57","date_gmt":"2012-10-29T15:51:57","guid":{"rendered":"http:\/\/www.2diabolos.com\/blog\/?page_id=859"},"modified":"2024-08-26T19:46:29","modified_gmt":"2024-08-26T18:46:29","slug":"python-how-to-locate-the-maximum-of-a-array-ndim1-and-efficiently","status":"publish","type":"page","link":"https:\/\/www.2diabolos.com\/blog\/python-and-spyder\/python-how-to-locate-the-maximum-of-a-array-ndim1-and-efficiently\/","title":{"rendered":"Python : how to locate the maximum of a array (ndim>1, and efficiently)"},"content":{"rendered":"<div class=\"columns\"><p>It sounds stupid, but I searched a long time to be able to find the position of the maximum of an array.<\/p>\n<p>Find the maximum is really easy : myArray.max()<\/p>\n<p>you might be tempted by searching the position of that maximum.<\/p>\n<p>You may do it with loops, but then you have to write a function for each dimension : 1 loop for a flat array, 2 loops for a 2D array, etc&#8230;. And you wanted a optimized code : don&rsquo;t even think about loops !<\/p>\n<p>I found a tricky one : numpy.where locate you what you want in your array. So this code is nicely working. Remember this one, because you may use it with \u00ab\u00a0myArray&gt;100\u00a0\u00bb if you want the coordinates where the values are above 100<\/p>\n<pre>np.array(np.where(myArray==myArray.max())).T<\/pre>\n<p>But actually, searching a bit more in Numpy, i found the \u00ab\u00a0argmax\u00a0\u00bb function. I thought : it&rsquo;s done ! Not completely. The function gives you the index of the flattened array. So you need this extra \u00ab\u00a0unravel_index\u00a0\u00bb\u00a0tricks :<\/p>\n<pre>np.unravel_index(myArray.argmax(),myArray.shape)<\/pre>\n<p>Guess what : the \u00ab\u00a0unravel_index(argmax\u00a0\u00bb solution is 10 times faster than the where(a==a.max())&#8230;<\/p>\n<p>&nbsp;<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>It sounds stupid, but I searched a long time to be able to find the position of the maximum of an array. Find the maximum is really easy : myArray.max() you might be tempted by searching the position of that maximum. You may do it with loops, but then you have to write a function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":861,"menu_order":20,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-859","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":2,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/859\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/859\/revisions\/863"}],"up":[{"embeddable":true,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/861"}],"wp:attachment":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/media?parent=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}