比较ntype对象的numpy数组

Don*_*ong 7 python numpy python-3.x

我的问题是"为什么?:"

aa[0]
array([[405, 162, 414, 0,
        array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],
      dtype=object),
        0, 0, 0]], dtype=object)

aaa
array([[405, 162, 414, 0,
        array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],
      dtype=object),
        0, 0, 0]], dtype=object)

np.array_equal(aaa,aa[0])
False
Run Code Online (Sandbox Code Playgroud)

那些数组完全相同.

我的最小例子不会重现这个:

be=np.array([1],dtype=object)

be
array([1], dtype=object)

ce=np.array([1],dtype=object)

ce
array([1], dtype=object)

np.array_equal(be,ce)
True
Run Code Online (Sandbox Code Playgroud)

这也不是:

ce=np.array([np.array([1]),'5'],dtype=object)

be=np.array([np.array([1]),'5'],dtype=object)

np.array_equal(be,ce)
True
Run Code Online (Sandbox Code Playgroud)

但是,为了重现我的问题,试试这个:

be=np.array([[405, 162, 414, 0, np.array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],dtype=object),0, 0, 0]], dtype=object)

ce=np.array([[405, 162, 414, 0, np.array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],dtype=object),0, 0, 0]], dtype=object)

np.array_equal(be,ce)
False

np.array_equal(be[0],ce[0])
False
Run Code Online (Sandbox Code Playgroud)

我不知道为什么那些不平等.并添加奖金问题,我该如何比较它们?

我需要一种有效的方法来检查aaa是否在堆栈中aa.

我没有使用aaa in aa因为DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.因为False如果有人想知道它仍然会回来.


我还尝试了什么?:

np.equal(be,ce)
*** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

np.all(be,ce)
*** TypeError: only integer scalar arrays can be converted to a scalar index

all(be,ce)
*** TypeError: all() takes exactly one argument (2 given)

all(be==ce)
*** TypeError: 'bool' object is not iterable

np.where(be==ce)
(array([], dtype=int64),)
Run Code Online (Sandbox Code Playgroud)

而这些,我无法在控制台中运行,所有这些都评估为False,一些给出了弃用警告:

import numpy as np

ce=np.array([[405, 162, 414, 0, np.array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],dtype=object),0, 0, 0]], dtype=object)

be=np.array([[405, 162, 414, 0, np.array([list([1, 9, 2]), 18, (405, 18, 207), 64, 'Universal'],dtype=object),0, 0, 0]], dtype=object)

print(np.any([bee in ce for bee in be]))

print(np.any([bee==cee for bee in be for cee in ce]))

print(np.all([bee in ce for bee in be]))

print(np.all([bee==cee for bee in be for cee in ce]))
Run Code Online (Sandbox Code Playgroud)

当然还有其他问题告诉我这应该有用......

kma*_*o23 5

要在数组之间进行元素比较,可以使用numpy.equal()关键字参数,dtype=numpy.object如下所示:

In [60]: np.equal(be, ce, dtype=np.object)
Out[60]: 
array([[True, True, True, True,
        array([ True,  True,  True,  True,  True]), True, True, True]],
      dtype=object)
Run Code Online (Sandbox Code Playgroud)

使用NumPy版本1.15.2 和Python 检查PS3.6.6

编辑

从1.15的发行说明中,

https://docs.scipy.org/doc/numpy-1.15.1/release.html#comparison-ufuncs-accept-dtype-object-overriding-the-default-bool

Comparison ufuncs accept dtype=object, overriding the default bool

This allows object arrays of symbolic types, which override == and 
other operators to return expressions, to be compared elementwise with 
np.equal(a, b, dtype=object).
Run Code Online (Sandbox Code Playgroud)