assert_array_equal(x, y, err_msg='', verbose=True)
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.
When one of x
and y
is a scalar and the other is array_like, the function checks that each element of the array_like object is equal to the scalar.
The actual object to check.
The desired, expected object.
The error message to be printed in case of failure.
If True, the conflicting values are appended to the error message.
If actual and desired objects are not equal.
Raises an AssertionError if two array_like objects are not equal.
assert_allclose
Compare two array_like objects for equality with desired relative and/or absolute precision.
The first assert does not raise an exception:
This example is valid syntax, but we were not able to check execution>>> 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:
This example is valid syntax, but we were not able to check execution>>> 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 equal <BLANKLINE> Mismatched elements: 1 / 3 (33.3%) Max absolute difference: 4.4408921e-16 Max relative difference: 1.41357986e-16 x: array([1. , 3.141593, nan]) y: array([1. , 3.141593, 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)See :
... np.testing.assert_array_equal(x, 3)
The following pages refer to to this document either explicitly or contain code examples using this.
numpy.testing._private.utils.assert_array_less
numpy.testing._private.utils.assert_array_equal
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them