[英]compare tuple with tuples in numpy array
I have an array (dtype=object) with the first column containing tuples of arrays and the second column containing scalars. 我有一个数组(dtype = object),第一列包含数组的元组,第二列包含标量。 I want all scalars from the second column where the tuples in the first column equal a certain tuple. 我想要第二列中的所有标量,其中第一列中的元组等于某个元组。
Say 说
>>> X
array([[(array([ 21.]), array([ 13.])), 0.29452519286647716],
[(array([ 25.]), array([ 9.])), 0.9106600600510809],
[(array([ 25.]), array([ 13.])), 0.8137344043493814],
[(array([ 25.]), array([ 14.])), 0.8143093864975313],
[(array([ 25.]), array([ 15.])), 0.6004337591112664],
[(array([ 25.]), array([ 16.])), 0.6239450452872853],
[(array([ 21.]), array([ 13.])), 0.32082105959687424]], dtype=object)
and I want all rows where the 1st column equals X[0,0]. 我想要第一行等于X [0,0]的所有行。
ar = X[0,0]
>>> ar
(array([ 21.]), array([ 13.]))
I thaugh checking X[:,0]==ar
should find me those rows. 我检查X[:,0]==ar
应该找到我那些行。 I would had then retrieved my final result by X[X[:,0]==ar,1]
. 然后,我本可以通过X[X[:,0]==ar,1]
检索最终结果。
What seems to happen, however, is that ar
gets to be interpreted as a 2dimensional array and each single element in ar
is compared to the tuples in X[:,0]
. 但是,似乎发生的事情是ar
被解释为二维数组,并且ar
每个单个元素都与X[:,0]
的元组进行了比较。 This yields a, in this case, 2x7 array all entries equal to False
. 在这种情况下,这将产生一个2x7数组,所有条目等于False
。 In contrast, the comparison X[0,0]==ar
works just as I would want it giving a value of True
. 相反,比较X[0,0]==ar
工作方式与我希望给定值True
。
Why is that happening and how can I fix it to obtain the desired result? 为什么会发生这种情况,我该如何解决它才能获得理想的结果?
Comparison using list comprehension works: 使用列表推导进行比较:
In [176]: [x==ar for x in X[:,0]]
Out[176]: [True, False, False, False, False, False, True]
This is comparing tuples with tuples 这是将元组与元组进行比较
Comparing tuple ids gives a different result 比较元组ID会得出不同的结果
In [175]: [id(x)==id(ar) for x in X[:,0]]
Out[175]: [True, False, False, False, False, False, False]
since the 2nd match has a different id. 因为第二场比赛的ID不同。
In [177]: X[:,0]==ar
Out[177]:
array([[False, False, False, False, False, False, False],
[False, False, False, False, False, False, False]], dtype=bool)
returns a (2,7)
result because it is, effect comparing a (7,)
array with a (2,1)
array ( np.array(ar)
). 返回(2,7)
结果,因为将(7,)
数组与(2,1)
数组( np.array(ar)
)进行比较是有效的。
But this works like the comprehension: 但这就像理解:
In [190]: ar1=np.zeros(1,dtype=object)
In [191]: ar1[0]=ar
In [192]: ar1
Out[192]: array([(array([ 21.]), array([ 13.]))], dtype=object)
In [193]: X[:,0]==ar1
Out[193]: array([ True, False, False, False, False, False, True], dtype=bool)
art1
is a 1 element array containing the ar
tuple. art1
是包含ar
元组的1元素数组。 Now the comparison with the elements of X[:,0]
proceeds as expected. 现在,与X[:,0]
元素的比较按预期进行。
np.array(...)
tries to create as high a dimension array as the input data allows. np.array(...)
尝试创建输入数据允许的最大维度数组。 That is why it turns a 2 element tuple into a 2 element array. 这就是为什么它将2元素元组转换为2元素数组的原因。 I had to do a 2 step assignment to get around that default. 我必须进行两步分配才能解决该默认设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.