Dear all, sorry for this stupid question but I cannot find it in numpy tutorial or google. suppose I have a=np.arange(11). In [32]: a < 8 Out[32]: array([ True, True, True, True, True, True, True, True, False, False, False], dtype=bool) In [34]: a > 4 Out[34]: array([False, False, False, False, False, True, True, True, True, True, True], dtype=bool) how can I have boolean index like 4 < a < 8 np.where(a>4 and a<8);or plainly input "a>4 and a<8" doesn't work. thanks, Chao -- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
On Thu, Oct 13, 2011 at 10:13 AM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Dear all,
sorry for this stupid question but I cannot find it in numpy tutorial or google. suppose I have a=np.arange(11).
In [32]: a < 8 Out[32]: array([ True, True, True, True, True, True, True, True, False, False, False], dtype=bool)
In [34]: a > 4 Out[34]: array([False, False, False, False, False, True, True, True, True, True, True], dtype=bool)
how can I have boolean index like 4 < a < 8 np.where(a>4 and a<8);or plainly input "a>4 and a<8" doesn't work.
thanks,
Chao
I1 a=np.arange(11) I2 a[(a<8) & (a>4)] O2 array([5, 6, 7]) -- Gökhan
On Thu, Oct 13, 2011 at 11:13 AM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Dear all,
sorry for this stupid question but I cannot find it in numpy tutorial or google. suppose I have a=np.arange(11).
In [32]: a < 8 Out[32]: array([ True, True, True, True, True, True, True, True, False, False, False], dtype=bool)
In [34]: a > 4 Out[34]: array([False, False, False, False, False, True, True, True, True, True, True], dtype=bool)
how can I have boolean index like 4 < a < 8 np.where(a>4 and a<8);or plainly input "a>4 and a<8" doesn't work.
thanks,
Chao
Unfortunately, you can't use "and", "or", "not" keywords with boolean arrays because numpy can't overload them. Instead, use the bitwise operators: '&', '|', and '~'. Be careful, though, because of operator precedence is different for bitwise operators than the boolean keywords. I am in the habit of always wrapping my boolean expressions in parentheses, just in case. (a > 4) & (a < 8) is what you want. Note that "a > 4 & a < 8" would be evaluated in a different order -- "4 & a" would be first. I hope that helps! Ben Root
Thanks. I starts to use python do some real data processing and has bunch of questions. Chao 2011/10/13 Benjamin Root <ben.root@ou.edu>
On Thu, Oct 13, 2011 at 11:13 AM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Dear all,
sorry for this stupid question but I cannot find it in numpy tutorial or google. suppose I have a=np.arange(11).
In [32]: a < 8 Out[32]: array([ True, True, True, True, True, True, True, True, False, False, False], dtype=bool)
In [34]: a > 4 Out[34]: array([False, False, False, False, False, True, True, True, True, True, True], dtype=bool)
how can I have boolean index like 4 < a < 8 np.where(a>4 and a<8);or plainly input "a>4 and a<8" doesn't work.
thanks,
Chao
Unfortunately, you can't use "and", "or", "not" keywords with boolean arrays because numpy can't overload them. Instead, use the bitwise operators: '&', '|', and '~'. Be careful, though, because of operator precedence is different for bitwise operators than the boolean keywords. I am in the habit of always wrapping my boolean expressions in parentheses, just in case.
(a > 4) & (a < 8)
is what you want. Note that "a > 4 & a < 8" would be evaluated in a different order -- "4 & a" would be first.
I hope that helps!
Ben Root
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
you could use bitwise comparison with paretheses: In [8]: (a>4)&(a<8) Out[8]: array([False, False, False, False, False, True, True, True, False, False, False], dtype=bool) On Thu, Oct 13, 2011 at 12:13 PM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Dear all,
sorry for this stupid question but I cannot find it in numpy tutorial or google. suppose I have a=np.arange(11).
In [32]: a < 8 Out[32]: array([ True, True, True, True, True, True, True, True, False, False, False], dtype=bool)
In [34]: a > 4 Out[34]: array([False, False, False, False, False, True, True, True, True, True, True], dtype=bool)
how can I have boolean index like 4 < a < 8 np.where(a>4 and a<8);or plainly input "a>4 and a<8" doesn't work.
thanks,
Chao
--
*********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Marc Shivers <marc.shivers <at> gmail.com> writes:
you could use bitwise comparison with paretheses: In [8]: (a>4)&(a<8)Out[8]:
array([False, False, False, False, False, True, True, True, False, False, False], dtype=bool)
For cases like this I find it very useful to define a function between() - e.g. https://bitbucket.org/nhmc/pyserpens/src/4e2cc9b656ae/utilities.py#cl-88 Then you can use between(a, 4, 8) instead of (4 < a) & (a < 8), which I find less readable and more difficult to type. Neil
Thanks. quite useful!! Chao 2011/10/15 Neil <neilcrighton@gmail.com>
Marc Shivers <marc.shivers <at> gmail.com> writes:
you could use bitwise comparison with paretheses: In [8]:
(a>4)&(a<8)Out[8]: array([False, False, False, False, False, True, True, True, False, False, False], dtype=bool)
For cases like this I find it very useful to define a function between() - e.g. https://bitbucket.org/nhmc/pyserpens/src/4e2cc9b656ae/utilities.py#cl-88
Then you can use
between(a, 4, 8)
instead of
(4 < a) & (a < 8),
which I find less readable and more difficult to type.
Neil
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
participants (5)
-
Benjamin Root
-
Chao YUE
-
Gökhan Sever
-
Marc Shivers
-
Neil