How to compare two arrays in python

In this tutorial, we will learn about How to compare two arrays in python. The two arrays will be equal when is elements are the same. and if the elements are not equal the array will be not. equal. If the two arrays have the same elements but the sequence is not same. Then it will not consider as the same array. Lets learn this by given methods. I hope you guys like it.

How to compare two arrays in python

Advertisements
  1. compare two arrays in python

    to compare two arrays in python numpy.array_equal().In this method by using numpy.array_equal() you can check two array are equal or not. For check this you have to take two array as input and check by numpy.array_equal(). If the both array contains same elements in same sequence than it returns true value and if not than it will give you false. Lets learn this by example: arr1 = np.array([1,2,3,4,5]) arr2 = np.array([1,5,2,3,6]) print(np.array_equal(arr1,arr1)) print(np.array_equal(arr1,arr2)) Output : True False

  2. How to compare two arrays in python

    to compare two arrays in python just Use numpy.allclose().numpy.allclose() is used for to check the two arrays are equal or not. By using it you can check it. It consider the minor difference between two array as a equal array. Here are we will take three arrays as input and if the elements are same or containing minor difference than it will give you true otherwise it will give you false return value. Thus you can check it if they contain minor differences. Lets learn this by given below example:
    import numpy as np arr1 = np.array([1,2,4,6,7]) arr2 = np.array([1,5,2,3,6]) arr3 = np.array([1,5.00001,2,3,6]) print(np.allclose(arr1,arr2)) print(np.allclose(arr3,arr2)) Output : False True

Method 1: numpy.array_equal()

Advertisements

In this method by using numpy.array_equal() you can check two array are equal or not. For check this you have to take two array as input and check by numpy.array_equal(). If the both array contains same elements in same sequence than it returns true value and if not than it will give you false. Lets learn this by example:

arr1 = np.array([1,2,3,4,5])
arr2 = np.array([1,5,2,3,6])
print(np.array_equal(arr1,arr1))
print(np.array_equal(arr1,arr2))

Output :

True
False

Method 2: Use numpy.allclose()

Advertisements

numpy.allclose() is used for to check the two arrays are equal or not. By using it you can check it. It consider the minor difference between two array as a equal array. Here are we will take three arrays as input and if the elements are same or containing minor difference than it will give you true otherwise it will give you false return value. Thus you can check it if they contain minor differences. Lets learn this by given below example:

import numpy as np
arr1 = np.array([1,2,4,6,7])
arr2 = np.array([1,5,2,3,6])
arr3 = np.array([1,5.00001,2,3,6])
print(np.allclose(arr1,arr2))
print(np.allclose(arr3,arr2))

Output :

False
True

Method 3: Use numpy.array_equiv()

Advertisements

numpy.array_equiv() takes two array as an input and returns the true value if both contain same elements with no differenced and give false if it is contain monor or major differences between of them. Lets learn this by given below example:

import numpy as np
arr1 = np.array([1,2,4,6,7])
arr2 = np.array([1,5,2,3,6])
arr3 = np.array([1,5.00001,2,3,6])
print(np.array_equiv(arr1,arr2))
print(np.array_equiv(arr3,arr2))

Output :

False
False

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment