🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.isclose()

AI & DATA SCIENCE // np-isclose

np.isclose() returns a boolean array indicating, element by element, whether two arrays are equal within a given tolerance, unlike np.allclose(), which collapses the result into a single overall True/False.

Syntax

np.isclose(a, b, rtol=1e-05, atol=1e-08)

Deep Dive Course

isclose() computes the exact same tolerance-based comparison as allclose() for each corresponding pair of elements, but returns the full per-element boolean array instead of reducing it down to one combined result — which lets you see exactly which elements matched and which didn't, rather than only knowing whether all of them did. np.allclose(a, b) is functionally equivalent to applying np.all() to the result of np.isclose(a, b).

1Understanding np.isclose()

isclose() computes the exact same tolerance-based comparison as allclose() for each corresponding pair of elements, but returns the full per-element boolean array instead of reducing it down to one combined result — which lets you see exactly which elements matched and which didn't, rather than only knowing whether all of them did. np.allclose(a, b) is functionally equivalent to applying np.all() to the result of np.isclose(a, b).

💡

Use np.isclose() over np.allclose() specifically when you need to know which particular elements differ, not just whether the arrays match overall — allclose() is essentially isclose() with an extra np.all() applied on top.

editor.html
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
print(np.isclose(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.isclose() showing how it is used in production NumPy code.

editor.html
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
mismatches = np.where(~np.isclose(a, b))
print(mismatches)
localhost:3000

3Best Practices

Follow these guidelines when working with np.isclose():

1. Use np.isclose() instead of allclose() when you need per-element detail about which values matched and which didn't

2. Combine np.isclose() with boolean indexing to isolate and inspect specifically the elements that failed a tolerance check

3. Adjust rtol/atol deliberately based on your data's expected precision, the same considerations that apply to allclose()

⚠️

Tip: Use np.isclose() over np.allclose() specifically when you need to know which particular elements differ, not just whether the arrays match overall — allclose() is essentially isclose() with an extra np.all() applied on top.

editor.html
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
print(np.isclose(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
print(np.isclose(a, b))
Example 02Advanced Example
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
mismatches = np.where(~np.isclose(a, b))
print(mismatches)

Best Practices

  • Use np.isclose() instead of allclose() when you need per-element detail about which values matched and which didn't
  • Combine np.isclose() with boolean indexing to isolate and inspect specifically the elements that failed a tolerance check
  • Adjust rtol/atol deliberately based on your data's expected precision, the same considerations that apply to allclose()

Interview Question

How is np.allclose(a, b) related to np.isclose(a, b)?

Hint: Think about what one extra function call would need to be applied to go from one to the other.

np.allclose(a, b) is functionally equivalent to calling np.all() on the result of np.isclose(a, b) — isclose() does the actual per-element tolerance comparison and returns a full boolean array, and allclose() simply collapses that array down to a single overall True, only if every single element passed the tolerance check, or False otherwise. They share the exact same underlying comparison logic and default tolerance parameters; the only difference is whether you get the detailed per-element result or a single summary answer.

Exercises

MediumPractice using np.isclose() in a real scenario.
View Solution
import numpy as np

a = np.array([1.0, 2.0, 3.00001])
b = np.array([1.0, 2.0001, 3.0])
print(np.isclose(a, b))

Frequently Asked Questions

How is np.allclose(a, b) related to np.isclose(a, b)?

np.allclose(a, b) is functionally equivalent to calling np.all() on the result of np.isclose(a, b) — isclose() does the actual per-element tolerance comparison and returns a full boolean array, and allclose() simply collapses that array down to a single overall True, only if every single element passed the tolerance check, or False otherwise. They share the exact same underlying comparison logic and default tolerance parameters; the only difference is whether you get the detailed per-element result or a single summary answer.

Related Functions

np-allclosenp-wherefloats