问题描述
我有一个数组 (dtype=object),第一列包含数组元组,第二列包含标量.我想要第二列中的所有标量,其中第一列中的元组等于某个元组.
说
>>>X数组([[(数组([ 21.]), 数组([ 13.])), 0.29452519286647716],[(数组([ 25.]), 数组([ 9.])), 0.9106600600510809],[(数组([ 25.]), 数组([ 13.])), 0.8137344043493814],[(数组([ 25.]), 数组([ 14.])), 0.8143093864975313],[(数组([ 25.]), 数组([ 15.])), 0.6004337591112664],[(数组([ 25.]), 数组([ 16.])), 0.6239450452872853],[(数组([ 21.]), 数组([ 13.])), 0.32082105959687424]], dtype=object)我想要第一列等于 X[0,0] 的所有行.
ar = X[0,0]>>>ar(数组([ 21.]), 数组([ 13.]))
我虽然检查 X[:,0]==ar
应该能找到那些行.然后我会通过 X[X[:,0]==ar,1]
检索我的最终结果.
然而,似乎发生的是 ar
被解释为一个二维数组,并且 ar
中的每个元素都与 中的元组进行比较X[:,0]
.在这种情况下,这会产生一个 2x7 数组,所有条目都等于 False
.相比之下,比较 X[0,0]==ar
就像我希望它给出 True
的值一样工作.
为什么会发生这种情况,我该如何修复它以获得所需的结果?
比较使用列表推导式工作:
In [176]: [x==ar for x in X[:,0]]出[176]:[真,假,假,假,假,假,真]
这是元组与元组的比较
比较元组 ID 会得到不同的结果
In [175]: [id(x)==id(ar) for x in X[:,0]]出[175]:[真,假,假,假,假,假,假]
因为第二场比赛的 ID 不同.
在 [177]: X[:,0]==ar出[177]:数组([[假,假,假,假,假,假,假],[假,假,假,假,假,假,假]],dtype=bool)
返回一个 (2,7)
结果,因为它是将 (7,)
数组与 (2,1)
数组进行比较的效果> 数组 (np.array(ar)
).
但这就像理解一样:
在[190]中:ar1=np.zeros(1,dtype=object)在 [191] 中:ar1[0]=ar在 [192]: ar1出[192]:数组([(数组([21.]),数组([13.]))],dtype=object)在 [193] 中:X[:,0]==ar1出[193]:数组([真,假,假,假,假,假,真],dtype=bool)
art1
是一个包含 ar
元组的 1 元素数组.现在与 X[:,0]
元素的比较按预期进行.
np.array(...)
尝试创建一个输入数据允许的尽可能高的维度数组.这就是为什么它将 2 元素元组转换为 2 元素数组.我不得不做一个两步分配来绕过这个默认设置.
I have an array (dtype=object) with the first column containing tuples of arrays and the second column containing scalars. 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].
ar = X[0,0]
>>> ar
(array([ 21.]), array([ 13.]))
I thaugh checking X[:,0]==ar
should find me those rows. I would had then retrieved my final result by 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]
. This yields a, in this case, 2x7 array all entries equal to False
. In contrast, the comparison X[0,0]==ar
works just as I would want it giving a value of 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
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.
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)
).
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. Now the comparison with the elements of X[:,0]
proceeds as expected.
np.array(...)
tries to create as high a dimension array as the input data allows. That is why it turns a 2 element tuple into a 2 element array. I had to do a 2 step assignment to get around that default.
这篇关于将元组与 numpy 数组中的元组进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!