将数据框行转换为Python集 [英] Convert dataframe rows to Python set
本文介绍了将数据框行转换为Python集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数据集:
import pandas as pd
import itertools
A = ['A','B','C']
M = ['1','2','3']
F = ['plus','minus','square']
df = pd.DataFrame(list(itertools.product(A,M,F)), columns=['A','M','F'])
print(df)
示例输出如下:
A M F
0 A 1 plus
1 A 1 minus
2 A 1 square
3 A 2 plus
4 A 2 minus
5 A 2 square
我想从该数据帧中逐行比较(雅卡相似度),例如,比较
I want to pairwise comparison (jaccard similarity) of each row from this data frame, for example, comparing
A 1 plus
和A 2 square
并获得两者之间的相似性值.
A 1 plus
and A 2 square
and get the similarity value between those both set.
我写了一个jaccard函数:
I have wrote a jaccard function:
def jaccard(a, b):
c = a.intersection(b)
return float(len(c)) / (len(a) + len(b) - len(c))
因为我使用了intersection
我想要这样的输出(此预期结果值只是随机数):
I want the output like this (this expected result value is just random number):
0 1 2 3 45
0 1.00 0.43 0.61 0.55 0.46
1 0.43 1.00 0.52 0.56 0.49
2 0.61 0.52 1.00 0.48 0.53
3 0.55 0.56 0.48 1.00 0.49
45 0.46 0.49 0.53 0.49 1.00
获得成对度量标准结果的最佳方法是什么?
What is the best way to get the result of pairwise metrics?
谢谢
推荐答案
可以在此处找到所需内容的完整实现:
A full implementation of what you want can be found here:
series_set = df.apply(frozenset, axis=1)
new_df = series_set.apply(lambda a: series_set.apply(lambda b: jaccard(a,b)))
这篇关于将数据框行转换为Python集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文