python 中的 Jaccard 相似度

Jaccard similarity in python

我正在尝试查找两个文档之间的 jaccard 相似度。但是,根据我对 Jaccard 的 sim = 文档中术语的交集/文档中术语的并集的理解,我很难理解函数 sklearn.metrics.jaccard_similarity_score() 在 scene.As 背后是如何工作的。

考虑以下示例: 我对这两个文档的 DTM 是:

array([[1, 1, 1, 1, 2, 0, 1, 0],
       [2, 1, 1, 0, 1, 1, 0, 1]], dtype=int64)

以上功能。给我 jaccard sim 分数

print(sklearn.metrics.jaccard_similarity_score(tf_matrix[0,:],tf_matrix[1,:]))
0.25

我正在尝试自己查找分数:

intersection of terms in both the docs = 4
total terms in doc 1 = 6
total terms in doc 2 = 6
Jaccard = 4/(6+6-4)= .5

有人可以帮助我理解我是否在这里遗漏了一些明显的东西。

如前所述here

In binary and multiclass classification, the Jaccard similarity coefficient score is equal to the classification accuracy.

所以你的例子是在计算匹配元素的比例。这就是您得到 0.25 作为结果的原因。

According to me

intersection of terms in both the docs = 2.

peek to peek intersection according to their respective index. As we need to predict correct value for our model.

Normal Intersection = 4. Leaving the order of index.

# so,
jaccard_score = 2/(6+6-4) = 0.25