scipy.stats.

spearmanr#

scipy.stats.spearmanr(a, b=None, axis=0, nan_policy='propagate', alternative='two-sided')[源代码]#

计算 Spearman 相关系数及其 p 值。

Spearman 秩次相关系数是非参数方法,用于衡量两个数据集之间关系的单调性。与其他相关系数一样,它的值在 -1 和 +1 之间变化,0 表示无相关性。-1 或 +1 的相关性表示精确的单调关系。正相关表示当 x 增加时,y 也增加。负相关表示当 x 增加时,y 减少。

p 值大致表示在无相关性的系统中,生成 Spearman 相关性至少与计算出的相关性一样极端的(或更极端的)数据集的概率。尽管 p 值的计算并未对样本的潜在分布做出严格的假设,但它只适用于非常大的样本(>500 个观测值)。对于较小的样本量,请考虑置换检验(请参阅下面的示例部分)。

参数:
a, b一维或二维类数组,b 可选

一个或两个一维或二维类数组,包含多个变量和观测值。当它们是一维时,每个数组代表一个变量的观测值向量。有关二维情况的行为,请参阅下面的 axis

axis整数或 None,可选

如果 axis=0(默认),则每列代表一个变量,行包含观测值。如果 axis=1,则关系转置:每行代表一个变量,而列包含观测值。如果 axis=None,则两个数组都将被展平。

nan_policy{‘propagate’, ‘raise’, ‘omit’}, 可选

定义了如何处理输入包含 NaN 的情况。以下选项可用(默认为 'propagate')

  • ‘propagate’:返回 nan

  • ‘raise’:引发错误

  • ‘omit’:执行计算时忽略 nan 值

alternative{‘two-sided’, ‘less’, ‘greater’}, 可选

定义备择假设。默认为“双侧”。以下选项可用:

  • ‘two-sided’:相关性不为零

  • ‘less’:相关性为负(小于零)

  • ‘greater’:相关性为正(大于零)

版本 1.7.0 中新增。

返回:
resSignificanceResult

一个包含属性的对象

statistic浮点数或 ndarray(二维方阵)

Spearman 相关矩阵或相关系数(如果作为参数只给出 2 个变量)。相关矩阵是方阵,其长度等于 ab 中变量(列或行)的总数。

pvaluefloat

假设两个样本没有序数相关性的假设检验的 p 值。有关备择假设,请参阅上面的 alternativepvalue 的形状与 statistic 相同。

引发:
ValueError

如果 axis 不是 0、1 或 None,或者如果 a 的维度大于 2,或者如果 b 为 None 且 a 的维度小于 2。

警告:
ConstantInputWarning

当输入是常量数组时引发。在这种情况下,相关系数未定义,因此返回 np.nan

另请参阅

Spearman 相关系数

扩展示例

附注

数组 API 标准支持

spearmanr 除了 NumPy 之外,还对兼容 Python Array API Standard 的后端提供了实验性支持。请考虑通过设置环境变量 SCIPY_ARRAY_API=1 并提供 CuPy、PyTorch、JAX 或 Dask 数组作为数组参数来测试这些功能。支持以下后端与设备(或其他功能)的组合。

CPU

GPU

NumPy

不适用

CuPy

不适用

PyTorch

JAX

Dask

不适用

有关更多信息,请参阅 对数组 API 标准的支持

参考文献

[1]

Zwillinger, D. and Kokoska, S. (2000). CRC Standard Probability and Statistics Tables and Formulae. Chapman & Hall: New York. 2000. Section 14.7

[2]

Kendall, M. G. and Stuart, A. (1973). The Advanced Theory of Statistics, Volume 2: Inference and Relationship. Griffin. 1973. Section 31.18

示例

>>> import numpy as np
>>> from scipy import stats
>>> res = stats.spearmanr([1, 2, 3, 4, 5], [5, 6, 7, 8, 7])
>>> res.statistic
0.8207826816681233
>>> res.pvalue
0.08858700531354381
>>> rng = np.random.default_rng()
>>> x2n = rng.standard_normal((100, 2))
>>> y2n = rng.standard_normal((100, 2))
>>> res = stats.spearmanr(x2n)
>>> res.statistic, res.pvalue
(-0.07960396039603959, 0.4311168705769747)
>>> res = stats.spearmanr(x2n[:, 0], x2n[:, 1])
>>> res.statistic, res.pvalue
(-0.07960396039603959, 0.4311168705769747)
>>> res = stats.spearmanr(x2n, y2n)
>>> res.statistic
array([[ 1. , -0.07960396, -0.08314431, 0.09662166],
       [-0.07960396, 1. , -0.14448245, 0.16738074],
       [-0.08314431, -0.14448245, 1. , 0.03234323],
       [ 0.09662166, 0.16738074, 0.03234323, 1. ]])
>>> res.pvalue
array([[0. , 0.43111687, 0.41084066, 0.33891628],
       [0.43111687, 0. , 0.15151618, 0.09600687],
       [0.41084066, 0.15151618, 0. , 0.74938561],
       [0.33891628, 0.09600687, 0.74938561, 0. ]])
>>> res = stats.spearmanr(x2n.T, y2n.T, axis=1)
>>> res.statistic
array([[ 1. , -0.07960396, -0.08314431, 0.09662166],
       [-0.07960396, 1. , -0.14448245, 0.16738074],
       [-0.08314431, -0.14448245, 1. , 0.03234323],
       [ 0.09662166, 0.16738074, 0.03234323, 1. ]])
>>> res = stats.spearmanr(x2n, y2n, axis=None)
>>> res.statistic, res.pvalue
(0.044981624540613524, 0.5270803651336189)
>>> res = stats.spearmanr(x2n.ravel(), y2n.ravel())
>>> res.statistic, res.pvalue
(0.044981624540613524, 0.5270803651336189)
>>> rng = np.random.default_rng()
>>> xint = rng.integers(10, size=(100, 2))
>>> res = stats.spearmanr(xint)
>>> res.statistic, res.pvalue
(0.09800224850707953, 0.3320271757932076)

对于小样本,请考虑执行置换检验而不是依赖渐近 p 值。请注意,为了计算统计量的零分布(对于样本 xy 之间的所有可能的配对),只需要对两个输入中的一个进行置换。

>>> x = [1.76405235, 0.40015721, 0.97873798,
... 2.2408932, 1.86755799, -0.97727788]
>>> y = [2.71414076, 0.2488, 0.87551913,
... 2.6514917, 2.01160156, 0.47699563]
>>> def statistic(x): # permute only `x`
...     return stats.spearmanr(x, y).statistic
>>> res_exact = stats.permutation_test((x,), statistic,
...     permutation_type='pairings')
>>> res_asymptotic = stats.spearmanr(x, y)
>>> res_exact.pvalue, res_asymptotic.pvalue # asymptotic pvalue is too low
(0.10277777777777777, 0.07239650145772594)

有关更详细的示例,请参阅 Spearman 相关系数