<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Apr 10, 2015 at 9:58 AM, Derek Homeier <span dir="ltr"><<a href="mailto:derek@astro.physik.uni-goettingen.de" target="_blank">derek@astro.physik.uni-goettingen.de</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"><span class="">On 10 Apr 2015, at 06:22 pm, Alan G Isaac <<a href="mailto:alan.isaac@gmail.com">alan.isaac@gmail.com</a>> wrote:<br>
<br>
>><br>
>> On Thu, Apr 9, 2015 at 8:41 PM, Derek Homeier <<a href="mailto:derek@astro.physik.uni-goettingen.de">derek@astro.physik.uni-goettingen.de</a> <mailto:<a href="mailto:derek@astro.physik.uni-goettingen.de">derek@astro.physik.uni-goettingen.de</a>>> wrote:<br>
>> a[1:3,1:3]?<br>
>> Can’t be generalised to arbitrary selections of rows,columns, though (e.g. a[1::2,::2] still works…)<br>
><br>
><br>
><br>
> On 4/9/2015 11:26 PM, Alexander Belopolsky wrote:<br>
>> I am interested in the arbitrary selection of rows and columns given by indices or by boolean selectors.<br>
><br>
><br>
><br>
> You mean like this?<br>
> import numpy as np<br>
> a = np.arange(20).reshape((4,5))<br>
> rows = [0,3]<br>
> cols = [1,2,4]<br>
> print a[rows][:,cols]<br>
<br>
</span>This creates a copy, same apparently with np.ix_ - an objection I had cut from the original post…<br>
Compare to<br>
b = a[::2,1::2]<br>
b *= 2<br>
print(a)<br></blockquote><div><br></div><div>Well, the numpy ndarray model requires constant strides along each dimension, so yes, for consistency fancy indexing always makes a copy. I believe Alexander's complaint was not that <font face="monospace, monospace">a[[1, 2]][:, [1, 2]]</font> makes one copy, but that it makes two. Also, with that double fancy indexing approach you cannot assign to the subarray, something that <font face="monospace, monospace">np.ix_</font> does enable:</div><div><br></div><div><div><font face="monospace, monospace">>>> a = np.arange(16).reshape(4, 4)</font></div><div><font face="monospace, monospace">>>> a</font></div><div><font face="monospace, monospace">array([[ 0, 1, 2, 3],</font></div><div><font face="monospace, monospace"> [ 4, 5, 6, 7],</font></div><div><font face="monospace, monospace"> [ 8, 9, 10, 11],</font></div><div><font face="monospace, monospace"> [12, 13, 14, 15]])</font></div><div><font face="monospace, monospace">>>> a[np.ix_([1, 2], [1, 2])] *= 2</font></div><div><font face="monospace, monospace">>>> a</font></div><div><font face="monospace, monospace">array([[ 0, 1, 2, 3],</font></div><div><font face="monospace, monospace"> [ 4, 10, 12, 7],</font></div><div><font face="monospace, monospace"> [ 8, 18, 20, 11],</font></div><div><font face="monospace, monospace"> [12, 13, 14, 15]])</font></div></div><div><br></div><div>Where it gets complicated is when you have an at least 3D array, and you want to orthogonally index the first and last dimensions, while extracting a slice from the middle one. There's no easy answer to that one, and that's where this whole discussion on modifying numpy's indexing starts to get some real traction.</div><div><br></div><div>Jaime</div><div> </div><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>
On 10 Apr 2015, at 02:23 am, Alexander Belopolsky <<a href="mailto:ndarray@mac.com">ndarray@mac.com</a>> wrote:<br>
<br>
> I could do<br>
><br>
> >>> a[[1,2]][:,[1,2]]<br>
<span class="im">> array([[22, 23],<br>
> [32, 33]])<br>
><br>
> but this creates an extra copy.<br>
><br>
> The best solution I can think of involves something like<br>
><br>
> >>> i = np.array([[1,2]])<br>
> >>> a.flat[i + len(a)*i.T]<br>
> array([[22, 23],<br>
> [32, 33]])<br>
><br>
> which is hardly elegant or obvious.<br>
><br>
<br>
</span><div class=""><div class="h5">_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature">(\__/)<br>( O.o)<br>( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.</div>
</div></div>