Numpy Comparison Functions - numpy equal, not_equal, less, greater, greater equal function with example
In this numpy tutorial, we will discuss different comparison operations performed on the given numpy array:
-
numpy equal function,
-
numpy function not_equal,
-
numpy less function,
-
numpy greater function,
-
numpy greater equal,
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.
numpy equal function
numpy equal functio/method is used to check the elements in the two arrays are equal or not.
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If equal - True is returned, otherwise False is returned.
Scenario:
array1=[1,2,3,4,5]
array2=[1,4,2,1,4]
Working -
[ 1 equal 1 ] = True
[ 2 equal 4 ] = False
[ 3 equal 2 ] = False
[ 4 equal 1 ] = False
[ 5 equal 4 ] = False
Result :
[True False False False False]
Syntax:
numpy.equal(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
Example: numpy equal function
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,5])
#creating array2
arraydata2=numpy.array([1,2,10,4,23])
#display two arrays
print(arraydata1,arraydata2)
#check whether the two arrays are equal or not
print(numpy.equal(arraydata1,arraydata2))
In the above example, we created two arrays with 5 elements each and perform equal() operation.
Output:
[1 2 3 4 5] [ 1 2 10 4 23]
[ True True False True False]
numpy function not_equal
numpy function not_equal method is used to check the elements in the two arrays are not equal
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If equal - False is returned, otherwise True is returned.
Note - Viceversa of equal
Scenario:
array1=[1,2,3,4,5]
array2=[1,4,2,1,4]
Working -
[ 1 not_equal 1 ] = False
[ 2 not_equal 4 ] = True
[ 3 not_equal 2 ] = True
[ 4 not_equal 1 ] = True
[ 5 not_equal 4 ] = True
Result :
[False True True True True]
Syntax:
numpy.not_equal(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
Example: numpy function not_equal
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,5])
#creating array2
arraydata2=numpy.array([1,2,10,4,23])
#display two arrays
print(arraydata1,arraydata2)
#check whether the two arrays are not equal
print(numpy.not_equal(arraydata1,arraydata2))
In the above example, we created two arrays with 5 elements each and perform not_equal() operation.
Output:
[1 2 3 4 5] [ 1 2 10 4 23]
[False False True False True]
numpy less function
numpy less function\method is used to check the elements in first array are less than the elements in the second array.
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If less - True is returned, otherwise False is returned.
Scenario:
array1=[1,2,3,4,5]
array2=[1,4,2,1,4]
Working -
[ 1 less 1 ] = False
[ 2 less 4 ] = True
[ 3 less 2 ] = False
[ 4 less 1 ] = False
[ 5 less 4 ] = False
Result :
[False True False False False]
Syntax:
numpy.less(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
Example: numpy less function
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,5])
#creating array2
arraydata2=numpy.array([1,2,10,4,23])
#display two arrays
print(arraydata1,arraydata2)
#perform less() comparison operation
print(numpy.less(arraydata1,arraydata2))
In the above example, we created two arrays with 5 elements each and perform less() operation.
Output:
[1 2 3 4 5] [ 1 2 10 4 23]
[False False True False True]
numpy greater function
numpy greater function/method is used to check the elements in first array are greater than the elements in the second array.
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If greater - True is returned, otherwise False is returned.
Scenario:
array1=[1,2,3,4,5]
array2=[1,4,2,1,4]
Working -
[ 1 greater 1 ] = False
[ 2 greater 4 ] = False
[ 3 greater 2 ] = True
[ 4 greater 1 ] = True
[ 5 greater 4 ] = True
Result :
[False False True True True]
Syntax:
numpy.greater(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
Example: numpy greater function
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,5])
#creating array2
arraydata2=numpy.array([1,2,10,4,23])
#display two arrays
print(arraydata1,arraydata2)
#perform greater() comparison operation
print(numpy.greater(arraydata1,arraydata2))
In the above example, we created two arrays with 5 elements each and perform greater() operation.
Output:
[1 2 3 4 5] [ 1 2 10 4 23]
[False False False False False]
numpy greater_equal()
numpy greater equal method is used to check the elements in first array are greater than or eual to the elements in the second array.
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If greater or equal - True is returned, otherwise False is returned.
Syntax:
numpy.greater_equal(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
less_equal()
This method is used to check the elements in first array are less than or eual to the elements in the second array.
It will perform element wise comparison in both the arrays.
Return:
It will return boolean values
If greater or equal - True is returned, otherwise False is returned.
Syntax:
numpy.less_equal(arraydata1,arraydata2)
where,
-
arraydata1 is the first input array
-
arraydata2 is the second input array.
Example:
In this example, we will perform the above comparison operations
#importing numpy module
import numpy
#creating array1
arraydata1=numpy.array([1,2,3,4,5])
#creating array2
arraydata2=numpy.array([1,2,10,4,23])
#display two arrays
print(arraydata1,arraydata2)
#perform greater_equal() comparison operation
print(numpy.greater_equal(arraydata1,arraydata2))
print()
#perform less_equal() comparison operation
print(numpy.less_equal(arraydata1,arraydata2))
Output:
[1 2 3 4 5] [ 1 2 10 4 23]
[ True True False True False]
[ True True True True True]
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 :
Feb 26,2023