HOWTO · NumPy
NumPy Array Equal
Compare NumPy arrays element by element, for exact whole-array equality, with broadcasting equivalence, or within a numeric tolerance.
On this page
NumPy offers several ways to compare arrays, and the right choice depends on the result you need. Use == for an element-wise Boolean array, numpy.array_equal() for one exact whole-array result when shapes match, numpy.array_equiv() when broadcasting equivalence is intended, and numpy.allclose() for approximate numeric equality.
Compare NumPy arrays with the == operator
The == operator compares corresponding elements and returns a Boolean array. NumPy broadcasting allows compatible shapes, such as (2, 3) and (3,), to be compared; shapes that cannot broadcast together produce an error. Call .all() on the Boolean array when the question is whether every comparison is true. This reduction returns a single Boolean.
Run this example:
import numpy as np
left = np.array([1, 2, 3])
right = np.array([1, 2, 3])
comparison = left == right
print(comparison.tolist())
print(bool(comparison.all()))
[True, True, True]
True
The output shows the element-wise Boolean values first, followed by one Boolean indicating whether all values compare equal. When inputs may be empty, .all() also returns True because there is no false element; account for that vacuous result when empty inputs are possible.
Compare NumPy arrays with numpy.array_equal()
Use numpy.array_equal() when you need one Boolean for exact equality of two arrays. It returns True only when the arrays have the same shape and their corresponding elements are equal; different shapes return False rather than being treated as equivalent through broadcasting. Unlike (left == right).all(), this function handles the whole-array shape requirement as part of its comparison. By default, NaN values do not compare equal, including NaNs at matching positions; pass equal_nan=True when matching NaNs should count as equal.
For example, np.array_equal(np.array([1, 2]), np.array([1, 2])) evaluates to True. The call is illustrative and is not reported as executed.
Compare NumPy arrays with numpy.array_equiv()
numpy.array_equiv() returns one Boolean after checking whether two arrays have equal values under broadcasting. Therefore, arrays with different but compatible shapes can be equivalent: a scalar value can match every element of an array filled with that value. If the shapes cannot broadcast, the arrays are not equivalent. Choose this function when broadcast compatibility is part of the intended meaning of equality; use numpy.array_equal() when identical shapes are required.
For example, np.array_equiv(np.array([5, 5]), np.array(5)) evaluates to True under broadcasting. This expression is illustrative and is not reported as executed.
Compare NumPy arrays with numpy.allclose()
Floating-point calculations can produce slightly different values for mathematically equivalent results. numpy.allclose() returns one Boolean indicating whether every pair is close within a tolerance. Its comparison uses np.absolute(a - b) <= atol + rtol * np.absolute(b), where atol and rtol are the absolute and relative tolerances. The default tolerances may not suit every scale, so set them to match the precision your application requires. NaNs compare unequal by default; equal_nan=True treats NaNs in matching positions as equal.
For example, np.allclose(np.array([0.3]), np.array([0.1 + 0.2]), atol=1e-8) is intended to return True within the chosen tolerance. This expression is illustrative and is not reported as executed.
Choose == when you need each element’s result, then reduce it with .all() only when appropriate. Choose numpy.array_equal() for exact equality with matching shapes, numpy.array_equiv() for equality under broadcasting, and numpy.allclose() for approximate numeric comparisons.