Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
all elements equal
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Neal Becker  
View profile  
 More options Mar 5 2012, 2:14 pm
From: Neal Becker <ndbeck...@gmail.com>
Date: Mon, 05 Mar 2012 14:14:02 -0500
Local: Mon, Mar 5 2012 2:14 pm
Subject: [Numpy-discussion] all elements equal
What is a simple, efficient way to determine if all elements in an array (in my
case, 1D) are equal?  How about close?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 2:19 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 11:19:44 -0800
Local: Mon, Mar 5 2012 2:19 pm
Subject: Re: [Numpy-discussion] all elements equal

On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker <ndbeck...@gmail.com> wrote:
> What is a simple, efficient way to determine if all elements in an array (in my
> case, 1D) are equal?  How about close?

For the exactly equal case, how about:

I[1] a = np.array([1,1,1,1])
I[2] np.unique(a).size
O[2] 1    # All equal

I[3] a = np.array([1,1,1,2])
I[4] np.unique(a).size
O[4] 2   # All not equal
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neal Becker  
View profile  
 More options Mar 5 2012, 2:24 pm
From: Neal Becker <ndbeck...@gmail.com>
Date: Mon, 05 Mar 2012 14:24:07 -0500
Local: Mon, Mar 5 2012 2:24 pm
Subject: Re: [Numpy-discussion] all elements equal

Keith Goodman wrote:
> On Mon, Mar 5, 2012 at 11:14 AM, Neal Becker <ndbeck...@gmail.com> wrote:
>> What is a simple, efficient way to determine if all elements in an array (in
>> my case, 1D) are equal?  How about close?

> For the exactly equal case, how about:

> I[1] a = np.array([1,1,1,1])
> I[2] np.unique(a).size
> O[2] 1    # All equal

> I[3] a = np.array([1,1,1,2])
> I[4] np.unique(a).size
> O[4] 2   # All not equal

I considered this - just not sure if it's the most efficient

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Zachary Pincus  
View profile  
 More options Mar 5 2012, 2:28 pm
From: Zachary Pincus <zachary.pin...@yale.edu>
Date: Mon, 5 Mar 2012 14:28:41 -0500
Local: Mon, Mar 5 2012 2:28 pm
Subject: Re: [Numpy-discussion] all elements equal
How about the following?
exact: numpy.all(a == a[0])
inexact: numpy.allclose(a, a[0])

On Mar 5, 2012, at 2:19 PM, Keith Goodman wrote:

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 2:29 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 11:29:13 -0800
Local: Mon, Mar 5 2012 2:29 pm
Subject: Re: [Numpy-discussion] all elements equal

Yeah, it is slow:

I[1] a = np.ones(100000)
I[2] timeit np.unique(a).size
1000 loops, best of 3: 1.56 ms per loop
I[3] timeit (a == a[0]).all()
1000 loops, best of 3: 203 us per loop

I think all() short-circuits for bool arrays:

I[4] a[1] = 9
I[5] timeit (a == a[0]).all()
10000 loops, best of 3: 89 us per loop

You could avoid making the bool array by writing a function in cython.
It could grab the first array element and then return False as soon as
it finds an element that is not equal to it. And you could check for
closeness.

Or:

I[8] np.allclose(a, a[0])
O[8] False
I[9] a = np.ones(100000)
I[10] np.allclose(a, a[0])
O[10] True
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Hunter  
View profile  
 More options Mar 5 2012, 2:32 pm
From: John Hunter <jdh2...@gmail.com>
Date: Mon, 5 Mar 2012 13:32:40 -0600
Local: Mon, Mar 5 2012 2:32 pm
Subject: Re: [Numpy-discussion] all elements equal

On Mon, Mar 5, 2012 at 1:29 PM, Keith Goodman <kwgood...@gmail.com> wrote:

> I[8] np.allclose(a, a[0])
> O[8] False
> I[9] a = np.ones(100000)
> I[10] np.allclose(a, a[0])
> O[10] True

One disadvantage of using a[0] as a proxy is that the result depends on the
ordering of a

  (a.max() - a.min()) < epsilon

is an alternative that avoids this.  Another good use case for a minmax
func.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier Delalleau  
View profile  
 More options Mar 5 2012, 2:33 pm
From: Olivier Delalleau <sh...@keba.be>
Date: Mon, 5 Mar 2012 14:33:23 -0500
Local: Mon, Mar 5 2012 2:33 pm
Subject: Re: [Numpy-discussion] all elements equal

Le 5 mars 2012 14:29, Keith Goodman <kwgood...@gmail.com> a écrit :

Looks like the following is even faster:
np.max(a) == np.min(a)

-=- Olivier

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
josef.p...@gmail.com  
View profile  
 More options Mar 5 2012, 2:36 pm
From: josef.p...@gmail.com
Date: Mon, 5 Mar 2012 14:36:44 -0500
Local: Mon, Mar 5 2012 2:36 pm
Subject: Re: [Numpy-discussion] all elements equal

How about numpy.ptp, to follow this line? I would expect it's single
pass, but wouldn't short circuit compared to cython of Keith

