{"id":1673,"date":"2015-05-09T18:19:45","date_gmt":"2015-05-09T17:19:45","guid":{"rendered":"http:\/\/www.2diabolos.com\/blog\/?page_id=1673"},"modified":"2024-08-26T19:46:29","modified_gmt":"2024-08-26T18:46:29","slug":"force-maximum-and-minimum-value-in-an-array-clip-and-optimization-tests","status":"publish","type":"page","link":"https:\/\/www.2diabolos.com\/blog\/python-and-spyder\/force-maximum-and-minimum-value-in-an-array-clip-and-optimization-tests\/","title":{"rendered":"force maximum and minimum value in an array (clip and optimization tests)"},"content":{"rendered":"<div class=\"columns\"><p>you have an multi-dimensional array (we test with 3D here, but it could be \u00ab\u00a0nD\u00a0\u00bb), and you don&rsquo;t want value to be bigger than a maximum, or smaller than a minimum.<br \/>\nI found (and tested) 3 different ways using NumPy:<\/p>\n<ol>\n<li>np.choose (with a trick in building choice matrix)<\/li>\n<li>use indexing to change directly the value for the \u00ab\u00a0bad\u00a0\u00bb values<\/li>\n<li>numpy.clip() (yes, I&rsquo;ve seen that it existed after finding the 2 previous)<\/li>\n<\/ol>\n<p>so here is the Python code for testing the speed<\/p>\n<p>[pastacode lang=\u00a0\u00bbpython\u00a0\u00bb message=\u00a0\u00bbclip_optimizer_tester\u00a0\u00bb highlight=\u00a0\u00bbnp.clip\u00a0\u00bb provider=\u00a0\u00bbmanual\u00a0\u00bb]<\/p>\n<pre><code>import numpy as np\r\nfrom time import time\r\n\r\nt1=t2=t3=0\r\nfor i in range(100):\r\na=np.random.rand(1000,1000,10)\r\ntimeStart=time()\r\na1=np.choose((a&lt;.25)+2*(a&gt;.75),(a,.25,.75))\r\nt1+=time()-timeStart\r\ntimeStart=time()\r\na2=a\r\na2[a&lt;.25]=.25\r\na2[a&gt;.75]=.75\r\nt2+=time()-timeStart\r\ntimeStart=time()\r\na3=np.clip(a,.25,.75)\r\nt3+=time()-timeStart\r\nif not np.all(a1==a2) :\r\nprint &#34;not egal!&#34;\r\nif not np.all(a1==a3) :\r\nprint &#34;not egal!&#34;\r\n\r\nprint &#34;time &#039;choose&#039;&#34;,t1,\r\nprint &#34;time &#039;indexing&#039;&#34;, t2\r\nprint &#34;time np.clip&#34;,t3<\/code><\/pre>\n<p>[\/pastacode]<\/p>\n<p>guess which one is the fastests ?<\/p>\n<p>np.clip obviously! numpy is not programmed by dummies, but by clever guys \ud83d\ude42<\/p>\n<p>and the factor is good : np.clip takes 4.1s on my computer, indexing is nearly 4 times slower with 15.6s and np.choose is really slow with more than 28s. The ratio is about the same for 10 times smaller matrix.<\/p>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>you have an multi-dimensional array (we test with 3D here, but it could be \u00ab\u00a0nD\u00a0\u00bb), and you don&rsquo;t want value to be bigger than a maximum, or smaller than a minimum. I found (and tested) 3 different ways using NumPy: np.choose (with a trick in building choice matrix) use indexing to change directly the value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":861,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-1673","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1673","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=1673"}],"version-history":[{"count":3,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1673\/revisions"}],"predecessor-version":[{"id":1676,"href":"https:\/\/www.2diabolos.com\/blog\/wp-json\/wp\/v2\/pages\/1673\/revisions\/1676"}],"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=1673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}