Convert both arrays to pandas dataframes:

df1 = pd.DataFrame({"x" : x1, "y" : y1})).reset_index()

merge them:

result = pd.merge(df1, df2, left_on=["x","y"], right_on=["x","y"])
#   index_x  x  y  index_y
#0        0  1  5        0
#1        2  3  3        8
#2        3  4  2        5

and get the indexes:

result[["index_x","index_y"]]
#   index_x  index_y
#0        0        0
#1        2        8
#2        3        5
Answer from DYZ on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.5 Manual
If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.2 Manual
If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple.
🌐
GitHub
gist.github.com › Robaina › b742f44f489a07cd26b49222f6063ef7
Python function to find the intersection between two 2D numpy arrays, i.e. intersection of rows · GitHub
Python function to find the intersection between two 2D numpy arrays, i.e. intersection of rows ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Educative
educative.io › answers › what-is-the-numpyintersect1d-function-in-python
What is the numpy.intersect1d() function in Python?
The function optionally returns two additional arrays, which contain the indices of intersection elements in the input arrays. Each of these two optionally returned arrays represents one input array. Note: The optional arrays are only returned when the return_indices input argument has been set to True. ... Line 1: We import numpy as np.
Find elsewhere
🌐
Pydocs
pydocs.github.io › p › numpy › 1.22.4 › api › numpy.intersect1d.html
intersect1d
>>> x = np.array([1, 1, 2, 3, 4]) ... y = np.array([2, 1, 4, 6]) ... xy, x_ind, y_ind = np.intersect1d(x, y, return_indices=True) ... x_ind, y_ind (array([0, 2, 4]), array([1, 0, 2])) >>> xy, x[x_ind], y[y_ind] (array([1, 2, 4]), array([1, 2, 4]), array([1, 2, 4])) See : The following pages refer to to this document either explicitly or contain code examples using this. numpy.ma.extras.intersect1d
🌐
3D Slicer
discourse.slicer.org › support
Find the intersection of two models - Support - 3D Slicer Community
November 20, 2022 - Hi there, How can i find the points in which two models (a sort of cylinder and a bar) intersect one another? i’ve tried messing up with numpy trying to search for intersections in data arrays but that doesn’t seem the…
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.0 Manual
If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.0 › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v1.10 Manual
>>> from functools import reduce >>> reduce(np.intersect1d, ([1, 3, 4, 3], [3, 1, 2, 1], [6, 3, 4, 2])) array([3])
🌐
GitHub
gist.github.com › hellpanderrr › a6c30179f64bb1b13b85
Python find intersection of two vectors using matplotlib and numpy · GitHub
from numpy import dot,array,empty_like from matplotlib.path import Path def make_path(x1,y1,x2,y2): return Path([[x1,y1],[x1,y2],[x2,y2],[x2,y1]]) def perp( a ) : b = empty_like(a) b[0] = -a[1] b[1] = a[0] return b # line segment a given by endpoints a1, a2 # line segment b given by endpoints b1, b2 # return def seg_intersect(a1,a2, b1,b2) : da = a2-a1 db = b2-b1 dp = a1-b1 dap = perp(da) denom = dot( dap, db) num = dot( dap, dp ) x3 = ((num / denom.astype(float))*db + b1)[0] y3 = ((num / denom.astype(float))*db + b1)[1] p1 = make_path(a1[0],a1[1],a2[0],a2[1]) p2 = make_path(b1[0],b1[1],b2[0],b2[1]) if p1.contains_point([x3,y3]) and p2.contains_point([x3,y3]): return x3,y3 else: return False p1 = array( [2.0, 1.0] ) p2 = array( [7.0, 3.0] ) p3 = array( [2.0, 5.0] ) p4 = array( [5.0, 2.0] ) print seg_intersect( p1,p2, p3,p4) Copy link ·
Top answer
1 of 6
4

This should do it:

In [11]:

def f(arrA, arrB):
    return not set(map(tuple, arrA)).isdisjoint(map(tuple, arrB))
In [12]:

f(A, B)
Out[12]:
True
In [13]:

f(A, C)
Out[13]:
False
In [14]:

f(B, C)
Out[14]:
False

To find intersection? OK, set sounds like a logical choice. But numpy.array or list are not hashable? OK, convert them to tuple. That is the idea.

A numpy way of doing involves very unreadable boardcasting:

In [34]:

(A[...,np.newaxis]==B[...,np.newaxis].T).all(1)
Out[34]:
array([[False, False],
       [ True, False],
       [False, False]], dtype=bool)
In [36]:

(A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
Out[36]:
True

Some timeit result:

In [38]:
#Dan's method
%timeit set_comp(A,B)
10000 loops, best of 3: 34.1 µs per loop
In [39]:
#Avoiding lambda will speed things up
%timeit f(A,B)
10000 loops, best of 3: 23.8 µs per loop
In [40]:
#numpy way probably will be slow, unless the size of the array is very big (my guess)
%timeit (A[...,np.newaxis]==B[...,np.newaxis].T).all(1).any()
10000 loops, best of 3: 49.8 µs per loop

Also the numpy method will be RAM hungry, as A[...,np.newaxis]==B[...,np.newaxis].T step creates a 3D array.

2 of 6
3

Using the same idea outlined here, you could do the following:

def make_1d_view(a):
    a = np.ascontiguousarray(a)
    dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(dt).ravel()

def f(a, b):
    return len(np.intersect1d(make_1d_view(A), make_1d_view(b))) != 0

>>> f(A, B)
True
>>> f(A, C)
False

This doesn't work for floating point types (it will not consider +0.0 and -0.0 the same value), and np.intersect1d uses sorting, so it is has linearithmic, not linear, performance. You may be able to squeeze some performance by replicating the source of np.intersect1d in your code, and instead of checking the length of the return array, calling np.any on the boolean indexing array.

🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_intersection.htm
NumPy - Intersection
In NumPy, the term "intersection" refers to the elements that are common between two or more arrays. NumPy provides a built-in function called numpy.intersect1d() that helps in finding the intersection between two arrays.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.intersect1d.html
numpy.intersect1d — NumPy v2.6.dev0 Manual
If True, the indices which correspond to the intersection of the two arrays are returned. The first instance of a value is used if there are multiple.
🌐
Plain English
python.plainenglish.io › how-to-find-an-intersection-between-two-matrices-easily-using-numpy-30263373b546
How to find an Intersection between two matrices easily using NumPy | by Sameer | Python in Plain English
February 18, 2021 - We also the above symbol as AND. Programatically this becomes very easy to code or to find the intersection between two sets. Using NumPy, we can find the intersection of two matrices in two different ways.