How to Perform Element-wise Comparison in NumPy Array
Using __eq__() method of NumPy library we can compare two ndarray or compare a ndarray to a specific value.
Let us understand it better with an example:
Example
In this example, we can see that after applying numpy.__eq__(), we get the simple boolean array that can tell us which element in an array is equal to that of the provided parameter.
Python3
# import the important module in python import numpy as np # make an array with numpy gfg = np.array([[ 1 , 2 , 3 , 4 , 5 , 6 ], [ 6 , 5 , 4 , 3 , 2 , 1 ]]) # applying numpy.__eq__() method print (gfg.__eq__( 4 )) |
[[False False False True False False] [False False True False False False]]
NumPy ndarray.__eq__() Method | Compare Array Values to Specific Value
The ndarray.__eq__() method of Numpy compares the values in ndarray to a specific value. It is useful to find which value in ndarray is equal to the given value.
It will return you a NumPy array with a boolean type having only values True and False.
Example
Python3
# import the important module in python import numpy as np # make an array with numpy gfg = np.array([ 1 , 2 , 3 , 4 , 5 , 6 ]) # applying numpy.__eq__() method print (gfg.__eq__( 4 )) |
[False False False True False False]