【问题标题】:Compute Spearman correlation with pandas计算与 pandas 的 Spearman 相关性
【发布时间】:2019-04-15 05:34:29
【问题描述】:

当按降序排序时,我必须计算我计算的 (name, score) list 之间的 Spearman 相关性。

您可以假设没有固定等级,这意味着可以使用更简单的 Spearman 计算形式。

我可以在不使用scipy 的情况下计算吗?我试过使用scipy.stats 模块,但什么也没发生。我也可以使用scipy

 import scipy.stats
 x=[['x',0.212],['a', 0.324],['s', -2.432], ['b',0.2342],['x', -3.232]]
 def spearman(l):
    c = []
    for i in l:
         c.append(scipy.stats.pearsonr(i,i+1))
    return c
 computed_spearman = spearman(x)

我收到以下错误:

Traceback(最近一次调用最后一次):文件“”,第 2 行,in TypeError:只能将列表(不是“int”)连接到列表

【问题讨论】:

  • 错误是由表达式 i+1 引起的。我将是一个列表,例如['x', 0.212],在您的第一次迭代中, i+1 将尝试将 1 附加到列表中。你的意思是在列表中取第 i 个元素吗?
  • 另外我不明白你为什么使用 pearsonr 而不是 spearmanr 以及 pandas 如何适应所有这些。也许您可以改写您的问题以明确您想要做什么。

标签: python python-3.x scipy


【解决方案1】:

如果您想使用 pandas 计算 spearman 相关性,您可以按以下方式进行:

import pandas as pd

x = [['x',0.212], ['a', 0.324], ['s', -2.432], ['b',0.2342], ['x', -3.232]]

df = pd.DataFrame(data=x)
df.corr(method='spearman')

但是,我认为这对您的数据没有任何意义。也许你可以澄清你的意图。

【讨论】:

    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2020-11-18
    • 2018-05-09
    • 2016-03-16
    • 1970-01-01
    相关资源
    最近更新 更多