Compare two arrays in numpy | Data Science tutorial | codin india

 Hey Friends! In today's post, we will learn how to compare two arrays in Numpy.

For comparison in Numpy, we have two methods.

  • ==
  • comparison operator or NumPy comparison operator
Let us understand both methods one by one.

1. Using == operator

We can do element to element-wise comparison using == operator. 

Syntax:- array1 == array2

Let us undestand it by Code:-



Explanation:-

In the above code, we created two arrays and then compare both of them with == operator.

  • imported NumPy as np
  • arr1 and arr2 are arrays
  • print the result of the comparison
OutPut Explanation:-

It will compare both arrays element-wise i.e one element from one array to one element of another element and print the output. In the above example, it compared both arrays and gives output element-wise.


2. Using comparison operator

We can compare array using a comparison operator also. It will also compare arrays element-wise and provide output as True or False. 

We have standard Python comparison operators to compare arrays and Numpy operators to compare arrays. Both will provide the same result. So we will only focus on NumPy Comparison Operators.

Numpy provides us operators are given below:-

  • greater
  • greater_equal
  • less
  • less_equal
  • equal
  • array_equal
  • not_equal
Let us understand it one by one.

1. Greater:-

NumPy provides us greater operator which will check if numbers in array1 is greater than array2 or not. 

It will compare one by one and gives output as True or False.

Let us understand it by Code:-

greater in numpy


It gives out as False False True because 4 is greater than 3.


2. Greater than or Equal 

It will check for both greater than or equal to. If any of one condition is True it will give output as True.

Let us understand it by code:-

greater or equal in numpy

It will give output as all True because all the value from arr1 is greater of equal to values in arr2.


3. Less or Equal:-

If you want to check for value that is lesser than array2, you can use np.less().

Let us understand it by code:-


It will give all the output as false because all the elements in arr1 is equalt or greater than arr2 so thats by it gives outputs as all false.


4. Less or equal

you can also check for value if elements in array1 is less or equal to elements in array2. If the result is equal or less than it gives output as True else false.

Let us understand it by code:-


It gives first two output as True because 1==1 and 2==2 and 4>3 but we are looking for 4<3. So thats why it gaves false as output.


5. Equal

It will check both are equal or not. If are equal it return True else False same as == operator.

Code:-



6.  Not Equal:- 

It will check for the value and return the output as True for that which is not equal and for equal result it gives output as False. 

Let us understand it by code:-


7. Equal array:-

It will check for all elements as one and result in a single True or False. If the all result is True then the output is True. If there is any False in the resultant array the output is false.

Let us understand it one by one:-



it gives output as False because 4>3 but it looks for equal or same. But 4 is not equal to 3 and gives False so thats why if there is any result is false it gives output as False.


I hope you find our post great. Please Subscribe to our Youtube channel for more updates.


Codin India - India Toward Digitalization

Comments