Numpy isclose() function with examples

In this post, we will learn about Numpy’s isclose() function to determine if two numbers or two arrays are close to each other allowing for tolerance to call if they are close.

Numpy’s np.close() function is extremely useful when comparing floating-point numbers. We know that floating-point numbers are not represented accurately in computer memory. And Numpy’s isclose should be used to compare them instead of Python’s is or == methods.

Another use of isclose() function is to compare Numpy arrays with small differences in elements. Visual inspection can be misleading to decide if the two arrays are equal or close enough. Numpy’s isclose() can easily spot differences.

Let us started on learning how to use Numpy’s isclose() function. Let us load numpy and check Numpy’s version.

# import numpy
import numpy as np
# check version
np.__version__

1.24.0

Numpy isclose() function to check if numbers or arrays are equal

Basic syntax of using isclose is as shown below.

isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

Numpy’s isclose function takes two numbers are two numpy arrays as input and returns a Boolean/logical value or array telling us if the numbers or elements of array close. Here the degree of how close is defined by the two tolerance arguments. Two numbers are considered close if the absolute difference between them is less than or equal to the value defined by

absolute(a - b) <= (atol + rtol * absolute(b))

Numpy isclose() function: Check if two numbers are the same

Int he first example of Numpy’s isclose() fucntion, we check if two numbers are close enough. Let us say we have two numbers a and b

a = 1
b = 1.1

If we use Numpy’s isclose() to find if they are close enough, we will get False as result. This is because the relative difference between the two numbers is bigger than the default tolerance 1e-05.

np.isclose(a,b)

False

Now let us consider another two numbers as shown below

a = 1
b = 1.00001

Here we can see that the difference is equal to the relative tolerance level. Therefore, we would get True when we use np.close() function.

np.isclose(a,b)

True

We can change the tolerance level to force two numbers close when needed. In the example below we changed the relative tolerance level from default 1e-05 to 0.1. Now the two numbers

a = 1
b = 1.1

are close enough under the new tolerance level. And we will get True as result.

np.isclose(a, b, rtol=0.1)

True

Numpy isclose() function: Check if two arrays are the same

Now lets us consider two Numpy arrays and check if their elements are close to each other. We can use isclose as follows:

a = np.array([1, 2, 3])
b = np.array([1.01, 2.01, 3.01])

result = np.isclose(a, b)

When comparing arrays, Numpy’s isclose function returns an array of Boolean values, with each element indicating whether the corresponding element in a is close to the corresponding element in b.

In this case, we get boolean array containing three False as the relative difference between every element is bigger than the default tolerance value.

print(result) 

[False False False]

Now let us consider the following two arrays and use Numpy’s np.close() with the default tolerance level.

a = np.array([1, 2, 3])
b = np.array([1.0000001, 2.00001, 3.01])

result = np.isclose(a, b)

print(result) 

[ True  True False]

We got True for the first and second element as the difference is smaller than or equal to rtol value. And for the third element we got False is the difference is bigger than the default tolerance.

Another close relative of Numpy’s isclose() function is allclose() function. See this post to learn about how to use allcose() function.

  • Numpy allclose() function with examples