簡體   English   中英

如何比較 itertools.combinations 中的元素?

[英]How to Compare Elements in itertools.combinations?

我想比較兩個series的元素。

0    1
1    3
2    4
3    2
4    4
Name: s1, dtype: int32
0    3
1    3
2    0
3    5
4    1
Name: s2, dtype: int64 

為了輕松比較series ,我使用了itertools.combinations

x = combinations(s1, 2)
y = combinations(s2, 2)

結果x

(1, 3)
(1, 4)
(1, 2)
(1, 4)
(3, 4)
(3, 2)
(3, 4)
(4, 2)
(4, 4)
(2, 4)

Y:

(3, 3)
(3, 0)
(3, 5)
(3, 1)
(3, 0)
(3, 5)
(3, 1)
(0, 5)
(0, 1)
(5, 1)

比較的方法部分類似於 Kendall 的 tau 距離。 x (x1, x2)的對和y (y1, y2) 如果x1 > x2y1 > y2 ,或x1 < x2y1 < y2 ,則score = score+1 否則, score = score 但到目前為止,我仍然無法找到一種方法來比較成對中的元素。


我得到m1m2m1|m2

米1:

0    False
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool

平方米:

0    False
1    False
2     True
3    False
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool

米1|米2:

0    False
1    False
2     True
3    False
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool

我得到了和你一樣的結果。 我不知道為什么它加起來這么多時間。


m1 和 m2 都包含默認值中的所有錯誤值。 確實如此,而且目前的結果在理想情況下是正確的。 但我希望score每次都加 1 (m1 | m2) == true

score
0
0
1
0
0
0
0
0
0
0

像上面這樣的分數的理想結果。

您可以從輸出創建DataFrame ,然后按條件修改數據:

#changed data for better sample
s1 = pd.Series([1,3,4,2,4])
s2 = pd.Series([3,4,0,5,8])

x = combinations(s1, 2)
y = combinations(s2, 2)

dfx = pd.DataFrame(list(x)).rename(columns=lambda x: x+1).add_prefix('x')
dfy = pd.DataFrame(list(y)).rename(columns=lambda x: x+1).add_prefix('y')
df = pd.concat([dfx, dfy], axis=1)

m1 = (df.x1 > df.x2) & (df.y1 > df.y2)
m2 = (df.x1 < df.x2) & (df.y1 < df.y2)
m = m1 | m2

print (m)
0     True
1    False
2     True
3     True
4    False
5    False
6     True
7    False
8    False
9     True
dtype: bool

df['score'] = np.where(m, m.cumsum(), 0)
print (df)
   x1  x2  y1  y2  score
0   1   3   3   4      1
1   1   4   3   0      0
2   1   2   3   5      2
3   1   4   3   8      3
4   3   4   4   0      0
5   3   2   4   5      0
6   3   4   4   8      4
7   4   2   0   5      0
8   4   4   0   8      0
9   2   4   5   8      5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM