Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Nadav Horesh
In python empty sequences are always equivalent to False, and non-empty to 
True. You can use this property or:

if len(b)  0:
   .

  Nadav 


-Original Message-
From: numpy-discussion-boun...@scipy.org on behalf of Shailendra
Sent: Fri 02-Apr-10 06:07
To: numpy-discussion@scipy.org
Subject: [Numpy-discussion] Is this odd?
 
Hi All,
Below is some array behaviour which i think is odd
 a=arange(10)
 a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
(array([], dtype=int32),)
 if not b[0]:
... print 'b[0] is false'
...
b[0] is false

Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
(array([0]),)
 if not b[0]:
... print 'b[0] is false'
...
b[0] is false

Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
(array([9]),)
 if not b[0]:
... print 'b[0] is false'
...

Above case b[0] is non-empty and should be consider true.Which it does.

I don't understand why non-empty array should not be considered true
irrespective to what value they have.
Also, please suggest the best way to differentiate between an empty
array and non-empty array( irrespective to what is inside array).

Thanks,
Shailendra
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

winmail.dat___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Alan G Isaac
On 4/2/2010 8:29 AM, Nadav Horesh wrote:
 In python empty sequences are always equivalent to False, and non-empty to 
 True.



I think that was why the OP objected to this behavior:

  bool(np.array([0]))
 False

Alan Isaac
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Robert Kern
On Thu, Apr 1, 2010 at 22:07, Shailendra shailendra.vi...@gmail.com wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.

We raise an exception for most arrays. We used to evaluate bool(arr)
as True if any element in arr were nonzero. However, people routinely
got confused and thought that the behavior was that it would return
False if any element were zero. It's ambiguous, so we raise an
exception to prevent errors and misconceptions. Empty arrays are very
rare, so we decided that evaluating bool(arr) as True if there were
any elements had very little use case and would still have the
potential to confuse people. As shown below, there is a better, more
explicit way to test for emptiness.

However, we do allow single-element arrays to evaluate to True or
False based on its single element because that is unambiguous. This is
possibly a mistake, but it tends not to cause too many problems. I
don't know why we also allow bool(array([])) to evaluate to False,
too; we probably shouldn't.

 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

a.size  0

-- 
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


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread josef . pktd
On Fri, Apr 2, 2010 at 10:11 AM, Robert Kern robert.k...@gmail.com wrote:
 On Thu, Apr 1, 2010 at 22:07, Shailendra shailendra.vi...@gmail.com wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.

 We raise an exception for most arrays. We used to evaluate bool(arr)
 as True if any element in arr were nonzero. However, people routinely
 got confused and thought that the behavior was that it would return
 False if any element were zero. It's ambiguous, so we raise an
 exception to prevent errors and misconceptions. Empty arrays are very
 rare, so we decided that evaluating bool(arr) as True if there were
 any elements had very little use case and would still have the
 potential to confuse people. As shown below, there is a better, more
 explicit way to test for emptiness.

 However, we do allow single-element arrays to evaluate to True or
 False based on its single element because that is unambiguous. This is
 possibly a mistake, but it tends not to cause too many problems. I
 don't know why we also allow bool(array([])) to evaluate to False,
 too; we probably shouldn't.

 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

 a.size  0

 --
 Robert Kern

with examples:
 bool(0)
False
 bool(1)
True
 bool(np.array([0]))
False
 bool(np.array([1]))
True
 bool([0])
True
 bool([1])
True
 bool(np.array([[0]]))
False
 bool(np.array([[1]]))
True
 np.array([[1]]).size
1

Josef

 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

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Ryan May
On Thu, Apr 1, 2010 at 10:07 PM, Shailendra shailendra.vi...@gmail.com wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.
 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

But by using:

if not b[0]:

You're not considering the array as a whole, you're looking at the
first element, which is giving expected results.  As I'm sure you're
aware, however, you can't simply do:

