[Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-03 Thread Ning Sean
Hi, I want to extract elements of an array (say, a) that are contained in another array (say, b). That is, if a=array([1,1,2,3,3,4]), b=array([1,4]), then I want array([1,1,4]). I did the following but the speed is very slow (maybe because a is very long): c=array([]) for x in b: c=append(c,a[

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-03 Thread josef . pktd
On Wed, Jun 3, 2009 at 8:29 PM, Ning Sean wrote: > Hi, I want to extract elements of an array (say, a) that are contained in > another array (say, b). That is, if a=array([1,1,2,3,3,4]), b=array([1,4]), > then I want array([1,1,4]). > > I did the following but the speed is very slow (maybe because

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-03 Thread Ning Sean
Thanks! Tried it and it is about twice as fast as my approach. -Ning On Wed, Jun 3, 2009 at 7:45 PM, wrote: > On Wed, Jun 3, 2009 at 8:29 PM, Ning Sean wrote: > > Hi, I want to extract elements of an array (say, a) that are contained in > > another array (say, b). That is, if a=array([1,1,2,3,

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
a[(a==b[:,None]).sum(axis=0,dtype=bool)] hth, Alan Isaac ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 8:23 AM, Alan G Isaac wrote: > a[(a==b[:,None]).sum(axis=0,dtype=bool)] this is my preferred way when b is small and has unique elements. if the elements in b are not unique, then be can be replaced by np.unique(b) If b is large this creates a huge intermediate array The a

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
> On Thu, Jun 4, 2009 at 8:23 AM, Alan G Isaac wrote: >> a[(a==b[:,None]).sum(axis=0,dtype=bool)] On 6/4/2009 8:35 AM josef.p...@gmail.com apparently wrote: > If b is large this creates a huge intermediate array True enough, but one could then use fromiter: setb = set(b) itr = (ai for ai in a

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 10:13 AM, Alan G Isaac wrote: >> On Thu, Jun 4, 2009 at 8:23 AM, Alan G Isaac wrote: >>> a[(a==b[:,None]).sum(axis=0,dtype=bool)] > > > On 6/4/2009 8:35 AM josef.p...@gmail.com apparently wrote: >> If b is large this creates a huge intermediate array > > > True enough, but

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
> On Thu, Jun 4, 2009 at 10:13 AM, Alan G Isaac wrote: >> Or if a stable order is not important (I don't >> recall if the OP specified), one could just >> np.intersect1d(a, np.unique(b)) On 6/4/2009 10:50 AM josef.p...@gmail.com apparently wrote: > This requires that also `a` has only unique elem

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 11:12 AM, Alan G Isaac wrote: >> On Thu, Jun 4, 2009 at 10:13 AM, Alan G Isaac wrote: >>> Or if a stable order is not important (I don't >>> recall if the OP specified), one could just >>> np.intersect1d(a, np.unique(b)) > > On 6/4/2009 10:50 AM josef.p...@gmail.com apparen

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
On 6/4/2009 10:50 AM josef.p...@gmail.com apparently wrote: > intersect1d gives set intersection if both arrays have > only unique elements (i.e. are sets). I thought the > naming is pretty clear: > intersect1d(a,b) set intersection if a and b with unique elements > intersect1d_nu(a,b) set

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
Alan G Isaac wrote: > On 6/4/2009 10:50 AM josef.p...@gmail.com apparently wrote: >> intersect1d gives set intersection if both arrays have >> only unique elements (i.e. are sets). I thought the >> naming is pretty clear: > >> intersect1d(a,b) set intersection if a and b with unique elements

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 11:19 AM, Alan G Isaac wrote: > On 6/4/2009 10:50 AM josef.p...@gmail.com apparently wrote: >> intersect1d gives set intersection if both arrays have >> only unique elements (i.e. are sets).  I thought the >> naming is pretty clear: > >> intersect1d(a,b)   set intersection i

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
On 6/4/2009 11:29 AM josef.p...@gmail.com apparently wrote: > intersect1d is the intersection between sets (which are stored as > arrays), just like in the mathematical definition the two sets only > have unique elements Hmmm. OK, I see you and Robert believe this. But it does not match the do

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 12:32 PM, Alan G Isaac wrote: > On 6/4/2009 11:29 AM josef.p...@gmail.com apparently wrote: >> intersect1d  is the intersection between sets (which are stored as >> arrays), just like in the mathematical definition the two sets only >> have unique elements > > Hmmm. OK, I se

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Alan G Isaac
On 6/4/2009 1:27 PM josef.p...@gmail.com apparently wrote: > Note: there are two versions of the docs for np.intersect1d, the > currently published docs which describe the actual behavior (for the > non-unique case), and the new docs on the doc editor > http://docs.scipy.org/numpy/docs/numpy.lib.ar

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 2:58 PM, Alan G Isaac wrote: > On 6/4/2009 1:27 PM josef.p...@gmail.com apparently wrote: >> Note: there are two versions of the docs for np.intersect1d, the >> currently published docs which describe the actual behavior (for the >> non-unique case), and the new docs on the

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Kim Hansen
Concerning the name setmember1d_nu, I personally find it quite verbose and not the name I would expect as a non-insider coming to numpy and not knowing all the names of the more special hidden-away functions and not being a python-wiz either. I think ain(a,b) would be the name I had expected as an

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Gael Varoquaux
On Thu, Jun 04, 2009 at 10:27:11PM +0200, Kim Hansen wrote: > "in(b)" or "in_iterable(b)" method, such that you could do a.in(b) > which would return a boolean array of the same shape as a with > elements true if the equivalent a members were members in the iterable > b. That would really by what

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Anne Archibald
2009/6/4 : > intersect1d should throw a domain error if you give it arrays with > non-unique elements, which is not done for speed reasons It seems to me that this is the basic source of the problem. Perhaps this can be addressed? I realize maintaining compatibility with the current behaviour is

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 4:30 PM, Gael Varoquaux wrote: > On Thu, Jun 04, 2009 at 10:27:11PM +0200, Kim Hansen wrote: >> "in(b)" or "in_iterable(b)" method, such that you could do a.in(b) >> which would return a boolean array of the same shape as a with >> elements true if the equivalent a members w

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Gael Varoquaux
On Thu, Jun 04, 2009 at 04:43:39PM -0400, josef.p...@gmail.com wrote: > Just using "in" might promise more than it does, eg. it works only for > one dimensional arrays, maybe "in1d". With "in", Then 'in_1d' > I found arraysetops because of unique1d, but I didn't figure out what > the subpackage

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Thu, Jun 4, 2009 at 4:52 PM, Gael Varoquaux wrote: > On Thu, Jun 04, 2009 at 04:43:39PM -0400, josef.p...@gmail.com wrote: >> Just using "in" might promise more than it does, eg. it works only for >> one dimensional arrays, maybe "in1d". With "in", > > Then 'in_1d' No, if the breaks in a name

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
josef.p...@gmail.com wrote: > On Thu, Jun 4, 2009 at 2:58 PM, Alan G Isaac wrote: >> On 6/4/2009 1:27 PM josef.p...@gmail.com apparently wrote: >>> Note: there are two versions of the docs for np.intersect1d, the >>> currently published docs which describe the actual behavior (for the >>> non-uniq

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
Kim Hansen wrote: > Concerning the name setmember1d_nu, I personally find it quite verbose > and not the name I would expect as a non-insider coming to numpy and > not knowing all the names of the more special hidden-away functions > and not being a python-wiz either. To explain the naming: those

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
Anne Archibald wrote: > 2009/6/4 : > >> intersect1d should throw a domain error if you give it arrays with >> non-unique elements, which is not done for speed reasons > > It seems to me that this is the basic source of the problem. Perhaps > this can be addressed? I realize maintaining compatibi

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
josef.p...@gmail.com wrote: > On Thu, Jun 4, 2009 at 4:30 PM, Gael Varoquaux > wrote: >> On Thu, Jun 04, 2009 at 10:27:11PM +0200, Kim Hansen wrote: >>> "in(b)" or "in_iterable(b)" method, such that you could do a.in(b) >>> which would return a boolean array of the same shape as a with >>> element

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread josef . pktd
On Fri, Jun 5, 2009 at 1:48 AM, Robert Cimrman wrote: > josef.p...@gmail.com wrote: >> On Thu, Jun 4, 2009 at 4:30 PM, Gael Varoquaux >> wrote: >>> On Thu, Jun 04, 2009 at 10:27:11PM +0200, Kim Hansen wrote: "in(b)" or "in_iterable(b)" method, such that you could do a.in(b) which would

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-04 Thread Robert Cimrman
josef.p...@gmail.com wrote: > On Fri, Jun 5, 2009 at 1:48 AM, Robert Cimrman wrote: >> josef.p...@gmail.com wrote: >>> On Thu, Jun 4, 2009 at 4:30 PM, Gael Varoquaux >>> wrote: On Thu, Jun 04, 2009 at 10:27:11PM +0200, Kim Hansen wrote: > "in(b)" or "in_iterable(b)" method, such that you

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-05 Thread David Warde-Farley
On 4-Jun-09, at 4:38 PM, Anne Archibald wrote: > It seems to me that this is the basic source of the problem. Perhaps > this can be addressed? I realize maintaining compatibility with the > current behaviour is necessary, so how about a multistage deprecation: > > 1. add a keyword argument to inte

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-06 Thread Neil Crighton
Robert Cimrman ntc.zcu.cz> writes: > Anne Archibald wrote: > > > 1. add a keyword argument to intersect1d "assume_unique"; if it is not > > present, check for uniqueness and emit a warning if not unique > > 2. change the warning to an exception > > Optionally: > > 3. change the meaning of the fun

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-06 Thread josef . pktd
On Sat, Jun 6, 2009 at 4:42 AM, Neil Crighton wrote: > Robert Cimrman ntc.zcu.cz> writes: > >> Anne Archibald wrote: >> >> > 1. add a keyword argument to intersect1d "assume_unique"; if it is not >> > present, check for uniqueness and emit a warning if not unique >> > 2. change the warning to an

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-08 Thread Robert Cimrman
Hi Josef, thanks for the summary! I am responding below, later I will make an enhancement ticket. josef.p...@gmail.com wrote: > On Sat, Jun 6, 2009 at 4:42 AM, Neil Crighton wrote: >> Robert Cimrman ntc.zcu.cz> writes: >> >>> Anne Archibald wrote: >>> 1. add a keyword argument to intersec

Re: [Numpy-discussion] extract elements of an array that are contained in another array?

2009-06-08 Thread Robert Cimrman
Robert Cimrman wrote: > Hi Josef, > > thanks for the summary! I am responding below, later I will make an > enhancement ticket. Done, see http://projects.scipy.org/numpy/ticket/1133 r. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http:/