【问题标题】:Python: How to find correlation of 2 columns having list of values?Python:如何找到具有值列表的 2 列的相关性?
【发布时间】:2021-01-08 15:27:29
【问题描述】:

我有一个类似的数据框

    title                                               titlenew
0   Two Workers Are Struck By Motor Vehicle And O...    two workers are struck by motor vehicle and o...
1   Foreman Is Fatally Crushed When Forklift Tips...    foreman is fatally crushed when forklift tips...
2   Employee Suffers Abdominal Fracture In Fall F...    employee suffers abdominal fracture in fall f...
3   Employee'S Body Is Caught In Asphalt Machine ...    employee's body is caught in asphalt machine ...
4   Employee Is Punctured In Abdomen With Nail  employee is punctured in abdomen with nail

我转换为向量用于 NLP 处理。它们现在看起来像

    card2vec_title                                      card2vec_titlenew
0   [0.09446411579847336, 0.18325935304164886, 0.1...   [0.01013200543820858, -0.015507892705500126, 0...
1   [0.11135150492191315, 0.16989260911941528, 0.1...   [0.0871051624417305, 0.07891112565994263, -0.0...
2   [-0.019224125891923904, 0.3285079598426819, -0...   [0.052899472415447235, 0.2530696988105774, -0....
3   [0.06179530546069145, 0.10462947934865952, 0.0...   [0.05848287418484688, 0.062050893902778625, -0...
4   [0.0604548417031765, 0.2742682993412018, -0.00...   [0.09018705040216446, 0.23053207993507385, -0.

我的问题是,我怎样才能找到这两列的相关分数(或余弦相似度)。执行df.card2vec_titlenew.corr(df.card2vec_title) 会出现错误提示

/ 不支持的操作数类型:'list' 和 'int'

我的问题是相关性,因此没有提供我如何将字符串转换为向量的代码。帮助表示赞赏。谢谢

【问题讨论】:

  • 这里有什么帮助吗?
  • 显示您的代码。您应该能够从数据帧 sns.heatmap(weights_df.corr(), center=0, cmap=cmap, linewidths=1, annot=True, fmt=".2f") 运行 corr 并将其发送到热图。 mask = np.triu(np.ones_like(corr, dtype=bool)) sns.heatmap(corr, cmap=cmap, center=0, linewidths=1, annot=True, fmt=".2f",mask=mask)

标签: python pandas nlp cosine-similarity


【解决方案1】:

相关性是一对一或多对一函数,因此 list 和 int 会出错,因为它需要一个 int 值而不是值列表。

你可以做的事情:

  • 对每一行中的多个值做点积,然后找出两列之间的相关性

  • 另一种方法是拆分每个值的列(类似于this),然后找到新列之间的相关性。但是,如果您的列的单词长度不同,这可能会很棘手。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多