numpy.testing.assert_array_less#

testing.assert_array_less(x, y, err_msg='', verbose=True, *, strict=False)[源代码]#

如果两个类数组对象的顺序不是小于,则引发 AssertionError.

给定两个类数组对象 xy ,检查形状是否相等,并且 x 的所有元素是否严格小于 y 的对应元素(但请参阅注释以了解标量的特殊处理). 如果形状不匹配或值的顺序不正确,则会引发异常. 与 NumPy 中的标准用法相反,如果两个对象在相同的位置都有 NaN,则不会引发断言.

参数:
xarray_like

要检查的较小对象.

yarray_like

要比较的更大对象.

err_msg字符串

失败时要打印的错误消息.

verbosebool

如果为 True,则冲突的值将附加到错误消息.

strictbool,可选

如果为 True,则当类数组对象的形状或数据类型不匹配时,引发 AssertionError. 将禁用注释部分中提到的标量的特殊处理.

在 2.0.0 版本加入.

Raises:
AssertionError

如果 x 不是严格小于 y,逐元素.

参见

assert_array_equal

测试对象的等价性

assert_array_almost_equal

测试对象是否在精度范围内相等

注释

xy 之一是标量,而另一个是类数组时,该函数执行比较,就好像标量被广播到数组的形状一样. 可以使用 strict 参数禁用此行为.

示例

以下断言通过,因为 x 的每个有限元素都严格小于 y 的对应元素,并且 NaN 位于相应的位置.

>>> x = [1.0, 1.0, np.nan]
>>> y = [1.1, 2.0, np.nan]
>>> np.testing.assert_array_less(x, y)

以下断言失败,因为 x 的第零个元素不再严格小于 y 的第零个元素.

>>> y[0] = 1
>>> np.testing.assert_array_less(x, y)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not strictly ordered `x < y`

Mismatched elements: 1 / 3 (33.3%)
Max absolute difference among violations: 0.
Max relative difference among violations: 0.
 x: array([ 1.,  1., nan])
 y: array([ 1.,  2., nan])

在这里, y 是一个标量,因此 x 的每个元素都与 y 进行比较,并且断言通过.

>>> x = [1.0, 4.0]
>>> y = 5.0
>>> np.testing.assert_array_less(x, y)

但是,使用 strict=True ,断言将失败,因为形状不匹配.

>>> np.testing.assert_array_less(x, y, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not strictly ordered `x < y`

(shapes (2,), () mismatch)
 x: array([1., 4.])
 y: array(5.)

使用 strict=True ,如果两个数组的dtypes不匹配,断言也会失败.

>>> y = [5, 5]
>>> np.testing.assert_array_less(x, y, strict=True)
Traceback (most recent call last):
    ...
AssertionError:
Arrays are not strictly ordered `x < y`

(dtypes float64, int64 mismatch)
 x: array([1., 4.])
 y: array([5, 5])