<div dir="auto">Bin index is just value floor divided by the bin size.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Dec 18, 2020, 09:59 MartÃn Chalela <<a href="mailto:tinchochalela@gmail.com">tinchochalela@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi all! I was wondering if there is a way around to using np.digitize when dealing with equidistant bins. For example: <br></div><div>bins = np.linspace(0, 1, 20)</div><div><br></div><div>The main problem I encountered is that digitize calls np.searchsorted. This is the correct way, I think, for generic bins, i.e. bins that have different widths. However, in the special, but not uncommon, case of equidistant bins, the searchsorted call can be very expensive and unnecessary. One can perform a simple calculation like the following:</div><div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono","monospace",monospace,"Droid Sans Fallback";font-weight:normal;font-size:14px;line-height:19px;white-space:pre-wrap"><div><span style="color:rgb(212,212,212)"></span><span style="color:rgb(86,156,214)">def</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">digitize_eqbins</span><span style="color:rgb(212,212,212)">(x</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(156,220,254)">bins</span><span style="color:rgb(212,212,212)">):</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(206,145,120)">"""<span><br></span></span></div><div><span style="color:rgb(206,145,120)"><span> Return the indices of the bins to which each value in input array belongs</span>.<br></span></div><div><span style="color:rgb(206,145,120)"> Assumes equidistant bins.</span></div><div><span style="color:rgb(206,145,120)"> """</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(79,193,255)">nbins</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(220,220,170)">len</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(156,220,254)">bins</span><span style="color:rgb(212,212,212)">) - </span><span style="color:rgb(181,206,168)">1</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">d</span><span style="color:rgb(212,212,212)">igit = (</span>nbins<span style="color:rgb(212,212,212)"> * (x</span><span style="color:rgb(212,212,212)"> - </span><span style="color:rgb(156,220,254)">bins</span><span style="color:rgb(212,212,212)">[</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">]) / (</span><span style="color:rgb(156,220,254)">bins</span><span style="color:rgb(212,212,212)">[-</span><span style="color:rgb(181,206,168)">1</span><span style="color:rgb(212,212,212)">] - </span><span style="color:rgb(156,220,254)">bins</span><span style="color:rgb(212,212,212)">[</span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">])).astype(</span><span style="color:rgb(78,201,176)">np</span><span style="color:rgb(212,212,212)">.int)</span></div><div><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(197,134,192)">return</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">digit + 1</span></div></div></div><div><br></div><div>Is there a better way of computing this for equidistant bins?</div><div><br></div><div>Thank you!</div><div>Martin.<br></div></div>
_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@python.org" target="_blank" rel="noreferrer">NumPy-Discussion@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/numpy-discussion" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/numpy-discussion</a><br>
</blockquote></div>