简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM