Numpy isClose() method implementation: 3 Steps Only

Numpy isClose() method implementation

Numpy is the best Python library for manipulating the NumPy array. There are many inbuilt functions in it that allow you to do complex mathematical calculations over the NumPy array. The numpy isClose() is one of them. In this entire tutorial, you will know the implementation of numpy isClose() method through steps.

Syntax of the Numpy isClose()

Before going to the coding demonstration part let’s know the syntax of the NumPy isClose method.

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

Explanation of syntax

a: It is the first input array.

b: The second input array to compare.

rtol : The relative tolerance parameter of type float.

equal_nan: Whether to compare NaN’s as equal. If True, NaN’s in a will be considered equal to NaN’s in b in the output array.

The method returns a boolean array of where a and b are equal within the given tolerance.

Steps to Implement Numpy isClose Method

In this section, you will know all the steps for implementing isClose() method. Just follow all the steps for more understanding.

Step 1: Import required libraries

The first step is to import all the necessary libraries that we require in our example. I am using only the NumPy module. So let’s import it using the import statement.

Step 2: Create a Sample Numpy array

The next step is to create a dummy number array where you will apply NumPy isClose. For demonstration, I will create a Scalar and a single-dimensional NumPy array.

Scalar Value

numpy_scalar_1 = np.array()
numpy_scalar_2 = np.array()

Single Dimension Array

array_1d_1= np.array([1,2,3,4])
array_1d_2= np.array([1.1,2.1,3.2,4.1])

Step 3: Apply the numpy isClose() method

After the creation of the array, let’s check whether elements are close to each other or not.  Execute the full code and see the output.

Checking closeness of the Scalar Value

import numpy as np
numpy_scalar_1 = np.array([10])
numpy_scalar_2 = np.array([11])
print(np.isclose(numpy_scalar_1,numpy_scalar_2,atol=1))

Output

Checking closeness of two scalar value
Checking closeness of two scalar value

Checking closeness of the Single Dimensional Array

import numpy as np
array_1d_1= np.array([1,2,3,4])
array_1d_2= np.array([1.1,2.1,3.2,4.1])
print(np.isclose(array_1d_1,array_1d_2,atol=0.1)

Output

Checking closeness of two numpy array
Checking closeness of two NumPy array

You can see the first array is compared with the second with a tolerance of 0.1. It will check the difference of each element and if the value is 0.1 then it will return true otherwise false.

Conclusion

Numpy isClose() is the method to check the closeness of any elements in the array. These are the steps for implementing it. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

Numpy Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner