Open In App

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




# 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))

Output
[False False False  True False False]

Syntax

Syntax: ndarray.__eq__($self, value, /) 

Parameter

  • self: The array on which the method is called.
  • value: The value or array to compare with.

Return: New array with boolean values

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.




# 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))

Output
[[False False False  True False False]
 [False False  True False False False]]

Article Tags :