[Numpy-discussion] Does x[True] trigger basic or advanced indexing?

Sebastian Berg sebastian at sipsolutions.net
Thu Dec 14 15:37:38 EST 2017


On Thu, 2017-12-14 at 16:24 +0000, Eric Wieser wrote:
> It sounds like you're using an old version of numpy, where boolean
> scalars were interpreted as integers.
> What version are you using?
> Eric
> 

Indeed, you are maybe using a pre 1.9 version (post 1.9 should at least
have a DeprecationWarning or some such, though you might not notice it
IIRC).
For newer versions you should get boolean indexing, the result of it
may be a bit confusing. It is advanced indexing, basically with False
giving you an empty array (with an extra dimension of size 0) and True
being much like an `np.newaxis`.
It all makes perfect sense if you think of it of a 0-d array
picking....

The same thing is true for example for lists of booleans.

- Sebastian



> On Thu, Dec 14, 2017, 04:27 Joe <solarjoe at posteo.org> wrote:
> > Hello,
> > thanks for you feedback.
> > 
> > Sorry, if thie question is stupid and the case below does not make
> > sense.
> > I am just trying to understand the logic.
> > For
> > 
> > x = np.random.rand(2,3)
> > 
> > x[True]
> > x[(True,)]
> > 
> > or
> > 
> > x[False]
> > x[(False,)]
> > 
> > where True and False are not arrays,
> > it will pick the first or second row.
> > 
> > Is this basic indexing then with one the rules
> > - obj is an integer
> > - obj is a tuple of slice objects and integers.
> > ?
> > 
> > 
> > Am 13.12.2017 21:49 schrieb Eric Wieser:
> > > Increasingly, NumPy does not considers booleans to be integer
> > types,
> > > and indexing is one of these cases.
> > >
> > > So no, it will not be treated as a tuple of integers, but as a 0d
> > mask
> > >
> > > Eric
> > >
> > > On Wed, 13 Dec 2017 at 12:44 Joe <solarjoe at posteo.org> wrote:
> > >
> > >> Hi,
> > >>
> > >> yet another question.
> > >>
> > >> I looked through the indexing rules in the
> > >> documentation but I count not find which one
> > >> applies to x[True] and x[False]
> > >>
> > >> that might e.g result from
> > >>
> > >> import numpy as np
> > >> x = np.array(3)
> > >> x[x>5]
> > >> x[x<1]
> > >> x[True]
> > >> x[False]
> > >>
> > >> x = np.random.rand(2,3)
> > >> x[x>5]
> > >> x[x<1]
> > >> x[True]
> > >> x[False]
> > >>
> > >> I understood that they are equivalent to
> > >>
> > >> x[(False,)]
> > >>
> > >> I tested it and it looks like advanced indexing,
> > >> but I try to unterstand the logic behind this,
> > >> if there is any :)
> > >>
> > >> In x[x<1] the x<1 is a mask and thus I guess it is a
> > >> "tuple with at least one sequence object or ndarray (of data
> > type
> > >> integer or bool)", right?
> > >>
> > >> Or will x[True] trigger basic indexing as it is "a tuple of
> > >> integers"
> > >> because True will be converted to Int?
> > >>
> > >> Cheers,
> > >> Joe
> > >> _______________________________________________
> > >> NumPy-Discussion mailing list
> > >> NumPy-Discussion at python.org
> > >> https://mail.python.org/mailman/listinfo/numpy-discussion [1]
> > >
> > >
> > > Links:
> > > ------
> > > [1] https://mail.python.org/mailman/listinfo/numpy-discussion
> > >
> > > _______________________________________________
> > > NumPy-Discussion mailing list
> > > NumPy-Discussion at python.org
> > > https://mail.python.org/mailman/listinfo/numpy-discussion
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at python.org
> > https://mail.python.org/mailman/listinfo/numpy-discussion
> > 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171214/54401a40/attachment.sig>


More information about the NumPy-Discussion mailing list