如何在Pandas中找到Jaccard类的Jaccard相似性

时间:2017-01-27 11:00:29

标签: python pandas scikit-learn

我想在我的数据集中找到每对组之间的Jaccard相似性。我的数据如下,第一列是我的数据,第二列是类标签:

import pandas as pd
import numpy as np
df = pd.DataFrame({'Data' : ["a1","a2","a3","a4","a5","a6","a7"], 'ClassLable' :     ["c1","c2","c2","c2","c3","c3","c1"]}); df
df2 = pd.DataFrame({'Data' : ["a1","a2","a4","a6","a7","a8","a9"], 'ClassLable' : ["c11","c21","c21","c12","c13","c13","c11"]}); df2

我想计算df和df2之间每对Class Lable的Jaccard Index。例如:

c1class = pd.DataFrame({'Data':["a1","a7"]})
c11class = pd.DataFrame({'Data':["a1","a9"]})
Jaccard = 1/3

换句话说,对于df1和df2,我想在每个类标签上找到联合项目的联合项目

1 个答案:

答案 0 :(得分:1)

你在寻找这样的东西:

from sklearn.metrics import jaccard_similarity_score

jaccard_similarity_score(df['Data'],df2['Data'])
Out[92]: 0.2857142857142857

jaccard_similarity_score(c1class, c11class)
Out[93]: 0.5