Np allclose python 3 описание как работает
Перейти к содержимому

Np allclose python 3 описание как работает

  • автор:

NumPy Logic functions: allclose() function

The allclose() function is used to returns True if two arrays are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers.
The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

If either array contains one or more NaNs, False is returned.
Infs are treated as equal if they are in the same place and of the same sign in both arrays.

Syntax:

Version: 1.15.0

Parameter:

Name Description Required /
Optional
a, b Input arrays to compare.
array_like
Required
rtol The relative tolerance parameter (see Notes).
float
Required
atol The absolute tolerance parameter (see Notes).
float
Required
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.
bool
Required

Returns:
allclose : bool — Returns True if the two arrays are equal within the given tolerance; False otherwise.

Notes:
If the following equation is element-wise True, then allclose returns True.

The above equation is not symmetric in a and b, so that allclose(a, b) might be different from allclose(b, a) in some rare cases.

The comparison of a and b uses standard broadcasting, which means that a and b need not have the same shape in order
for allclose(a, b) to evaluate to True. The same is true for equal but not array_equal.

NumPy.allclose() method Example-1:

NumPy.allclose() method Example-2:

NumPy.allclose() method Example-3:

NumPy.allclose() method Example-4:

NumPy.allclose() method Example-5:

Python — NumPy Code Editor:

Follow us on Facebook and Twitter for latest update.

  • Weekly Trends

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

numpy.allclose#

Returns True if two arrays are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

NaNs are treated as equal if they are in the same place and if equal_nan=True . Infs are treated as equal if they are in the same place and of the same sign in both arrays.

Parameters : a, b array_like

Input arrays to compare.

rtol float

The relative tolerance parameter (see Notes).

atol float

The absolute tolerance parameter (see Notes).

equal_nan bool

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.

New in version 1.10.0.

Returns True if the two arrays are equal within the given tolerance; False otherwise.

If the following equation is element-wise True, then allclose returns True.

The above equation is not symmetric in a and b, so that allclose(a, b) might be different from allclose(b, a) in some rare cases.

The comparison of a and b uses standard broadcasting, which means that a and b need not have the same shape in order for allclose(a, b) to evaluate to True. The same is true for equal but not array_equal .

allclose is not defined for non-numeric data types. bool is considered a numeric data-type for this purpose.

NumPy np.allclose()

This tutorial will explore the allclose() function syntax and give several practical examples demonstrating how to use it.

NumPy allclose() Function

The allclose() function will compare the corresponding elements in the input arrays and determine if they are equal (with tolerance).

A tolerance value is always positive, typically in small numbers. To calculate the absolute difference between the two input arrays, NumPy adds the relative and absolute differences.

The relative difference is the product of rtol and abs(b), where b is the second input array.

Function Syntax

This is depicted in the function syntax shown below:

Let us explore the function parameters.

Function Parameters

  1. a – the first input array.
  2. b – the second input array.
  3. rtol – defines the relative tolerance.
  4. atol – defines absolute tolerance.
  5. equal_nan – specifies whether or not to compare NaN as equal. If set to true, the function will treat a NaN in the first array as equivalent to a NaN in the second array.

Function Return Value

The function returns a Boolean value. If the specified arrays are equal within the defined tolerance value, the function returns True. Otherwise, the function will return false.

Example #1

Consider the example below that shows how to use the allclose() function in a 1D array.

We create two 1-D arrays in the example above and compare them using the allclose() function.

NOTE: We do not set the absolute and relative tolerance values in the example above. The function should return:

Example #2To set tolerance values, we can use the example below:

In the example above, we set the relative and absolute tolerance values using the rtol and atol parameters.

NOTE: The above example’s tolerance values have been tweaked for illustration purposes.

The code below should return:

Example #3

In the example below, we use the allclose() function to test equality with arrays that include NaN values.

In the example above, we have two arrays that seem equal. However, when we use the allclose() function, it returns false as shown:

This is because the arrays contain NaN values. By default, the allclose() function will treat NaN values differently.

To solve this, we can set the equal_nan parameter to true as shown:

In this case, the function should return:

Terminating

This article discussed how to use the allclose() function in NumPy. We also demonstrated how to use the function with various examples.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

NumPy allclose() Method Illustration – With Examples

NumPy allclose

Hello coders!! This article will be learning about the NumPy.allclose() method in Python. NumPy is an inbuilt module in Python used for array-like functions. Without much further ado, let us dive straight into the topic.

What is NumPy allclose ()?

numpy.allclose() is a function of the NumPy module in Python. It is used to find if two arrays are equal element-wise within a given tolerance. The tolerance values are small positive numbers. The relative difference and the absolute difference are added together and compared against the absolute difference between two arrays. If either array contains one or more NaNs, it returns False. Items are treated as equal if they are in the same place and of the same sign in both arrays.

Syntax:

Parameters:

  • arr1: 1st array input
  • arr2: 2nd array input
  • rtol: relative tolerance
  • atol: absolute tolerance
  • equal_nan:Whether to compare NaN’s as equal. If True, NaN’s in arr1 is considered equal to NaN’s in arr2

Return Value:

True if the two arrays are equal within the given tolerance, else False.

Examples of the allclose method:

1) Basic Numpy allclose() Example:

Output & Explanation:

As the first element of both the elements are not equal, it returns False.

2) NumPy allclose() with relative tolerance:

Output & Explanation:

Here, we have used the relative tolerance as 0.1 and the absolute tolerance as 0.1. In the given tolerance range, thearray eleents are same and hence it returns True.

3) Array having NaN value:

Output & Explanation:

As the equal_nan parameter is not set as True, the nan values of both the arrays are considered different. As a result the output is False.

4) Array having NaN value with equal_nan value set as True:

Output & Explanation:

Since we have now used the equal_nan parameter as True, the NaN value of both the arrays are considered to be equal and so the output is True.

Difference between NumPy allclose() & isclose():

NumPy allclose() NumPy isclose()
import numpy as np
a=[1e10,1.002e10]
b=[1e10,1.003e10]
print(np.allclose(a,b))
import numpy as np
a=[1e10,1.002e10]
b =[1e10,1.003e10]
print(np.isclose(a,b))
False [ True False]

Conclusion:

With this article, we come to an end of this article. Here, we saw some illustrated examples to clear our concept on NumPy allclose() method.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *