
Hi Numpy discussions Quite often I find myself wanting to generate a boolean mask for fancy slicing of some array, where the mask itself is generated by checking if its value has one of several relevant values (corresponding to states) So at the the element level thsi corresponds to checking if element in iterable But I can't use the in operator on a numpy array: In [1]: test = arange(5) In [2]: states = [0, 2] In [3]: mask = test in states --------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\Documents and Settings\kha\<ipython console> in <module>() ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() I can however make my own utility function which works effectively the same way by iterating through the states In [4]: for i, state in enumerate(states): ...: if i == 0: ...: result = test == state ...: else: ...: result |= test == state ...: ...: In [5]: result Out[5]: array([ True, False, True, False, False], dtype=bool) However, I would have thought such an "array.is_in()" utility function was already available in the numpy package? But I can't find it, and I am curious to hear if it is there or if it just available in another form which I have simply overlooked. If it is not there I think it could be a nice extra utility funtion for the ndarray object. --Slaunger

On Wed, Feb 25, 2009 at 7:28 AM, Kim Hansen <slaunger@gmail.com> wrote:
Hi Numpy discussions Quite often I find myself wanting to generate a boolean mask for fancy slicing of some array, where the mask itself is generated by checking if its value has one of several relevant values (corresponding to states) So at the the element level thsi corresponds to checking if element in iterable But I can't use the in operator on a numpy array:
In [1]: test = arange(5) In [2]: states = [0, 2] In [3]: mask = test in states --------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\Documents and Settings\kha\<ipython console> in <module>() ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I can however make my own utility function which works effectively the same way by iterating through the states
In [4]: for i, state in enumerate(states): ...: if i == 0: ...: result = test == state ...: else: ...: result |= test == state ...: ...: In [5]: result Out[5]: array([ True, False, True, False, False], dtype=bool)
However, I would have thought such an "array.is_in()" utility function was already available in the numpy package?
But I can't find it, and I am curious to hear if it is there or if it just available in another form which I have simply overlooked.
If it is not there I think it could be a nice extra utility funtion for the ndarray object.
--Slaunger _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
does this help:
np.setmember1d(test,states) array([ True, False, True, False, False], dtype=bool)
Josef

Yes, this is exactly what I was after, only the function name did not ring a bell (I still cannot associate it with something meaningful for my use case). Thanks! -- Slaunger 2009/2/25 <josef.pktd@gmail.com>:
On Wed, Feb 25, 2009 at 7:28 AM, Kim Hansen <slaunger@gmail.com> wrote:
Hi Numpy discussions Quite often I find myself wanting to generate a boolean mask for fancy slicing of some array, where the mask itself is generated by checking if its value has one of several relevant values (corresponding to states) So at the the element level thsi corresponds to checking if element in iterable But I can't use the in operator on a numpy array:
In [1]: test = arange(5) In [2]: states = [0, 2] In [3]: mask = test in states --------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\Documents and Settings\kha\<ipython console> in <module>() ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I can however make my own utility function which works effectively the same way by iterating through the states
In [4]: for i, state in enumerate(states): ...: if i == 0: ...: result = test == state ...: else: ...: result |= test == state ...: ...: In [5]: result Out[5]: array([ True, False, True, False, False], dtype=bool)
However, I would have thought such an "array.is_in()" utility function was already available in the numpy package?
But I can't find it, and I am curious to hear if it is there or if it just available in another form which I have simply overlooked.
If it is not there I think it could be a nice extra utility funtion for the ndarray object.
--Slaunger _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
does this help:
np.setmember1d(test,states) array([ True, False, True, False, False], dtype=bool)
Josef _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

On Wed, Feb 25, 2009 at 9:02 AM, Kim Hansen <slaunger@gmail.com> wrote:
Yes, this is exactly what I was after, only the function name did not ring a bell (I still cannot associate it with something meaningful for my use case). Thanks!
-- Slaunger
I just looked under "set routines" in the help file. I really like the speed of the windows help file. Josef

I just looked under "set routines" in the help file. I really like the speed of the windows help file.
Is there a Numpy windows help file? Cool! But where is it? I can't find it in my numpy 1.2.1 installation?!? I like the Python 2.5 Windows help file too and I agree it is a fast and efficient way to find what you need, fast. --Slaunger

