The equal() function of NumPy module

The equal() function of NumPy does an element-wise comparison of two numpy array-likes and returns the result as a Boolean array. The result is a single Boolean value when input parameters are scalars.

Logical Operations Updated 26 Oct 2024 2 min read

Overview

  • The numpy.equal() function checks whether two NumPy array-likes are equal element-wise. If the pair of elements match the result is True. The result is False otherwise. The results are returned as an ndarray containing Boolean values. A single Boolean value is returned if the compared input types are scalars.

Example 1

python
# Example Python program that uses numpy.equal()

            # to compare the elements of two ndarray instances

            import numpy

            import random

# Function to fill a 3x3 ndarray with double-precision

            # floating point numbers

            def fillDPArray(arr_in):

                for x in range(0, 3):

                    for y in range(0, 3):

                            arr_in[x][y] = random.uniform(1, 2)

# Create ndarray instances of double-precision floating point numbers

            dpArray1 = numpy.ndarray(shape = (3, 3), dtype = numpy.float64)

            dpArray2 = numpy.ndarray(shape = (3, 3), dtype = numpy.float64)

fillDPArray(dpArray1)

            fillDPArray(dpArray2)

# Do not want all the results as False, as

            # we do not want to rely on the likelyhood of having

            # random.uniform() generating two equal numbers in short

            # span of time/interval

            dpArray2[0, 0] = dpArray1[0, 0]

print("Array1:")

            print(dpArray1)

print("Array2:")

            print(dpArray2)

print("Array1 = Array2")

            print("(Element-wise):")

            results = numpy.equal(dpArray1, dpArray2)

            print(results)

Output

Output
Array1:

[[1.78418893 1.34502285 1.84007858]

 [1.29815828 1.17697561 1.67392455]

 [1.63705878 1.85393845 1.24246788]]

Array2:

[[1.78418893 1.73576206 1.47579266]

 [1.44167562 1.47260249 1.42296846]

 [1.07237369 1.01274364 1.12999644]]

Array1 = Array2

(Element-wise):

[[ True False False]

 [False False False]

 [False False False]]

Example 2

python
# Example Python program to check for equality between

            # elements pairs drawn from two NumPy ndarray instances

            import numpy

exps1 = numpy.array([[5,     10.1,  15/5],

                               [-20,  round(25.11), 30 * 2],

                               [pow(10, 2),  len(range(1, 5)), sum([-1, -2, -3])]])

            print(exps1)

exps2 = numpy.array([[5,     10.12,  15/5],

                               [-20,  round(25.112), 30 * 2],

                               [pow(10, 2),  len(range(1, 5)), sum((-1, -2, -3))]])

            print(exps2)

            results = numpy.equal(exps1, exps2)

            print(results)

Output

Output
[[  5.   10.1   3. ]

 [-20.   25.   60. ]

 [100.    4.   -6. ]]

[[  5.    10.12   3.  ]

 [-20.    25.    60.  ]

 [100.     4.    -6.  ]]

[[ True False  True]

 [ True  True  True]

 [ True  True  True]]

Example 3

python
# Example Python program that checks whether each

            # element of two NumPy array-likes are equal

import numpy

# Create two ndarray instances and populate them with values

            numbers1 = numpy.ndarray(shape = (2, 2), dtype=complex)

numbers1[0][0] = 1.1

            numbers1[0][1] = 2

            numbers1[1][0] = 3.14

            numbers1[1][1] = 4+2j

numbers2 = numpy.ndarray(shape = (2, 2), dtype=complex)

            numbers2[0][0] = 1.11

            numbers2[0][1] = 2.0

            numbers2[1][0] = 3.141

            numbers2[1][1] = 4-2j

results = numpy.equal(numbers1, numbers2)

            print("Array1:")

            print(numbers1)

print("Array2:")

            print(numbers2)

print("Array1 == Array2:")

            print(results)

Output

Output
Array1:

[[1.1 +0.j 2.  +0.j]

 [3.14+0.j 4.  +2.j]]

Array2:

[[1.11 +0.j 2.   +0.j]

 [3.141+0.j 4.   -2.j]]

Array1 == Array2:

[[False  True]

 [False False]]