• notice
  • Congratulations on the launch of the Sought Tech site

How to find the intersection between two Numpy arrays?

In this problem, we will find the intersection between two numpy arrays. The intersection of two arrays is an array that has elements in common in both original arrays

algorithm


Step  1 :  Import  numpy. 
Step  2 :  Define  two  numpy  arrays. 
Step  3 :  Find  intersection  between  the  arrays  using  the  numpy.intersect1d()  function. 
Step  4 :  Print  the  array  of  intersecting  elements.


sample code


import numpy as np

array_1 = np.array([1,2,3,4,5])
print( "Array 1:\n" , array_1)

array_2 = np.array([2,4,6,8,10])
print( "\nArray 2:\n" , array_2)

intersection = np.intersect1d(array_1, array_2)
print( "\nThe intersection between the two arrays is:\n" , intersection)

output result

Array 1:
  [1 2 3 4 5 ]
Array 2:
  [2 4 6 8 10 ]
The intersection between the two arrays is:
  [twenty four ]


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+