On Wed, Feb 25, 2009 at 1:37 PM, Kim Hansen <slaunger@gmail.com> wrote:
I just looked under "set routines" in the help file. I really like the speed of the windows help file.
Is there a Numpy windows help file?
Cool!
But where is it? I can't find it in my numpy 1.2.1 installation?!?
I like the Python 2.5 Windows help file too and I agree it is a fast and efficient way to find what you need, fast.
--Slaunger
You can download them from here: http://docs.scipy.org/doc/ But since the docs are continuously improved, it is also worth to check the online documentation if the help file is not very informative about something. Josef

Hi again It turned out not to be quite good enough as is, as it requires unique values for both arrays. Whereas this is often true for the second argument, it is never true for the first argument in my use case, and I struggled with that for some time until i realized I could use unique1d with the rever_index optional parameter set True def ismember(totest, members) """ A setmember1d, which works for totest arrays with duplicate values """ uniques_in_test, rev_idx = unique1d(totest, return_inverse=True) uniques_in_members_mask = setmember1d(uniques_in_test, members) # Use this instead is members is not unique # uniques_in_members_mask = setmember1d(uniques_in_test, unique1d(members)) return uniques_in_members_mask[rev_idx] I saw someone else providing an alternative implementation of this, which was longer and included a loop. I do not know which is the most efficient one, but I understand this one better. -- Slaunger 2009/2/25 <josef.pktd@gmail.com>:
On Wed, Feb 25, 2009 at 7:28 AM, Kim Hansen <slaunger@gmail.com> wrote:
Hi Numpy discussions Quite often I find myself wanting to generate a boolean mask for fancy slicing of some array, where the mask itself is generated by checking if its value has one of several relevant values (corresponding to states) So at the the element level thsi corresponds to checking if element in iterable But I can't use the in operator on a numpy array:
In [1]: test = arange(5) In [2]: states = [0, 2] In [3]: mask = test in states --------------------------------------------------------------------------- ValueError Traceback (most recent call last) C:\Documents and Settings\kha\<ipython console> in <module>() ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I can however make my own utility function which works effectively the same way by iterating through the states
In [4]: for i, state in enumerate(states): ...: if i == 0: ...: result = test == state ...: else: ...: result |= test == state ...: ...: In [5]: result Out[5]: array([ True, False, True, False, False], dtype=bool)
However, I would have thought such an "array.is_in()" utility function was already available in the numpy package?
But I can't find it, and I am curious to hear if it is there or if it just available in another form which I have simply overlooked.
If it is not there I think it could be a nice extra utility funtion for the ndarray object.
--Slaunger _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
does this help:
np.setmember1d(test,states) array([ True, False, True, False, False], dtype=bool)
Josef _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

Kim Hansen wrote:
Hi again
It turned out not to be quite good enough as is, as it requires unique values for both arrays. Whereas this is often true for the second argument, it is never true for the first argument in my use case, and I struggled with that for some time until i realized I could use unique1d with the rever_index optional parameter set True
def ismember(totest, members) """ A setmember1d, which works for totest arrays with duplicate values """ uniques_in_test, rev_idx = unique1d(totest, return_inverse=True) uniques_in_members_mask = setmember1d(uniques_in_test, members) # Use this instead is members is not unique # uniques_in_members_mask = setmember1d(uniques_in_test, unique1d(members)) return uniques_in_members_mask[rev_idx]
I saw someone else providing an alternative implementation of this, which was longer and included a loop. I do not know which is the most efficient one, but I understand this one better.
-- Slaunger
I have added your implementation to http://projects.scipy.org/numpy/ticket/1036 - is it ok with you to add the function eventually into arraysetops.py, under the numpy (BSD) license? cheers, r.

2009/3/5 Robert Cimrman <cimrman3@ntc.zcu.cz>: I have added your implementation to http://projects.scipy.org/numpy/ticket/1036 - is it ok with you to add the function eventually into arraysetops.py, under the numpy (BSD) license?
cheers, r.
Yes, that would be fine with me. In fact that would be an honor! There is some formatting issue in the code you copied into the ticket... Cheers, Kim

Kim Hansen wrote:
2009/3/5 Robert Cimrman <cimrman3@ntc.zcu.cz>: I have added your implementation to http://projects.scipy.org/numpy/ticket/1036 - is it ok with you to add the function eventually into arraysetops.py, under the numpy (BSD) license?
cheers, r.
Yes, that would be fine with me. In fact that would be an honor! There is some formatting issue in the code you copied into the ticket... Cheers, Kim
Great! It's a nice use case for return_inverse=True in unique1d(). I have fixed the formatting, but cannot remove the previous comment. r.
participants (3)
-
josef.pktd@gmail.com
-
Kim Hansen
-
Robert Cimrman