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.
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() numpy_scalar_2 = np.array() print(np.isclose(numpy_scalar_1,numpy_scalar_2,atol=1))
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)
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.
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.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.