【问题标题】:is numpy testing assert_array_less working correctly with np.inf?numpy 测试 assert_array_less 是否与 np.inf 一起正常工作?
【发布时间】:2016-01-27 13:30:00
【问题描述】:

我在测试具有无限类型的数组时发现了这种奇怪的行为。

这很好用:

In [91]: np.testing.assert_array_less(5, 6)

In [92]: np.testing.assert_array_less(5, np.array([6]))

In [93]: 5 < np.inf
Out[93]: True

但是在使用 numpy 测试模块时,会出现 5 不小于 inf:

In [94]: np.testing.assert_array_less(5, np.array([np.inf]))
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-93-c43a15aa8a1a> in <module>()
----> 1 np.testing.assert_array_less(5, np.array([np.inf]))

c:\python27\lib\site-packages\numpy\testing\utils.pyc in assert_array_less(x, y, err_msg, verbose)
    911     assert_array_compare(operator.__lt__, x, y, err_msg=err_msg,
    912                          verbose=verbose,
--> 913                          header='Arrays are not less-ordered')
    914
    915 def runstring(astr, dict):

c:\python27\lib\site-packages\numpy\testing\utils.pyc in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision)
    629             if any(x_isinf) or any(y_isinf):
    630                 # Check +inf and -inf separately, since they are different
--> 631                 chk_same_position(x == +inf, y == +inf, hasval='+inf')
    632                 chk_same_position(x == -inf, y == -inf, hasval='-inf')
    633

c:\python27\lib\site-packages\numpy\testing\utils.pyc in chk_same_position(x_id, y_id, hasval)
    606                                 % (hasval), verbose=verbose, header=header,
    607                                 names=('x', 'y'), precision=precision)
--> 608             raise AssertionError(msg)
    609
    610     try:

AssertionError:
Arrays are not less-ordered

x and y +inf location mismatch:
 x: array(5)
 y: array([ inf])

为什么 numpy 会检查 infs 是否在相同的位置?这是期望的行为吗?

In [99]: np.__version__
Out[99]: '1.9.3'

【问题讨论】:

标签: python numpy testing


【解决方案1】:

正如 Warren 在他的评论中提到的,这是 numpy 中的一个错误。它已在当前的 master 中修复,因此可能会在即将发布的 1.13 版本中修复:

In [1]: import numpy as np

In [2]: np.testing.assert_array_less(5, np.array([np.inf]))

In [3]: np.testing.assert_array_less(-np.inf, np.array([np.inf]))

In [4]: np.testing.assert_array_less(-np.inf, 5)

In [5]: np.testing.assert_array_less(np.inf, 5)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-5-a31a27ca3803> in <module>()
----> 1 np.testing.assert_array_less(np.inf, 5)

/XXX/lib/python2.7/site-packages/numpy/testing/utils.pyc in assert_array_less(x, y, err_msg, verbose)
   1029                          verbose=verbose,
   1030                          header='Arrays are not less-ordered',
-> 1031                          equal_inf=False)
   1032
   1033

/XXX/lib/python2.7/site-packages/numpy/testing/utils.pyc in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)
    773                                 names=('x', 'y'), precision=precision)
    774             if not cond:
--> 775                 raise AssertionError(msg)
    776     except ValueError:
    777         import traceback

AssertionError:
Arrays are not less-ordered

(mismatch 100.0%)
 x: array(inf)
 y: array(5)

In [6]: np.__version__
Out[6]: '1.13.0.dev0+c5e1773'

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 2015-01-03
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 1970-01-01
    • 2011-08-18
    • 2012-02-02
    相关资源
    最近更新 更多