bundles / numpy 2.4.4 / numpy / testing / _private / utils / assert_equal
function
numpy.testing._private.utils:assert_equal
Signature
def assert_equal ( actual , desired , err_msg = '' , verbose = True , * , strict = False ) Summary
Raises an AssertionError if two objects are not equal.
Extended Summary
Given two objects (scalars, lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised at 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 same positions. This is in contrast to the IEEE standard on NaNs, which says that NaN compared to anything must return False.
Parameters
actual: array_likeThe object to check.
desired: array_likeThe expected object.
err_msg: str, optionalThe error message to be printed in case of failure.
verbose: bool, optionalIf True, the conflicting values are appended to the error message.
strict: bool, optionalIf True and either of the
actualanddesiredarguments is an array, raise anAssertionErrorwhen either the shape or the data type of the arguments does not match. If neither argument is an array, this parameter has no effect.
Raises
: AssertionErrorIf actual and desired 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
np.testing.assert_equal([4, 5], [4, 6])
The following comparison does not raise an exception. There are NaNs
in 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 special
handling for scalars when one of the arguments is an array.
Here, the test checks that each value in `x` is 3:
x = np.full((2, 5), fill_value=3) np.testing.assert_equal(x, 3)Use `strict` to raise an AssertionError when comparing a scalar with an array of a different shape:
np.testing.assert_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_equal(x, y, strict=True)
See also
Aliases
-
numpy.testing.assert_equal