Comparison Operations in Numpy Arrays
Comparison Operations in Numpy
As we already know the comparison or relational operations like less than, less than and equal to, greater than, greater than and equal to, equal to, not equal to, etc. in python. The same comparison operations can be performed in the Numpy arrays.
Create an Numpy arrays
import numpy as np
data1 = np.array([1,2,3])
data2 = np.array([4,5,6])
Here, we create an arrays named 'data1' and 'data2' to perform comparison or relational operations on it.
Less Than in Numpy arrays
We can check whether the elements in the given two numpy array are less than or not with each other. It returns boolean values as a result.
Built function - ndarray.__lt__(value)
Example
print("Less than :", data1.__lt__(data2))
Output
Less than : [ True True True]
Less Than & Equal To in Numpy arrays
We can check whether the elements in the given two numpy array are less than and equal to or not with each other. It returns boolean values as a result.
Built function - ndarray.__le__(value)
Example
print("Less than & Equal to :", data1.__le__(data2))
Output
Less than & Equal to : [ True True True]
Greater Than in Numpy arrays
We can check whether the elements in the given two numpy array are greater than or not with each other. It returns boolean values as a result.
Built function - ndarray.__gt__(value)
Example
print("Greater than :", data1.__gt__(data2))
Output
Greater than : [False False False]
Greater Than & Equal To in Numpy arrays
We can check whether the elements in the given two numpy array are greater than and equal to or not with each other. It returns boolean values as a result.
Built function - ndarray.__ge__(value)
Example
print("Greater than & Equal to :", data1.__ge__(data2))
Output
Greater than & Equal to : [False False False]
Equal To in Numpy arrays
We can check whether the elements in the given two numpy array are equal to or not with each other. It returns boolean values as a result.
Built function - ndarray.__eq__(value)
Example
print("Equal to :", data1.__eq__(data2))
Output
Equal to : [False False False]
Not Equal To in Numpy arrays
We can check whether the elements in the given two numpy array are not equal to or not with each other. It returns boolean values as a result.
Built function - ndarray.__ne__(value)
Example
print("Not Equal to :", data1.__ne__(data2))
Output
Not Equal to : [ True True True]
Challenge: Try out the all above operations in 2D array and see the output.
Follow on LinkedIn - Trillionaire TK
Follow on Instagram - Trillionaire TK
Visit our website - Information Technology
Learn, Code, Practice!!!