【问题标题】:Estimate correlation in Python在 Python 中估计相关性
【发布时间】:2020-09-05 00:53:37
【问题描述】:

我有一个带有标签和用户名的数据集:

Labels   Usernames
1         Londonderry
1         Londoncalling
1          Steveonder43
0         Maryclare_re
1         Patent107391
0         Anonymous 
1         _24londonqr
... 

我需要证明包含单词 London 和标签 1 的用户名之间存在相关性。为此,我创建了第二个标签来查看单词 London 的位置

for idx, username in df['Usernames']:
    if 'London' in username:
        df['London'].iloc[idx] = 1
    else:
        df['London'].iloc[idx] = 0

然后我比较了这些二元变量,使用 Pearson 相关系数:

import scipy.stats.pearsonr as rho
corr = rho(df['labels'], df['London'])

但是它不起作用。 我在上述步骤中遗漏了什么吗?

【问题讨论】:

    标签: python pandas scipy pearson-correlation


    【解决方案1】:

    您的数据框中有Labels,但您传递了labels,我还通过contains 增强了代码

    df['London'] = df['Usernames'].str.contains('London').astype(int)
    from scipy import stats
    stats.pearsonr(df['Labels'], df['London'])
    Out[12]: (0.4, 0.37393392381774704)
    

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2018-05-01
      • 2020-05-23
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多