# 使用Python计算非参数的秩相关

AiTechYun

Spearman秩相关

Kendall秩相关

Spearman秩相关。

Kendall秩相关。

Goodman和Kruskal秩相关。

Somers秩相关。

# prepare datadata1 = rand(1000) * 20data2 = data1 + (rand(1000) * 10)

# generate related variablesfrom numpy.random import randfrom numpy.random import seedfrom matplotlib import pyplot# seed random number generatorseed(1)# prepare datadata1 = rand(1000) * 20data2 = data1 + (rand(1000) * 10)# plotpyplot.scatter(data1, data2)pyplot.show()

Spearman秩相关

Spearman秩相关以Charles Spearman命名。

Spearman等级顺序相关是一种旨在度量序数测量尺度上两个变量之间关系的统计过程。

- 第124页，Nonparametric Statistics for Non-Statisticians: A Step-by-Step Approach，2009年。

Spearman秩相关的直觉是，它使用秩值而不是实际值来计算Pearson相关。Pearson相关性由两个变量中每个变量的方差或分布的标准化的协方差计算。

Spearman的秩相关可以在Python中使用SciPy函数spearmanr（）计算。

# calculate spearman's correlationcoef, p = spearmanr(data1, data2)

# calculate the spearman's correlation between two variablesfrom numpy.random import randfrom numpy.random import seedfrom scipy.stats import spearmanr# seed random number generatorseed(1)# prepare datadata1 = rand(1000) * 20data2 = data1 + (rand(1000) * 10)# calculate spearman's correlationcoef, p = spearmanr(data1, data2)print('Spearmans correlation coefficient: %.3f' % coef)# interpret the significancealpha = 0.05if p > alpha:print('Samples are uncorrelated (fail to reject H0) p=%.3f' % p)else:print('Samples are correlated (reject H0) p=%.3f' % p)

Spearmans correlation coefficient: 0.900Samples are correlated (reject H0) p=0.000Kendall秩相关

Kendall秩相关以Maurice Kendall命名。

# calculate kendall's correlationcoef, p = kendalltau(data1, data2)

# calculate the kendall's correlation between two variablesfrom numpy.random import randfrom numpy.random import seedfrom scipy.stats import kendalltau# seed random number generatorseed(1)# prepare datadata1 = rand(1000) * 20data2 = data1 + (rand(1000) * 10)# calculate kendall's correlationcoef, p = kendalltau(data1, data2)print('Kendall correlation coefficient: %.3f' % coef)# interpret the significancealpha = 0.05if p > alpha:print('Samples are uncorrelated (fail to reject H0) p=%.3f' % p)else:print('Samples are correlated (reject H0) p=%.3f' % p)

Kendall correlation coefficient: 0.709Samples are correlated (reject H0) p=0.000

Nonparametric Statistics for Non-Statisticians: A Step-by-Step Approach, 2009.

Applied Nonparametric Statistical Methods, Fourth Edition, 2007.

Rank Correlation Methods, 1990.

API

scipy.stats.spearmanr（）API :https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.spearmanr.html

scipy.stats.kendalltau（）API：https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kendalltau.html

Nonparametric statistics

Rank correlation

Spearman’s rank correlation coefficient

Kendall rank correlation coefficient

Goodman and Kruskal’s gamma on Wikipedia

Somers’ D

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180710B1K47U00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16

2021-06-16