Re: [Numpy-discussion] Fancy indexing with masks
2011/9/27 Olivier Delalleau > 2011/9/27 Zbigniew Jędrzejewski-Szmek > >> On 09/22/2011 12:09 PM, Pauli Virtanen wrote: >> > Thu, 22 Sep 2011 08:12:12 +0200, Han Genuit wrote: >> > [clip] >> >> I also noticed that it does strange things when using a list: >> >> >> > c[[True, False, True]] >> >> array([[3, 4, 5], >> >> [0, 1, 2], >> >> [3, 4, 5]]) >> > >> > It casts the list with booleans to an integer array. Probably shouldn't >> > work like that... >> Changing that would require looping over the list first to check if >> everything is an boolean, or maybe just looking at the first few >> elements. Either way pretty ugly. So I guess that the current (slightly >> surprising) behaviour has to stay. >> >> > Ugly implementation is better than ugly behavior IMO. > > -=- Olivier > > It should also be possible to convert the list to a NumPy array before doing the actual indexing, then you won't lose consistency. ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
2011/9/27 Zbigniew Jędrzejewski-Szmek > On 09/22/2011 12:09 PM, Pauli Virtanen wrote: > > Thu, 22 Sep 2011 08:12:12 +0200, Han Genuit wrote: > > [clip] > >> I also noticed that it does strange things when using a list: > >> > > c[[True, False, True]] > >> array([[3, 4, 5], > >> [0, 1, 2], > >> [3, 4, 5]]) > > > > It casts the list with booleans to an integer array. Probably shouldn't > > work like that... > Changing that would require looping over the list first to check if > everything is an boolean, or maybe just looking at the first few > elements. Either way pretty ugly. So I guess that the current (slightly > surprising) behaviour has to stay. > > Ugly implementation is better than ugly behavior IMO. -=- Olivier ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
On 09/22/2011 12:09 PM, Pauli Virtanen wrote: > Thu, 22 Sep 2011 08:12:12 +0200, Han Genuit wrote: > [clip] >> I also noticed that it does strange things when using a list: >> > c[[True, False, True]] >> array([[3, 4, 5], >> [0, 1, 2], >> [3, 4, 5]]) > > It casts the list with booleans to an integer array. Probably shouldn't > work like that... Changing that would require looping over the list first to check if everything is an boolean, or maybe just looking at the first few elements. Either way pretty ugly. So I guess that the current (slightly surprising) behaviour has to stay. Zbyszek ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
Thu, 22 Sep 2011 08:12:12 +0200, Han Genuit wrote: [clip] > I also noticed that it does strange things when using a list: > c[[True, False, True]] > array([[3, 4, 5], >[0, 1, 2], >[3, 4, 5]]) It casts the list with booleans to an integer array. Probably shouldn't work like that... Pauli ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
2011/9/20 Stéfan van der Walt > On Tue, Sep 20, 2011 at 12:43 AM, Robert Kern > wrote: > > If the array is short in a dimension, it gets implicitly continued > > with Falses. You can see this in one dimension: > > [...] > > > I honestly don't know if this is documented or tested anywhere or even > > if this existed in older versions. > > The behaviour is already present in 1.4, so I guess it's too late to > insert a shape check now? > > There already is a shape check present in the development version[1]: >>> a = np.arange(10) >>> b = np.array([False, True, False]) >>> a[b] Traceback (most recent call last): File "", line 1, in ValueError: operands could not be broadcast together with shapes (10) (3) But it does not seem to work on multidimensional arrays: >>> c = np.arange(12).reshape((4,3)) >>> c[b] array([[3, 4, 5]]) I also noticed that it does strange things when using a list: >>> c[[True, False, True]] array([[3, 4, 5], [0, 1, 2], [3, 4, 5]]) Regards, Han [1] See also: http://mail.scipy.org/pipermail/numpy-discussion/2011-July/057870.html ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
On Tue, Sep 20, 2011 at 12:43 AM, Robert Kern wrote: > If the array is short in a dimension, it gets implicitly continued > with Falses. You can see this in one dimension: [...] > I honestly don't know if this is documented or tested anywhere or even > if this existed in older versions. The behaviour is already present in 1.4, so I guess it's too late to insert a shape check now? Regards Stéfan ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Fancy indexing with masks
2011/9/20 Stéfan van der Walt : > Hi all, > > Matthew Brett showed me an interesting code snippet this evening: > > # Construct input data > > In [15]: x > Out[15]: > array([[ 0, 1, 2], > [ 3, 4, 5], > [ 6, 7, 8], > [ 9, 10, 11]]) > > # Fancy indexing with 1D boolean array > > In [16]: x[np.array([True, False, True])] > Out[16]: > array([[0, 1, 2], > [6, 7, 8]]) > > # Fancy indexing with 2D boolean array > > In [17]: x[np.array([[True, False, True]])] > Out[18]: array([0, 2]) > > > I guess it's been a long day, but why does this work at all? > > I expected the first example to break, because the 1D mask does not > match the number of rows in x. In the second example, I expected an > error because the 2D mask was not of the same shape as x. But, oddly, > both work. There's also no attempt at broadcasting indexes. If the array is short in a dimension, it gets implicitly continued with Falses. You can see this in one dimension: [~] |1> x = np.arange(12) [~] |2> x[np.array([True, False, True])] array([0, 2]) I honestly don't know if this is documented or tested anywhere or even if this existed in older versions. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion