[Numpy-discussion] intersect1d and setmember1d

Zachary Pincus zachary.pincus at yale.edu
Thu Feb 26 13:07:21 EST 2009


Hi,

> intersect1d and setmember1d doesn't give expected results in case  
> there are duplicate values in either array becuase it works by  
> sorting data and substracting previous value. Is there an  
> alternative in numpy to get indices of intersected values.

 From the docstring for setmember1d (and other set operations), you  
are only supposed to pass it arrays with unique values (i.e. arrays  
that represent sets in the mathematical sense):

 >>> print numpy.setmember1d.__doc__
     Return a boolean array set True where first element is in second  
array.

     Boolean array is the shape of `ar1` containing True where the  
elements
     of `ar1` are in `ar2` and False otherwise.

     Use unique1d() to generate arrays with only unique elements to  
use as
     inputs to this function.
     [...]

As stated, use unique1d to generate set-arrays from your input.

On the other hand, intersect1d is supposed to work with repeated  
elements:
 >>> print numpy.intersect1d.__doc__
     Intersection returning repeated or unique elements common to both  
arrays.

     Parameters
     ----------
     ar1,ar2 : array_like
         Input arrays.

     Returns
     -------
     out : ndarray, shape(N,)
         Sorted 1D array of common elements with repeating elements.

     See Also
     --------
     intersect1d_nu : Returns only unique common elements.
     [...]

Do you have an example of intersect1d not working right? If so, what  
version of numpy are you using (print numpy.version.version)?

Zach




On Feb 26, 2009, at 12:48 PM, mudit sharma wrote:

>
> intersect1d and setmember1d doesn't give expected results in case  
> there are duplicate values in either array becuase it works by  
> sorting data and substracting previous value. Is there an  
> alternative in numpy to get indices of intersected values.
>
> In [31]: p nonzero(setmember1d(v1.Id, v2.Id))[0]
> [ 0  1  2  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22  
> 23 24 25                        <-------------- index 2 shouldn't be  
> here look at the data below.
> 26 27 28 29]
>
> In [32]: p v1.Id[:10]
> [ 232.  232.  233.  233.  234.  234.  235.  235.  237.  237.]
>
> In [33]: p v2.Id[:10]
> [ 232.  232.  234.  234.  235.  235.  236.  236.  237.  237.]
>
> In [34]: p setmember1d(v1.Id, v2.Id)
> [ True  True  True False  True  True  True  True  True  True  True   
> True                       <-------------- index 2 shouldn't be True
>  True  True  True  True  True  True  True  True  True  True  True   
> True
>  True  True  True  True  True  True]
>
> In [35]: p setmember1d(v1.Id[:10], v2.Id[:10])
> [ True  True  True False  True  True  True  True  True  True]
>
>
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list