<div dir="ltr"><br><div class="gmail_extra"><div class="gmail_quote">On Fri, Sep 27, 2013 at 5:27 AM, Sebastian Berg <span dir="ltr"><<a href="mailto:sebastian@sipsolutions.net" target="_blank">sebastian@sipsolutions.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
And most importantly, is there any behaviour thing in the index<br>
machinery that is bugging you, which I may have forgotten until now?<br></blockquote><div><br></div><div>I find this behavior of boolean indexing a little bit annoying:</div><div><br></div><div><div>>>> a = np.arange(12).reshape(3, 4)</div>
<div><div><div>>>> a</div><div>array([[ 0,  1,  2,  3],</div><div>       [ 4,  5,  6,  7],</div><div>       [ 8,  9, 10, 11]])</div></div></div><div>>>> row_idx = np.array([True, True, False])<br></div><div>
>>> col_idx = np.array([False, True, True, False])</div><div><br></div><div>This shouldn't work, but it does, because there are the same number of Trues in both indexing arrays. Do we really want this to happen?:</div>
<div>>>> a[row_idx, col_idx]</div><div>array([1, 6])</div><div><br></div><div>This shouldn't work, and it doesn't:<br></div><div>>>> col_idx = np.array([False, True, True, True])<br></div><div>>>> a[row_idx, col_idx]</div>
<div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div><div>ValueError: shape mismatch: objects cannot be broadcast to a single shape</div><div><br></div><div><br>
</div><div>It would be nice if something like this worked, or at least it should raise a different error, because those arrays **can** be broadcast to a single shape:</div><div>>>> a[row_idx[:, np.newaxis], col_idx]<br>
</div><div>Traceback (most recent call last):</div><div>  File "<stdin>", line 1, in <module></div><div>ValueError: shape mismatch: objects cannot be broadcast to a single shape</div><div><br></div><div>
For this there is the following workaround, although it does creation of a fully expanded boolean indexing array, which I was hoping the previous non-working code would avoid:</div><div>>>> a[row_idx[:, np.newaxis] & col_idx]</div>
<div>array([1, 2, 3, 5, 6, 7])</div><div><br></div><div>Jaime</div><div><br></div></div></div></div></div>