Josef

> -=- Olivier

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

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 2:44 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 11:44:38 -0800
Local: Mon, Mar 5 2012 2:44 pm
Subject: Re: [Numpy-discussion] all elements equal

On Mon, Mar 5, 2012 at 11:36 AM,  <josef.p...@gmail.com> wrote:
> How about numpy.ptp, to follow this line? I would expect it's single
> pass, but wouldn't short circuit compared to cython of Keith

I[1] a = np.ones(100000)
I[2] timeit (a == a[0]).all()
1000 loops, best of 3: 203 us per loop
I[3] timeit a.min() == a.max()
10000 loops, best of 3: 106 us per loop
I[4] timeit np.ptp(a)
10000 loops, best of 3: 106 us per loop

I[5] a[1] = 9
I[6] timeit (a == a[0]).all()
10000 loops, best of 3: 89.7 us per loop
I[7] timeit a.min() == a.max()
10000 loops, best of 3: 102 us per loop
I[8] timeit np.ptp(a)
10000 loops, best of 3: 103 us per loop
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Root  
View profile  
 More options Mar 5 2012, 2:52 pm
From: Benjamin Root <ben.r...@ou.edu>
Date: Mon, 5 Mar 2012 13:52:04 -0600
Local: Mon, Mar 5 2012 2:52 pm
Subject: Re: [Numpy-discussion] all elements equal

Another issue to watch out for is if the array is empty.  Technically
speaking, that should be True, but some of the solutions offered so far
would fail in this case.

Ben Root

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 2:56 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 11:56:56 -0800
Local: Mon, Mar 5 2012 2:56 pm
Subject: Re: [Numpy-discussion] all elements equal

On Mon, Mar 5, 2012 at 11:52 AM, Benjamin Root <ben.r...@ou.edu> wrote:
> Another issue to watch out for is if the array is empty.  Technically
> speaking, that should be True, but some of the solutions offered so far
> would fail in this case.

Good point.

For fun, here's the speed of a simple cython allclose:

I[2] a = np.ones(100000)
I[3] timeit a.min() == a.max()
10000 loops, best of 3: 106 us per loop
I[4] timeit allequal(a)
10000 loops, best of 3: 68.9 us per loop

I[5] a[1] = 9
I[6] timeit a.min() == a.max()
10000 loops, best of 3: 102 us per loop
I[7] timeit allequal(a)
1000000 loops, best of 3: 269 ns per loop

where

@cython.boundscheck(False)
@cython.wraparound(False)
def allequal(np.ndarray[np.float64_t, ndim=1] a):
    cdef:
        np.float64_t a0
        Py_ssize_t i, n=a.size
    a0 = a[0]
    for i in range(n):
        if a[i] != a0:
            return False
    return True
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brett Olsen  
View profile  
 More options Mar 5 2012, 3:01 pm
From: Brett Olsen <brett.ol...@gmail.com>
Date: Mon, 5 Mar 2012 14:01:52 -0600
Local: Mon, Mar 5 2012 3:01 pm
Subject: Re: [Numpy-discussion] all elements equal

> Another issue to watch out for is if the array is empty.  Technically
> speaking, that should be True, but some of the solutions offered so far
> would fail in this case.

Similarly, NaNs or Infs could cause problems:  they should signal as
False, but several of the solutions would return True.

~Brett
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neal Becker  
View profile  
 More options Mar 5 2012, 3:06 pm
From: Neal Becker <ndbeck...@gmail.com>
Date: Mon, 05 Mar 2012 15:06:23 -0500
Local: Mon, Mar 5 2012 3:06 pm
Subject: Re: [Numpy-discussion] all elements equal

But doesn't this one fail on empty array?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 3:12 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 12:12:41 -0800
Local: Mon, Mar 5 2012 3:12 pm
Subject: Re: [Numpy-discussion] all elements equal

On Mon, Mar 5, 2012 at 12:06 PM, Neal Becker <ndbeck...@gmail.com> wrote:
> But doesn't this one fail on empty array?

Yes. I'm optimizing for fun, not for corner cases. This should work
for size zero and NaNs:

@cython.boundscheck(False)
@cython.wraparound(False)
def allequal(np.ndarray[np.float64_t, ndim=1] a):
    cdef:
        np.float64_t a0
        Py_ssize_t i, n=a.size
    if n == 0:
        return False # Or would you like True?
    a0 = a[0]
    for i in range(n):
        if a[i] != a0:
            return False
    return True
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Goodman  
View profile  
 More options Mar 5 2012, 3:30 pm
From: Keith Goodman <kwgood...@gmail.com>
Date: Mon, 5 Mar 2012 12:30:05 -0800
Local: Mon, Mar 5 2012 3:30 pm
Subject: Re: [Numpy-discussion] all elements equal

Sorry for all the posts. I'll go back to being quiet. Seems like
np.allclose returns True for empty arrays:

I[2] a = np.array([])
I[3] np.allclose(np.array([]), np.array([]))
O[3] True

The original allequal cython code did the same:

I[4] allequal(a)
O[4] True
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discuss...@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »