{"id":47,"date":"2017-11-30T22:35:19","date_gmt":"2017-11-30T22:35:19","guid":{"rendered":"http:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/?page_id=47"},"modified":"2017-11-30T23:01:24","modified_gmt":"2017-11-30T23:01:24","slug":"finding-maximum-values-from-arrays","status":"publish","type":"page","link":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/finding-maximum-values-from-arrays\/","title":{"rendered":"Finding maximum values from arrays"},"content":{"rendered":"<p>There are times when you may wish to find a maximum, or a minimum, value from a calculation. There are even times when you may wish to match this maximum value to the input value which created it.<\/p>\n<p>For these occasions the use of the <span style=\"color: #3366ff\">numpy<\/span> functions <span style=\"color: #3366ff\">np.amax<\/span> and <span style=\"color: #3366ff\">np.argmax<\/span> are incredibly useful.<\/p>\n<p>In <a href=\"https:\/\/github.com\/bjmorgan\/python_in_chemistry\/blob\/master\/General\/Blackbody%20radiation%20maximum%20values.ipynb\">the attached notebook<\/a> the blackbody spectrum of a hot cup of tea is calculated and the spectrum plotted using the <span style=\"color: #3366ff\">matplotlib<\/span> functionality. The maximum value may easily be observed in this case, but so far we have not detailed how we can use functions in python to generate calculated values.<\/p>\n<p><span style=\"color: #3366ff\">np.amax<\/span> will find the maximum value in an array. In this case we can run this function agains the output 'calculated' array for intensity.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-48 aligncenter\" src=\"http:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npamax.png\" alt=\"The blackbody spectrum of a hot cup of tea with the maximum value highlighted - this maximum value may be calculated using the function np.amax\" width=\"389\" height=\"282\" srcset=\"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npamax.png 389w, https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npamax-300x217.png 300w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/p>\n<p>In this case my calculated array is called intensity363 and so the complete function looks like:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #3366ff\">np.amax(intensity363)<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>It may also be useful to know at what wavelength this maximum appears:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-49 aligncenter\" src=\"http:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npargmax.png\" alt=\"The blackbody spectrum of a hot cup of tea with the maximum value highlighted - the index of this maximum value may be calculated using the function np.argmax\" width=\"389\" height=\"282\" srcset=\"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npargmax.png 389w, https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-content\/uploads\/sites\/140\/2017\/11\/blackbody-npargmax-300x217.png 300w\" sizes=\"auto, (max-width: 389px) 100vw, 389px\" \/><\/p>\n<p>The function<span style=\"color: #3366ff\"> np.argmax<\/span> will determine which index in the array where the maximum value arises in the intensity363 array, this index value is then matched to the input wavelengths array to find the value of the input which produces this maximum value.<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #3366ff\">wavelengths[np.argmax(intensity363)]<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>You can see both of these values calculated in the print statements at the bottom of the first box of code.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Ben has also worked out how to present these values nicely in markdown which is included at the end of the notebooks - this is using the same method of calculating the maxima and minima, but is presenting it as markdown.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when you may wish to find a maximum, or a minimum, value from a calculation. There are even times when you may wish to match this maximum value to the input value which created it. For these...<\/p>\n","protected":false},"author":1034,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-47","page","type-page","status-publish","hentry"],"acf":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/Pd4Prp-L","_links":{"self":[{"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/pages\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/users\/1034"}],"replies":[{"embeddable":true,"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":0,"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/pages\/47\/revisions"}],"wp:attachment":[{"href":"https:\/\/stg-universityofbathblogs-staging.kinsta.cloud\/python\/wp-json\/wp\/v2\/media?parent=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}