## Definition of NumPy isclose

Python provides different functions to the users. To work with arrays, the python library provides a numpy function. Numpy is an acronym for numerical python. Numpy performs logical and mathematical operations of arrays. In python, numpy is faster than the list. This is because numpy arrays can be stored at continuous places. Therefore, processing and manipulating can be done efficiently. Numpy isclose method is used to check whether the two given values are close or not. The result of isclose method returns a Boolean value. It means that if the given values are close then it will return ‘true’ otherwise it will return ‘false’. The isclose method uses relative or absolute tolerance to check to check the closeness of values.

**Syntax:**

```
import numpy as np
import math
math.isclose(p, q, relative_tol=value, absolute_tol=value )
```

**Explanation: **In the above syntax where first we import numpy class and access a numpy class as an np, then we import the math library. Generally, in isclose method, we have used the following parameters.

- p = this is the first value of isclose method for closeness. This parameter is compulsory for the method.
- q = this is the second value of isclose method. It is used to check the closeness of the values. This is a required parameter.
- relative_tol = this is an optional parameter called relative tolerance. Relative tolerance is nothing but the maximum difference which is allowed between values p and q. The default value of relative tolerance lies between 1e-09.
- absolute_tol = this is an optional parameter called absolute tolerance. Absolute tolerance compares the values which are near to 0. The value of absolute tolerance should be at least 0.

### How isclose Method works in NumPy?

- We must install Python on your system.
- We must install numpy using the pip command.
- We required basic knowledge about Python.
- We required basic knowledge about numpy.
- We can check the closeness of the values.

### Examples of NumPy isclose

Let’s see how we can implement isclose method with different examples.

#### Example #1

**Code:**

```
import numpy as anp
import math
print("First Value is : \n", math.isclose(2.555,2.3566))
print("Second Value is : \n",math.isclose(2.555,2.555))
print("Third Value is : \n",math.isclose(2.555,2.555000001))
```

**Output:**

**Explanation**

- We import numpy functions and use them as anp.
- We import math libraries.
- We use isclose method to check the closeness of values whose closeness is far. Therefore it will return a false value.
- Again we use isclose method for the values which are exactly close. Hence it will return a true value.
- Further, we use isclose method for the values which are nearly close. Hence it will return a true value.

In the above example, we try to implement math.isclose function. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #2

**Code:**

```
#import numpy and math library
import numpy as cnp
import math
#To check closeness of the values
print("Result of both value is : \n", cnp.isclose([2,5.5], [2,5.5]))
print("Result of both value is : \n",cnp.isclose([1e9,1e-7], [1.00001e9,1e-8]))
print("Result of both value is : \n",cnp.isclose([1e6,1e-9], [1.0001e6,1e-8]))
```

**Output:**

**Explanation **

- We import numpy functions and use them as cnp.
- We import math libraries.
- We use isclose method to check the closeness of two values that are the same close, therefore both values will be true
- Again we use isclose method for the two different values which are the first value is exactly close and the second is different. Hence it will return a true and false value.
- Further, we use isclose method for the two different values which are the first values are not nearly close and the second value is close. Hence it will return a false and true value.

In the above example, we tried to check multiple values which are closed or not by using isclose and math functions. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #3

**Code:**

```
#import numpy and math library
import numpy as cnp
import math
#To check closeness of the values
print("Result of both value is : \n",cnp.isclose([2.1, cnp.nan], [2.1, cnp.nan]))
print("Result of both value is : \n",cnp.isclose([4.1, cnp.nan], [4.1, cnp.nan], equal_nan=True))
```

**Output:**

**Explanation**

- We import numpy functions and use them as cnp.
- We import math libraries.
- We use isclose method to check the closeness of two values which are the first value is close and the second value is missing. Therefore the first value is true and the second value will be false
- Similarly, we try to find out closer values by using the same function with different values in which the first value is closer and the second value is missing but here we declared nan equal to true. Therefore both values are true.

In the above example, we have checked if missing values are closer or not by using the same function. Illustrate the end result of the above declaration by using the use of the following snapshot.

#### Example #4

For Absolute tolerance is defined.

**Code:**

```
# Import math library
import math
# check closeness of the two values
print("Closeness of two values is : \n", math.isclose(7.05, 7.06, abs_tol = 0.1))
print("Closeness of two values is : \n",math.isclose(4.666, 4.450, abs_tol = 0.150))
```

**Output:**

**Explanation**

- First, we import math libraries and access them as math.
- Then we check the closeness of two values when we define absolute tolerance. Here absolute tolerance is exactly close so it returns true.
- In the second case, we also check the closeness of two values but here absolute tolerance is different. Therefore it returns false.

In the above example, we have implemented isclose function when absolute tolerance is given. Illustrate the end result of the above declaration by using the use of the following snapshot.

### Conclusion

We hope from this article you have understood about the numpy isclose function. From the above article, we have learned the basic syntax numpy isclose function. We have also learned how we can implement them in Python with different examples of isclose function as well as we also learned math functions in python. From this article, we have learned how we can handle numpy isclose and math functions in python.

### Recommended Articles

This is a guide to NumPy isclose. Here we discuss the introduction and how isclose method work in numpy? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses