Hello geeks and welcome in this article, we will cover the NumPy isclose(). Along with that, for an overall better understanding, we will also look at its syntax and parameter. Then we will see the application of all the theory part through a couple of examples. But first, let us try to get a brief overview of this function through its definition.
Suppose you have two arrays and a situation arrives where you need to compare the 2 arrays element-wise. The function NumPy isclose() does that for you. Also, it provides you with an option to add tolerant value, which comes very handy when dealing with data from scientific experiments. Also, it returns an array of boolean on completion of the program. In the next section, we will look at the syntax associated with it.
This is the general syntax for our function. Now in the next section, let us look at the various parameters associated with it.
1. a,b : array_like
This parameter represents the 2 input arrays that need to be compared.
This parameter represents the relative tolerance parameter. This can be understood as the ratio of absolute error of the measurement being taken.
This parameter represents the absolute tolerance parameter, which can be understood as the minimum difference between the measured value and the actual value. It is advised not to use this when comparing numbers that are smaller than one.
4. equal_nan: bool
This parameter, if present, compares Nan’s as equal. If True, Nan’s in a will be considered equal to Nan’s in b in the output array.
On completion of the program it return a Boolean array. Which has true if the values are equal and false if not.
As we are done with all the theory portion related to NumPy isclose(). This section will be looking at how this function works and how it helps us achieve our desired output. We will start with an elementary level example and gradually move our way to more complicated examples.
1. Basic Numpy isclose() example
#input import numpy as ppool a=[2.67,2.56] b=[2.68,2.56] print(ppool.isclose(a,b))
Above, we can see a fundamental example of nothing fancy for understanding how the function works and its syntax application. First, we have imported the NumPy module, following which we have defined 2 arrays. Then used our syntax along with the print statement to get the desired output. Here we can see that we have not used any tolerance parameter. Due to which it will return true only if the 2 values are the same.
2. NumPy isclose() example with relative tolerance
import numpy as ppool a=[1.0001e10,1e-9] b=[1e10,1e-8] print(ppool.isclose(a,b,rtol=.01))
[ True True]
Another example related to NumPy isclose(). Here we have followed similar steps to the first example. Next, we have also used the optional parameter relative tolerance and specified it to “.01”. If we want to verify the output, we can also do so. Here our first value is 1.0001e10, which is equivalent to 10001000000, and we have to compare it with 1e10 equivalent to 10000000000. Now we have specified the relative tolerance to be .01 at maximum. Here if we find the relative tolerance for this case, we get .001, which is less than the specified condition. Hence true, you can similarly check for the 2nd case.
Why don’t you guys try out with the another parameter atol and tell me the result in that case.
NumPy isclose() vs allclose()
In this section, we will be comparing the 2 different NumPy functions. As we have already discussed NumPy isclose(). Here just look at the definition of NumPy allclose(), this function helps us find wheatear a 2 arrays are element-wise equal within the tolerance. Now lets us compare them side, which will help us in better understanding.
|NumPy isclose()||NumPy allclose()|
From the above comparison table, we can observe that we have taken the same set of arrays, but the end is different in 2 cases. This primarily because while the isclose function returns the Boolean for every single element after comparing. But in the second case the where we use allclose, we get a single Boolean value for any number of elements after comparing.
NumPy isclose() vs math isclose()
In this section, we will be looking at another comparison concerning the isclose() function. Math is an inbuilt library of python. It provides a function disclose, which is used to check whether the 2 floating-point numbers are close.
Below you can look at table showcasing the working of 2 functions
- Bitonic sort: Algorithm and Implementation in Python
- Numpy Vstack in Python For Different Arrays
- Numpy Pad Explained With Examples in Python
- isin Function Explained With Examples in Python
In this article, we covered the NumPy isclose(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. We can conclude that Numpy isclose() helps us compare two different arrays within a tolerance.
I hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done reading this, why not read about the square root function next.