问题描述
我的问题是为什么?:"
My question is "why?:"
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
那些数组完全相同.
我的最小示例没有重现这一点:
My minimal example doesn't reproduce this:
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
这个也没有:
ce=np.array([np.array([1]),'5'],dtype=object)
be=np.array([np.array([1]),'5'],dtype=object)
np.array_equal(be,ce)
True
但是,要重现我的问题,请尝试以下操作:
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
我不知道为什么它们不相等.还有一个额外的问题,我该如何比较它们?
And I have no idea why those are not equal. And to add the bonus question, how do I compare them?
我需要一种有效的方法来检查 aaa 是否在堆栈 aa 中.
我没有在 aa 中使用 aaa
,因为 DeprecationWarning: elementwise == 比较失败;这将在未来引发错误.
并且因为如果有人想知道它仍然返回 False
.
I'm not using aaa in aa
because of DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
and because it still returns False
if anyone is wondering.
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),)
而这些,我无法在控制台中运行,全部评估为 False,有些给出弃用警告:
And these, which I can't get to run in the console, all evaluate to False, some giving the deprecation warning:
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]))
当然还有其他问题告诉我这应该可行...
And of course other questions telling me this should work...
推荐答案
要在数组之间进行元素比较,您可以使用 numpy.equal()
带有关键字参数 dtype=numpy.object
如:
To make an element-wise comparison between the arrays, you can use numpy.equal()
with the keyword argument dtype=numpy.object
as in :
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)
P.S. 使用 NumPy 版本 1.15.2
和 Python 3.6.6
P.S. checked using NumPy version 1.15.2
and Python 3.6.6
来自 1.15 的发行说明,
From the release notes for 1.15,
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).
这篇关于比较 dtype 对象的 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!