numpy.testing.assert_array_equal#

testing.assert_array_equal(actual,desired,err_msg='',verbose=True,*,strict=False)[source]#

Raises an AssertionError if two array_like objects are not equal.

Given two array_like objects, check that the shape is equal and allelements of these objects are equal (but see the Notes for the specialhandling of a scalar). An exception is raised at shape mismatch orconflicting values. In contrast to the standard usage in numpy, NaNsare compared like numbers, no assertion is raised if both objects haveNaNs in the same positions.

The usual caution for verifying equality with floating point numbers isadvised.

Note

When eitheractual ordesired is already an instance ofnumpy.ndarray anddesired is not adict, the behavior ofassert_equal(actual,desired) is identical to the behavior of thisfunction. Otherwise, this function performsnp.asanyarray on theinputs before comparison, whereasassert_equal defines specialcomparison rules for common Python types. For example, onlyassert_equal can be used to compare nested Python lists. In new code,consider using onlyassert_equal, explicitly converting eitheractual ordesired to arrays if the behavior ofassert_array_equalis desired.

Parameters:
actualarray_like

The actual object to check.

desiredarray_like

The desired, expected object.

err_msgstr, optional

The error message to be printed in case of failure.

verbosebool, optional

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

strictbool, optional

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

New in version 1.24.0.

Raises:
AssertionError

If actual and desired objects are not equal.

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

Notes

When one ofactual anddesired is a scalar and the other is array_like,the function checks that each element of the array_like object is equal tothe scalar. This behaviour can be disabled with thestrict parameter.

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])Traceback (most recent call last):...AssertionError:Arrays are not equalMismatched elements: 1 / 3 (33.3%)Max absolute difference among violations: 4.4408921e-16Max relative difference among violations: 1.41357986e-16 ACTUAL: array([1.      , 3.141593,      nan]) DESIRED: array([1.      , 3.141593,      nan])

Useassert_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 specialhandling for scalars. Here the test checks that each value inx is 3:

>>>x=np.full((2,5),fill_value=3)>>>np.testing.assert_array_equal(x,3)

Usestrict to raise an AssertionError when comparing a scalar with anarray:

>>>np.testing.assert_array_equal(x,3,strict=True)Traceback (most recent call last):...AssertionError:Arrays are not equal(shapes (2, 5), () mismatch) ACTUAL: array([[3, 3, 3, 3, 3],       [3, 3, 3, 3, 3]]) DESIRED: array(3)

Thestrict 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)Traceback (most recent call last):...AssertionError:Arrays are not equal(dtypes int64, float32 mismatch) ACTUAL: array([2, 2, 2]) DESIRED: array([2., 2., 2.], dtype=float32)