1

如果我有 2 个系列对象,如下所示: [0,0,1] [1,0,0] 如何获得两者的交集和并集?它们只包含布尔值,这意味着它们是非唯一值。

我有一个大的布尔矩阵。我已经对其进行了minhashed,现在我试图找到误报和误报,我认为这意味着我必须获得每个原始对的 Jaccard 相似性。

4

1 回答 1

1

既然您说它们是布尔值,则使用logical_andand logical_ornumpy or &and |on 系列,即

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
于 2017-11-18T06:11:07.757 回答