2

I'm trying to use the intersect1d function with a view on a 2D array to find the intersection of it's first column with another 1D array, maintaining its 2nd column in the end result. However I'm stuck trying to construct the view.

Example input:

a1 = np.array([[1,2],[3,4],[4,9]], dtype=np.uint32)
a2 = np.array([8,3,8,1,0,9,3,2], dtype=np.uint32)

The desired result is:

[[1,2],[3,4]]
1
  • The documentation of np.intersect1d does not tell anything about multidimensional arrays (although it still returns a result, without raising any error or depreciation warnings...). However this is not the result you expect. Commented Feb 26, 2016 at 15:42

1 Answer 1

3

Ideally, you would like to have the rows indices or a mask of intersecting ones. Now np.intersect1d won't actually give you either. To solve it, you can use np.in1d to get the mask of intersecting rows. Thus, indexing with it would be your desired output, like so -

a1[np.in1d(a1[:,0],a2)]

Sample run -

In [15]: a1
Out[15]: 
array([[1, 2],
       [3, 4],
       [4, 9]], dtype=uint32)

In [16]: a2
Out[16]: array([8, 3, 8, 1, 0, 9, 3, 2], dtype=uint32)

In [17]: np.in1d(a1[:,0],a2) # Intersecting rows mask for a1
Out[17]: array([ True,  True, False], dtype=bool)

In [18]: a1[np.in1d(a1[:,0],a2)]
Out[18]: 
array([[1, 2],
       [3, 4]], dtype=uint32)
Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thanks for your suggestion. This gives me the following error on a bigger dataset: IndexError: too many indices
@tdma So, in your bigger case - Is a1 a 2D array and a2 a 1D array?
Affirmative, exactly the same structure just more rows (or pairs if you want) in a1. It starts failing after about 1 million pairs/rows in a1.
@tdma Well that sounds puzzling to me as well. It could complain about memory error for really huge datasets, but that indexing error doesn't make sense to me. Also, please make sure the datatype of these arrays are not Object.
Solved, didn't know that .append flattens the array by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.