๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v2.5 Manual
>>> 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]))
๐ŸŒ
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 โ€บ doc โ€บ 2.3 โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v2.3 Manual
>>> 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]))
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v2.6.dev0 Manual
>>> 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]))
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v2.2 Manual
>>> 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]))
๐ŸŒ
Studyopedia
studyopedia.com โ€บ home โ€บ intersection of numpy arrays
Intersection of Numpy Arrays - Studyopedia
March 30, 2026 - We will find the intersection between ... print("\nIterating array2...") for a in n2: print(a) # Find intersection using the intersect1d() method resarr = np.intersect1d(n1, n2) print("\nIntersection = \n", resarr)...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 27967914 โ€บ efficient-way-to-compute-intersecting-values-between-two-numpy-arrays
python - Efficient way to compute intersecting values between two numpy arrays - Stack Overflow
I have a bottleneck in my program which is caused by the following: A = numpy.array([10,4,6,7,1,5,3,4,24,1,1,9,10,10,18]) B = numpy.array([1,4,5,6,7,8,9]) C = numpy.array([i for i in A if i in B]...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ numpy โ€บ python numpy intersection
NumPy Intersection of Two Arrays | Delft Stack
March 11, 2025 - How do I handle duplicates in my arrays? numpy.intersect1d() automatically handles duplicates by returning unique values in the intersection. Is NumPy the only library for array manipulation in Python?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-intersect1d-function-in-python
numpy.intersect1d() function in Python | GeeksforGeeks
May 17, 2020 - numpy.intersect1d() function find the intersection of two arrays and return the sorted, unique values that are in both of the input arrays.
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-numpyintersect1d-function-in-python
What is the numpy.intersect1d() function in Python?
NumPy is a popular library for working with arrays. NumPyโ€™s intersect1d() function returns the intersection between two arrays.
Top answer
1 of 6
48

You could use the boolean array produced by in1d to index an arange. Reversing a so that the indices are different from the values:

>>> a[::-1]
array([10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0])
>>> a = a[::-1]

intersect1d still returns the same values...

>>> numpy.intersect1d(a, b)
array([ 2,  7, 10])

But in1d returns a boolean array:

>>> numpy.in1d(a, b)
array([ True, False, False,  True, False, False, False, False,  True,
       False, False], dtype=bool)

Which can be used to index a range:

>>> numpy.arange(a.shape[0])[numpy.in1d(a, b)]
array([0, 3, 8])
>>> indices = numpy.arange(a.shape[0])[numpy.in1d(a, b)]
>>> a[indices]
array([10,  7,  2])

To simplify the above, though, you could use nonzero -- this is probably the most correct approach, because it returns a tuple of uniform lists of X, Y... coordinates:

>>> numpy.nonzero(numpy.in1d(a, b))
(array([0, 3, 8]),)

Or, equivalently:

>>> numpy.in1d(a, b).nonzero()
(array([0, 3, 8]),)

The result can be used as an index to arrays of the same shape as a with no problems.

>>> a[numpy.nonzero(numpy.in1d(a, b))]
array([10,  7,  2])

But note that under many circumstances, it makes sense just to use the boolean array itself, rather than converting it into a set of non-boolean indices.

Finally, you can also pass the boolean array to argwhere, which produces a slightly differently-shaped result that's not as suitable for indexing, but might be useful for other purposes.

>>> numpy.argwhere(numpy.in1d(a, b))
array([[0],
       [3],
       [8]])
2 of 6
2

If you need to get unique values as given by intersect1d:

import numpy as np

a = np.array([range(11,21), range(11,21)]).reshape(20)
b = np.array([12, 17, 20])
print(np.intersect1d(a,b))
#unique values

inter = np.in1d(a, b)
print(a[inter])
#you can see these values are not unique

indices=np.array(range(len(a)))[inter]
#These are the non-unique indices

_,unique=np.unique(a[inter], return_index=True)

uniqueIndices=indices[unique]
#this grabs the unique indices

print(uniqueIndices)
print(a[uniqueIndices])
#now they are unique as you would get from np.intersect1d()

Output:

[12 17 20]
[12 17 20 12 17 20]
[1 6 9]
[12 17 20]
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v2.0 Manual
>>> 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]))
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.13 โ€บ reference โ€บ generated โ€บ numpy.intersect1d.html
numpy.intersect1d โ€” NumPy v1.13 Manual
June 10, 2017 - >>> 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)
Top answer
1 of 12
50

Stolen directly from https://web.archive.org/web/20111108065352/https://www.cs.mun.ca/~rod/2500/notes/numpy-arrays/numpy-arrays.html

#
# line segment intersection using vectors
# see Computer Graphics by F.S. Hill
#
from numpy import *
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 )
    return (num / denom.astype(float))*db + b1

p1 = array( [0.0, 0.0] )
p2 = array( [1.0, 0.0] )

p3 = array( [4.0, -5.0] )
p4 = array( [4.0, 2.0] )

print seg_intersect( p1,p2, p3,p4)

p1 = array( [2.0, 2.0] )
p2 = array( [4.0, 3.0] )

p3 = array( [6.0, 0.0] )
p4 = array( [6.0, 3.0] )

print seg_intersect( p1,p2, p3,p4)
2 of 12
33
import numpy as np

def get_intersect(a1, a2, b1, b2):
    """ 
    Returns the point of intersection of the lines passing through a2,a1 and b2,b1.
    a1: [x, y] a point on the first line
    a2: [x, y] another point on the first line
    b1: [x, y] a point on the second line
    b2: [x, y] another point on the second line
    """
    s = np.vstack([a1,a2,b1,b2])        # s for stacked
    h = np.hstack((s, np.ones((4, 1)))) # h for homogeneous
    l1 = np.cross(h[0], h[1])           # get first line
    l2 = np.cross(h[2], h[3])           # get second line
    x, y, z = np.cross(l1, l2)          # point of intersection
    if z == 0:                          # lines are parallel
        return (float('inf'), float('inf'))
    return (x/z, y/z)

if __name__ == "__main__":
    print get_intersect((0, 1), (0, 2), (1, 10), (1, 9))  # parallel  lines
    print get_intersect((0, 1), (0, 2), (1, 10), (2, 10)) # vertical and horizontal lines
    print get_intersect((0, 1), (1, 2), (0, 10), (1, 9))  # another line for fun

Explanation

Note that the equation of a line is ax+by+c=0. So if a point is on this line, then it is a solution to (a,b,c).(x,y,1)=0 (. is the dot product)

let l1=(a1,b1,c1), l2=(a2,b2,c2) be two lines and p1=(x1,y1,1), p2=(x2,y2,1) be two points.


Finding the line passing through two points:

let t=p1xp2 (the cross product of two points) be a vector representing a line.

We know that p1 is on the line t because t.p1 = (p1xp2).p1=0. We also know that p2 is on t because t.p2 = (p1xp2).p2=0. So t must be the line passing through p1 and p2.

This means that we can get the vector representation of a line by taking the cross product of two points on that line.


Finding the point of intersection:

Now let r=l1xl2 (the cross product of two lines) be a vector representing a point

We know r lies on l1 because r.l1=(l1xl2).l1=0. We also know r lies on l2 because r.l2=(l1xl2).l2=0. So r must be the point of intersection of the lines l1 and l2.

Interestingly, we can find the point of intersection by taking the cross product of two lines.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-find-intersection-between-two-numpy-arrays
Numpy intersect1d() Function
March 16, 2021 - The Numpy intersect1d() function finds the intersection of two arrays. It return's a sorted array of unique elements that are present in both input arrays. This function is useful for identifying common elements between arrays, whether they contain