{ } Raw JSON

bundles / numpy 2.4.4 / numpy / testing / _private / utils / assert_array_equal

function

numpy.testing._private.utils:assert_array_equal

source: /numpy/testing/_private/utils.py :994

Signature

def   assert_array_equal ( actual desired err_msg = '' verbose = True * strict = False )

Summary

Raises an AssertionError if two array_like objects are not equal.

Extended Summary

Given two array_like objects, check that the shape is equal and all elements of these objects are equal (but see the Notes for the special handling of a scalar). An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions.

The usual caution for verifying equality with floating point numbers is advised.

Parameters

actual : array_like

The actual object to check.

desired : array_like

The desired, expected object.

err_msg : str, optional

The error message to be printed in case of failure.

verbose : bool, optional

If True, the conflicting values are appended to the error message.

strict : bool, optional

If True, raise an AssertionError when either the shape or the data type of the array_like objects does not match. The special handling for scalars mentioned in the Notes section is disabled.

Raises

: AssertionError

If actual and desired objects are not equal.

Notes

When one of actual and desired is a scalar and the other is array_like, the function checks that each element of the array_like is equal to the scalar. Note that empty arrays are therefore considered equal to scalars. This behaviour can be disabled by setting strict==True.

Examples

The first assert does not raise an exception:
np.testing.assert_array_equal([1.0,2.33333,np.nan],
                              [np.exp(0),2.33333, np.nan])
Assert fails with numerical imprecision with floats:
np.testing.assert_array_equal([1.0,np.pi,np.nan],
                              [1, np.sqrt(np.pi)**2, np.nan])
Use `assert_allclose` or one of the nulp (number of floating point values) functions for these cases instead:
np.testing.assert_allclose([1.0,np.pi,np.nan],
                           [1, np.sqrt(np.pi)**2, np.nan],
                           rtol=1e-10, atol=0)
As mentioned in the Notes section, `assert_array_equal` has special handling for scalars. Here the test checks that each value in `x` is 3:
x = np.full((2, 5), fill_value=3)
np.testing.assert_array_equal(x, 3)
Use `strict` to raise an AssertionError when comparing a scalar with an array:
np.testing.assert_array_equal(x, 3, strict=True)
The `strict` parameter also ensures that the array data types match:
x = np.array([2, 2, 2])
y = np.array([2., 2., 2.], dtype=np.float32)
np.testing.assert_array_equal(x, y, strict=True)

See also

assert_allclose

Compare two array_like objects for equality with desired relative and/or absolute precision.

assert_array_almost_equal_nulp
assert_array_max_ulp
assert_equal

Aliases

  • numpy.testing.assert_array_equal