[Numpy-discussion] intersect1d for N input arrays

Robert Cimrman cimrman3 at ntc.zcu.cz
Fri Oct 16 03:56:28 EDT 2009


Hi Martin,

thanks for your ideas and contribution.

A few notes: I would let intersect1d as it is, and created a new function with another name for that (any proposals?). Considering that most of arraysetops functions are based on sort, and in particular here that an intersection array is (usually) smaller than each of the input arrays, it might be better just to call intersect1d repeatedly for each array and the result of the previous call, accumulating the intersection.

r.

Martin Spacek wrote:
> I have a list of many arrays (in my case each is unique, ie has no repeated
> elements), and I'd like to extract the intersection of all of them, all in one
> go. I'm running numpy 1.3.0, but looking at today's rev of numpy.lib.arraysetops
> (http://svn.scipy.org/svn/numpy/trunk/numpy/lib/arraysetops.py), I see
> intersect1d has changed. Just a note: the example used in the docstring implies
> that the two arrays need to be the same length, which isn't the case. Maybe it
> would be good to change the example to two arrays of different lengths.
> 
> intersect1d takes exactly 2 arrays. I've modified it a little to take the
> intersection of any number of 1D arrays (of any length), in a list or tuple. It
> seems to work fine, but could use more testing. Here it is with most of the docs
> stripped. Feel free to use it, although I suppose for symmetry, many of the
> other functions in arraysetops.py would also have to be modified to work with N
> arrays:
> 
> 
> def intersect1d(arrays, assume_unique=False):
>     """Find the intersection of any number of 1D arrays.
>     Return the sorted, unique values that are in all of the input arrays.
>     Adapted from numpy.lib.arraysetops.intersect1d"""
>     N = len(arrays)
>     arrays = list(arrays) # allow assignment
>     if not assume_unique:
>         for i, arr in enumerate(arrays):
>             arrays[i] = np.unique(arr)
>     aux = np.concatenate(arrays) # one long 1D array
>     aux.sort() # sorted
>     shift = N-1
>     return aux[aux[shift:] == aux[:-shift]]
> 
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 
> 




More information about the NumPy-Discussion mailing list