【问题标题】:pandas: calculate fuzzywuzzy for each category separatelypandas:分别计算每个类别的模糊模糊
【发布时间】:2020-12-16 14:12:49
【问题描述】:

我有一个数据集如下,只有更多行:

import pandas as pd

data = {'First':  ['First value','Third value','Second value','First value','Third value','Second value'],
'Second': ['the old man is here','the young girl is there', 'the old woman is here','the  young boy is there','the young girl is here','the old girl is here']}

df = pd.DataFrame (data, columns = ['First','Second'])

我计算了整个数据集的模糊模糊平均值,如下所示:

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

def similarity_measure(doc1, doc2): 
    return fuzz.token_set_ratio(doc1, doc2)


d= df.groupby('First')['Second'].apply(lambda x: (', '.join(x)))
d= d.reset_index()
all=[]
for val in list(combinations(range(len(d)), 2)):
    all.append(similarity_measure(d.iloc[val[0],1],d.iloc[val[1],1]))


avg = sum(all)/len(all)
print('lexical overlap between all example pairs in the dataset is: ', avg)

但是,我还想分别获得第一列中每个类别的平均值。 所以,我想要类似的东西(例如):

similarity average for sentences in First value: 85.56
similarity average for sentences in Second value: 89.01
similarity average for sentences in Third value: 90.01

所以我想修改 for 循环,使其具有上述输出。

【问题讨论】:

    标签: python-3.x pandas average categories fuzzywuzzy


    【解决方案1】:

    要计算每个组内的平均值,您需要两个步骤:

    1. 按某些标准分组,在您的案例列First。看来你已经知道怎么做了。
    2. 使用下面代码中的 all_similarity_measure 函数创建一个函数来计算组的相似度。

    代码

    import pandas as pd
    from fuzzywuzzy import fuzz
    from itertools import combinations
    
    
    def similarity_measure(doc1, doc2):
        return fuzz.token_set_ratio(doc1, doc2)
    
    
    data = {'First': ['First value', 'Third value', 'Second value', 'First value', 'Third value', 'Second value'],
            'Second': ['the old man is here', 'the young girl is there', 'the old woman is here', 'the  young boy is there',
                       'the young girl is here', 'the old girl is here']}
    
    df = pd.DataFrame(data, columns=['First', 'Second'])
    
    
    def all_similarity_measure(gdf):
        """This function computes the similarity between all pairs of sentences in a Series"""
        return pd.Series([similarity_measure(*docs) for docs in combinations(gdf, 2)]).mean()
    
    
    res = df.groupby('First', as_index=False)['Second'].apply(all_similarity_measure)
    print(res)
    

    输出

              First  Second
    0   First value    63.0
    1  Second value    86.0
    2   Third value    98.0
    

    计算平均相似度的关键是这个表达式:

    return pd.Series([similarity_measure(*docs) for docs in combinations(gdf, 2)]).mean()
    

    基本上,您使用combinations 生成句子对(无需通过索引访问),构造一个系列并在其上计算mean

    可以使用任何计算均值的函数来代替上述函数,例如,您可以使用statistics.mean,以避免构造系列。

    from statistics import mean
    
    def all_similarity_measure(gdf):
        """This function computes the similarity between all pairs of sentences in a Series"""
        return mean(similarity_measure(*docs) for docs in combinations(gdf, 2))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      相关资源
      最近更新 更多