search
Search
Login
Unlock 100+ guides
menu
menu
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | allclose method

schedule Aug 11, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Numpy's `allclose(~)` performs an element-wise comparison given two arrays, and returns True if all of the differences between each pair falls within the specified tolerance.

# Parameters

1. `x1` | `array-like`

The first input array.

2. `x2` | `array-like`

The second input array.

3. `rtol` | `float` | `optional`

The relative tolerance parameter. By default, `rtol=0`.

4. `atol` | `float` | `optional`

The absolute tolerance parameter. By default, `atol` is set to a small number (~1e-8)

5. `equal_nan` | `boolean` | `optional`

If True, then element-wise comparisons that involve two NaNs will evaluate to True. By default, `equal_nan=False`.

Here, the element-wise comparison evaluates to True if:

``` absolute(a - b) <= (atol + rtol * absolute(b)) ```

# Return value

A single boolean that indicates whether or not two arrays are "close" enough.

# Examples

## Basic usage

``` np.allclose([2,3], [5,3]) False ```

Here, the first element-wise comparison `2 != 5`, so the method returns `False`.

## Specifying an absolute tolerance parameter

``` np.allclose([6,4], [8,3], atol=2) True ```

Here, `absolute(6,8) <= 2` and `absolute(4,3) <= 2`.

## Specifying a relative tolerance parameter

``` np.allclose([6,3], [4,6], rtol=0.5) True ```

Here, `absolute(6,4) <= 4*0.5` and `absolute(3,6) <= 6*0.5`.

## Comparing NaNs

``` np.allclose(np.NaN, np.NaN) False `````` np.allclose(np.NaN, np.NaN, equal_nan=True) True ```
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!
Navigation
Home Blog
Contact us