【问题标题】:Python: How to pass Dataframe Columns as parameters to a function?Python:如何将数据框列作为参数传递给函数?
【发布时间】:2021-04-04 11:32:44
【问题描述】:

我有一个数据框df,其中包含两列文本嵌入,即embedding_1embedding_2。我想在df 中创建名为distances 的第三列,其中应包含embedding_1embedding_2 的每一行之间的余弦相似度。

但是当我尝试使用下面的代码来实现它时,我得到了一个ValueError

如何解决?

数据框df

           embedding_1              |            embedding_2                                 
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.49163356, -0.4877703,...]]
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.06686627, -0.75147504...]]
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.42776933, -0.88310856,...]]
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.6520882, -1.049325,...]]
 [[-0.28876397, -0.6367827, ...]]   |  [[-1.4216679, -0.8930428,...]]

计算余弦相似度的代码

df['distances'] = cosine_similarity(df['embeddings_1'], df['embeddings_2'])

错误

ValueError: setting an array element with a sequence.

必需的数据框

       embedding_1              |            embedding_2                 |  distances                        
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.49163356, -0.4877703,...]]   |    0.427
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.06686627, -0.75147504...]]   |    0.673
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.42776933, -0.88310856,...]]  |    0.882
 [[-0.28876397, -0.6367827, ...]]   |  [[-0.6520882, -1.049325,...]]     |    0.665
 [[-0.28876397, -0.6367827, ...]]   |  [[-1.4216679, -0.8930428,...]]    |    0.312

【问题讨论】:

    标签: python pandas dataframe nlp bert-language-model


    【解决方案1】:

    您可以使用apply() 在每一行上使用cosine_similarity()

    def cal_cosine_similarity(row):
        return cosine_similarity(row['embeddings_1'], row['embeddings_2'])
    
    df['distances'] = df.apply(cal_cosine_similarity, axis=1)
    

    或一个班轮

    df['distances'] = df.apply(lambda row: cosine_similarity(row['embeddings_1'], row['embeddings_2']), axis=1)
    

    【讨论】:

      猜你喜欢
      • 2019-07-23
      • 2023-03-28
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2019-08-21
      • 2013-01-27
      • 2020-12-26
      相关资源
      最近更新 更多