numpy.testing.assert_equal#

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

Raises an AssertionError if two objects are not equal.

Given two objects (scalars, lists, tuples, dictionaries or numpy arrays),check that all elements of these objects are equal. An exception is raisedat the first conflicting values.

This function handles NaN comparisons as if NaN was a “normal” number.That is, AssertionError is not raised if both objects have NaNs in the samepositions. This is in contrast to the IEEE standard on NaNs, which saysthat NaN compared to anything must return False.

Parameters:
actualarray_like

The object to check.

desiredarray_like

The 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 and either of theactual anddesired arguments is an array,raise anAssertionError when either the shape or the data type ofthe arguments does not match. If neither argument is an array, thisparameter has no effect.

New in version 2.0.0.

Raises:
AssertionError

If actual and desired are not equal.

Notes

By default, when one ofactual anddesired is a scalar and the other isan array, the function checks that each element of the array is equal tothe scalar. This behaviour can be disabled by settingstrict==True.

Examples

>>>np.testing.assert_equal([4,5],[4,6])Traceback (most recent call last):...AssertionError:Items are not equal:item=1 ACTUAL: 5 DESIRED: 6

The following comparison does not raise an exception. There are NaNsin the inputs, but they are in the same positions.

>>>np.testing.assert_equal(np.array([1.0,2.0,np.nan]),[1,2,np.nan])

As mentioned in the Notes section,assert_equal has specialhandling for scalars when one of the arguments is an array.Here, the test checks that each value inx is 3:

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

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

>>>np.testing.assert_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_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)