Wilcoxon python代码
时间: 2025-07-05 18:00:36 浏览: 5
### Python 实现 Wilcoxon 秩和检验
为了执行 Wilcoxon 秩和检验(也称为 Mann-Whitney U 检验),可以利用 `scipy` 库中的函数来完成这一统计测试。下面展示了如何在 Python 中实现该方法:
```python
import numpy as np
from scipy import stats
# 定义两个独立样本的数据集
sample1 = [28, 31, 36, 35, 32, 33, 21, 12, 12, 23, 19, 13, 20, 17, 14, 19]
sample2 = [12, 18, 19, 14, 20, 19, 12, 11, 8, 9, 10, 15, 16, 17, 10, 16]
# 执行 Wilcoxon 秩和检验
u_statistic, p_value = stats.mannwhitneyu(sample1, sample2)
print(f"Mann-Whitney U 统计量: {u_statistic}")
print(f"P 值: {p_value:.10f}")
if p_value < 0.05:
print("两组数据存在显著差异")
else:
print("两组数据无显著差异")
```
上述代码片段定义了两个样本列表并调用了 `mannwhitneyu()` 函数来进行非参数假设检验,用于比较这两个不相交群体之间的分布情况[^2]。
对于配对样本,则应采用 Wilcoxon 符号秩检验,如下所示:
```python
# 配对样本的情况
paired_sample1 = [28, 31, 36, 35, 32, 33, 21, 12, 12, 23, 19, 13, 20, 17, 14, 19]
paired_sample2 = [25, 30, 34, 33, 30, 31, 20, 10, 11, 21, 17, 12, 18, 16, 13, 18]
w_statistic, paired_p_value = stats.wilcoxon(paired_sample1, paired_sample2)
print(f"Wilcoxon W 统计量: {w_statistic}")
print(f"P 值: {paired_p_value:.10f}")
if paired_p_value < 0.05:
print("配对样本之间存在显著差异")
else:
print("配对样本之间无显著差异")
```
这段代码适用于处理来自同一总体但在不同条件下测量得到的一系列观测值,即所谓的“配对”或“依赖”样本。
阅读全文
相关推荐


