if not b: # Raises exception

So what you need to do is:

if b.any():

or:

if b.all()

Now for determining empty or not, you'll need to look at len(b) or b.shape

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Robert Kern
On Fri, Apr 2, 2010 at 08:28, Ryan May rma...@gmail.com wrote:
 On Thu, Apr 1, 2010 at 10:07 PM, Shailendra shailendra.vi...@gmail.com 
 wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.
 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

 But by using:

 if not b[0]:

 You're not considering the array as a whole, you're looking at the
 first element, which is giving expected results.

No, b is a tuple containing the array. b[0] is the array itself.

-- 
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


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Shailendra
Thanks everyone for replies/suggestion. It is simple to avoid this
problem. But my point that given the behavior of python this behavior
seems inconsistent. There could other method provided which could
evaluate bool value depending on values stored in the array.

Thanks,
Shailendra

On Fri, Apr 2, 2010 at 10:28 AM, Ryan May rma...@gmail.com wrote:
 On Thu, Apr 1, 2010 at 10:07 PM, Shailendra shailendra.vi...@gmail.com 
 wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.
 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

 But by using:

 if not b[0]:

 You're not considering the array as a whole, you're looking at the
 first element, which is giving expected results.  As I'm sure you're
 aware, however, you can't simply do:

 if not b: # Raises exception

 So what you need to do is:

 if b.any():

 or:

 if b.all()

 Now for determining empty or not, you'll need to look at len(b) or b.shape

 Ryan

 --
 Ryan May
 Graduate Research Assistant
 School of Meteorology
 University of Oklahoma
 ___
 NumPy-Discussion mailing list
 NumPy-Discussion@scipy.org
 http://mail.scipy.org/mailman/listinfo/numpy-discussion

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Is this odd?

2010-04-02 Thread Ryan May
On Fri, Apr 2, 2010 at 8:31 AM, Robert Kern robert.k...@gmail.com wrote:
 On Fri, Apr 2, 2010 at 08:28, Ryan May rma...@gmail.com wrote:
 On Thu, Apr 1, 2010 at 10:07 PM, Shailendra shailendra.vi...@gmail.com 
 wrote:
 Hi All,
 Below is some array behaviour which i think is odd
 a=arange(10)
 a
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
 (array([], dtype=int32),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
 (array([0]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...
 b[0] is false

 Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
 (array([9]),)
 if not b[0]:
 ...     print 'b[0] is false'
 ...

 Above case b[0] is non-empty and should be consider true.Which it does.

 I don't understand why non-empty array should not be considered true
 irrespective to what value they have.
 Also, please suggest the best way to differentiate between an empty
 array and non-empty array( irrespective to what is inside array).

 But by using:

 if not b[0]:

 You're not considering the array as a whole, you're looking at the
 first element, which is giving expected results.

 No, b is a tuple containing the array. b[0] is the array itself.

Wow, that's what I get for trying to read code *before* coffee.  On
the plus side, I now know how nonzero() actually works.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Is this odd?

2010-04-01 Thread Shailendra
Hi All,
Below is some array behaviour which i think is odd
 a=arange(10)
 a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
 b=nonzero(a0)
 b
(array([], dtype=int32),)
 if not b[0]:
... print 'b[0] is false'
...
b[0] is false

Above case the b[0] is empty so it is fine it is considered false

 b=nonzero(a1)
 b
(array([0]),)
 if not b[0]:
... print 'b[0] is false'
...
b[0] is false

Above case b[0] is a non-empty array. Why should this be consider false.

 b=nonzero(a8)
 b
(array([9]),)
 if not b[0]:
... print 'b[0] is false'
...

Above case b[0] is non-empty and should be consider true.Which it does.

I don't understand why non-empty array should not be considered true
irrespective to what value they have.
Also, please suggest the best way to differentiate between an empty
array and non-empty array( irrespective to what is inside array).

Thanks,
Shailendra
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion