【问题标题】:Why spearmanr function's output changes when the input representation is changed without changing the order?为什么当输入表示改变而不改变顺序时spearmanr函数的输出会改变?
【发布时间】:2020-05-06 04:49:45
【问题描述】:

以下代码计算两个有序列表之间的 spearman 等级相关性:

from scipy.stats import spearmanr
a1 = [0, 1, 2, 3, 4]
b1 = [0, 1, 3, 2, 5]
print(spearmanr(a1,b1).correlation) # the result is 0.9

结果是 0.9。但是当我在不改变顺序的情况下表示向量时,相关性变为0.5:

a2 = ['ESR1', 'TBC1D9', 'SCUBE2', 'EVL', 'NAT2']
b2 = ['ESR1', 'TBC1D9', 'EVL', 'SCUBE2', 'CIRBP']
print(spearmanr(a2,b2).correlation) # the result is 0.5

我想知道为什么即使顺序相同,结果也会改变。

【问题讨论】:

    标签: python scipy correlation


    【解决方案1】:

    Spearman's rank correlation coefficient 基于输入的排名。它假设输入变量是有序的,也就是说,它们具有自然顺序。如果传入字符串,则该排序是字符串的字母顺序。您的两个数据版本的顺序不同:

    In [16]: a1 = [0, 1, 2, 3, 4]
    
    In [17]: b1 = [0, 1, 3, 2, 5]
    
    In [18]: a2 = ['ESR1', 'TBC1D9', 'SCUBE2', 'EVL', 'NAT2']
    
    In [19]: b2 = ['ESR1', 'TBC1D9', 'EVL', 'SCUBE2', 'CIRBP']
    
    In [20]: from scipy.stats import rankdata
    
    In [21]: rankdata(a1)
    Out[21]: array([1., 2., 3., 4., 5.])
    
    In [22]: rankdata(b1)
    Out[22]: array([1., 2., 4., 3., 5.])
    
    In [23]: rankdata(a2)
    Out[23]: array([1., 5., 4., 2., 3.])
    
    In [24]: rankdata(b2)
    Out[24]: array([2., 5., 3., 4., 1.])
    

    【讨论】:

    • 感谢您的回复。我以为spearmanr是把有序列表作为输入,比如输入列表是a,那么a[0]就是排名最高的元素,以此类推。但根据你的回答,它似乎需要每个元素的某种位置。我说的对吗?
    • 它实际上是输入的等级的皮尔逊相关系数。
    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2013-03-17
    • 2020-10-03
    • 2021-03-11
    相关资源
    最近更新 更多