Check two numpy Arrays are equal or not
In this numpy tutorial, we will check the two numpy arrays are equal or not.
Introduction
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
Method 1 : Using array_equal()
Here, we will use array_equal() method available in numpy module, used to check two arrays are equal or not.
Syntax:
numpy.array_equal(array1,array2)
where,
1. array1 is the first input numpy array
2. array2 is the second input numpy array.
If arrays are equal, it will return True, otherwise False.
Example:
In this example.we will create 3 arrays with 10 integer elements each and check the arrays are equal or not.
#import numpy module
import numpy
#create first numpy array with 10 elements
array_input1=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input1)
#create second numpy array with 10 elements
array_input2=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input2)
#create third numpy array with 10 elements
array_input3=numpy.array([78,45,67,54,43,56,78,90,87,9])
#display array
print(array_input3)
#check if array1 and array2 are equal
print(numpy.array_equal(array_input1,array_input2))
#check if array1 and array3 are equal
print(numpy.array_equal(array_input1,array_input3))
Output:
From the code, we seen that first two arrays are equal and third array is not equal. In the first statement, we are checking array1 and array2 are equal or not. It will return True because they are equal and we are checking first array with third array. It will return False, since they ate not equal.
[10 45 67 54 43 56 78 90 87 9]
[10 45 67 54 43 56 78 90 87 9]
[78 45 67 54 43 56 78 90 87 9]
True
False
Method 2 : Using array_equiv()
Here, we will use array_equiv() method available in numpy module, used to check two arrays are equal or not.
Syntax:
numpy.array_equiv(array1,array2)
where,
1. array1 is the first input numpy array
2. array2 is the second input numpy array.
If arrays are equal, it will return True, otherwise False.
Example:
In this example.we will create 3 arrays with 10 integer elements each and check the arrays are equal or not.
#import numpy module
import numpy
#create first numpy array with 10 elements
array_input1=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input1)
#create second numpy array with 10 elements
array_input2=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input2)
#create third numpy array with 10 elements
array_input3=numpy.array([78,45,67,54,43,56,78,90,87,9])
#display array
print(array_input3)
#check if array1 and array2 are equal
print(numpy.array_equiv(array_input1,array_input2))
#check if array1 and array3 are equal
print(numpy.array_equiv(array_input1,array_input3))
Output:
From the code, we seen that first two arrays are equal and third array is not equal. In the first statement, we are checking array1 and array2 are equal or not. It will return True because they are equal and we are checking first array with third array. It will return False, since they ate not equal.
[10 45 67 54 43 56 78 90 87 9]
[10 45 67 54 43 56 78 90 87 9]
[78 45 67 54 43 56 78 90 87 9]
True
False
Method 3 : Using allclose()
Here, we will use allclose() method available in numpy module, used to check two arrays are equal or not.
Syntax:
numpy.allclose(array1,array2)
where,
1. array1 is the first input numpy array
2. array2 is the second input numpy array.
If arrays are equal, it will return True, otherwise False.
Example:
In this example.we will create 3 arrays with 10 integer elements each and check the arrays are equal or not.
#import numpy module
import numpy
#create first numpy array with 10 elements
array_input1=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input1)
#create second numpy array with 10 elements
array_input2=numpy.array([10,45,67,54,43,56,78,90,87,9])
#display array
print(array_input2)
#create third numpy array with 10 elements
array_input3=numpy.array([78,45,67,54,43,56,78,90,87,9])
#display array
print(array_input3)
#check if array1 and array2 are equal
print(numpy.allclose(array_input1,array_input2))
#check if array1 and array3 are equal
print(numpy.allclose(array_input1,array_input3))
Output:
From the code, we seen that first two arrays are equal and third array is not equal. In the first statement, we are checking array1 and array2 are equal or not. It will return True because they are equal and we are checking first array with third array. It will return False, since they ate not equal.
[10 45 67 54 43 56 78 90 87 9]
[10 45 67 54 43 56 78 90 87 9]
[78 45 67 54 43 56 78 90 87 9]
True
False
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024