【发布时间】:2021-01-02 17:28:19
【问题描述】:
我正在尝试比较两个相同的嵌套 numpy 数组:
In [1]: a1 = np.array([np.array([1, 1, 1]), np.array([2, 2, 2]), np.array([3])], dtype=object)
In [2]: a2 = np.array([np.array([1, 1, 1]), np.array([2, 2, 2]), np.array([3])], dtype=object)
我看到here,为了比较两个 dtype 对象数组,我需要使用 numpy.equal() 和关键字参数 dtype=numpy.object。
In [3]: e = np.equal(a1, a2, dtype=object)
In [4]: e
Out[4]:
array([array([ True, True, True]), array([ True, True, True]),
array([ True])], dtype=object)
但是,e.all 引发了异常:
In [5]: e.all()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-129-afa4ead28930> in <module>
----> 1 e.all()
/usr/local/lib/python3.8/dist-packages/numpy/core/_methods.py in _all(a, axis, dtype, out, keepdims)
55
56 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
---> 57 return umr_all(a, axis, dtype, out, keepdims)
58
59 def _count_reduce_items(arr, axis):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
如何比较这些数组?
【问题讨论】: