Comparison Operations in Numpy Arrays
Numpy - Comparison Operations

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!!!

To view or add a comment, sign in

More articles by Tharunkumar S

  • Arithmetic Operations in Numpy Arrays

    Arithmetic Operations in Numpy As we already know the arithmetic operations like addition, subtraction, division…

  • Shape of an Array and Reshape() function

    Shape of an Numpy array In Numpy array, shape is an attribute that is used to check the dimension of an array. shape…

  • Slicing in Numpy Arrays

    What is Slicing in Numpy Array? Slice the array in a particular range of elements. In other words, Make an sub array…

  • Indexing in Numpy Arrays

    What is Indexing in Numpy Array? Access the elements in a Numpy array based on index values. Accessing the elements in…

  • Explore array(), type() and astype() functions

    What is ndarray in Numpy? "ndarray" stands for n-dimensional array. Numpy creates an object for an array in this type.

  • Introduction to Numpy Library in Python

    Introduction NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language.

  • Journey of Machine Learning Begins

    Machine Learning Definition : The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the…

Explore content categories