![](/img/trans.png)
[英]What is the behaviour of operations on two pandas Series with non-unique labels?
[英]How to get the Intersection and Union of two Series in Pandas with non-unique values?
如果我有2个Series对象,例如:[0,0,1] [1,0,0]我将如何获取两者的交集和并集? 它们仅包含布尔值,这意味着它们是非唯一值。
我有一个大的布尔矩阵。 我已经将其最小化,现在我正试图找到误报和误报,我认为这意味着我必须为每个原始对获取Jaccard相似度。
由于您说的是布尔值,因此请使用numpy或&
和|
logical_and
和logical_or
。 在系列上
y1 = pd.Series([1,0,1,0])
y2 = pd.Series([1,0,0,1])
# Numpy approach
intersection = np.logical_and(y1.values, y2.values)
union = np.logical_or(y1.values, y2.values)
intersection.sum() / union.sum()
# 0.33333333333333331
# Pandas approach
sum(y1 & y2) / sum(y1 | y2)
# 0.33333333333333331
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.