https://machinelearningmastery.com/statistical-hypothesis-tests-in-python-cheat-sheet/
应用机器学习中需要的17种统计假设检验的快速参考指南,并提供Python中的示例代码。
虽然有数百种统计假设检验可以使用,但只有一小部分子集可能需要在机器学习项目中使用。
在这篇文章中,你将看到一个机器学习项目中最流行的统计假说检验的手册,其中有使用Python API的例子。
-
每一个统计检验的表述方式都是一致的,包括。
- 检验的名称
- 检验的内容。
- 检验的关键假设。
- 测试结果如何解释。
- 使用测试的Python API。
注意,当涉及到数据的预期分布或样本大小等假设时,如果违反了某个假设,某个测试的结果很可能会优雅地退化,而不是立即变得不可用。
一般来说,数据样本需要具有领域的代表性,并且足够大,以暴露其分布进行分析。
在某些情况下,可以对数据进行修正以满足假设,例如通过去除离群值将近似正态分布修正为正态分布,或者当样本具有不同的方差时,在统计测试中使用对自由度的修正,这是两个例子。
最后,对于一个给定的关注点,如正态性,可能有多种检验方法。我们无法通过统计学得到问题的明确答案;相反,我们得到的是概率性的答案。因此,我们可以通过考虑问题的不同方式,对同一问题得出不同的答案。因此,我们对数据的一些问题可能需要进行多种不同的检验。
目录
-
-
本教程分为5个部分,它们是:。
- 正态性检验
- 夏皮罗-威尔克检验
- D'Agostino's K^2检验
- 安德森-达林检验
- 相关性检验
- 皮尔逊相关系数
- 斯皮尔曼秩相关
- Kendall's Rank Correlation
- 卡方检验
- 平稳性检验
- Augmented Dickey-Fuller
- Kwiatkowski-Phillips-Schmidt-Shin案
- 参数统计假设检验
- 学生T检验
- 配对学生T检验
- 方差分析检验(ANOVA)
- 重复计量方差分析检验
- 非参数统计假设检验
- Mann-Whitney U检验
- Wilcoxon Signed-Rank检验
- Kruskal-Wallis H检验
- 弗里德曼检验
1. 正态性检验 Normality Tests
本节列出了可以用来检查数据是否具有高斯分布的统计测试。
夏皮罗-威尔克测试 Shapiro-Wilk Test
-
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
解释
- H0:样本具有高斯分布。
- H1:样本不具有高斯分布。
Python代码
# Example of the Shapiro-Wilk Normality Test
from scipy.stats import shapiro
data = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
stat, p = shapiro(data)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably Gaussian')
else:
print('Probably not Gaussian')
更多资料
D'Agostino's K^2检验 D’Agostino’s K^2 Test
-
测试数据样本是否具有高斯分布。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
解释
- H0:样本具有高斯分布。
- H1:样本不具有高斯分布。
Python Code
# Example of the D'Agostino's K^2 Normality Test
from scipy.stats import normaltest
data = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
stat, p = normaltest(data)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably Gaussian')
else:
print('Probably not Gaussian')
More Information
安德森-达林检验 Anderson-Darling Test
-
测试数据样本是否具有高斯分布。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
解释
- H0:样本具有高斯分布。
- H1:样本不具有高斯分布。
Python Code
# Example of the Anderson-Darling Normality Test
from scipy.stats import anderson
data = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
result = anderson(data)
print('stat=%.3f' % (result.statistic))
for i in range(len(result.critical_values)):
sl, cv = result.significance_level[i], result.critical_values[i]
if result.statistic < cv:
print('Probably Gaussian at the %.1f%% level' % (sl))
else:
print('Probably not Gaussian at the %.1f%% level' % (sl))
More Information
2. 相关性检验 Correlation Tests
This section lists statistical tests that you can use to check if two samples are related.
皮尔逊相关系数 Pearson’s Correlation Coefficient
-
检验两个样本是否有线性关系。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
- 每个样本中的观测值都是正态分布。
- 每个样本中的观测值具有相同的方差。
解释
- H0:两个样本是独立的。
- H1:样本之间有 dependency。
Python Code
# Example of the Pearson's Correlation test
from scipy.stats import pearsonr
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [0.353, 3.517, 0.125, -7.545, -0.555, -1.536, 3.350, -1.578, -3.537, -1.579]
stat, p = pearsonr(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably independent')
else:
print('Probably dependent')
More Information
斯皮尔曼秩相关 Spearman’s Rank Correlation
Python Code
# Example of the Spearman's Rank Correlation Test
from scipy.stats import spearmanr
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [0.353, 3.517, 0.125, -7.545, -0.555, -1.536, 3.350, -1.578, -3.537, -1.579]
stat, p = spearmanr(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably independent')
else:
print('Probably dependent')
More Information
Kendall’s Rank Correlation
Python Code
# Example of the Kendall's Rank Correlation Test
from scipy.stats import kendalltau
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [0.353, 3.517, 0.125, -7.545, -0.555, -1.536, 3.350, -1.578, -3.537, -1.579]
stat, p = kendalltau(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably independent')
else:
print('Probably dependent')
More Information
卡方检验 Chi-Squared Test
-
检验两个分类变量是否相关或独立。
假设
- Observations used in the calculation of the contingency table are independent.
- 应急表的每个单元格中有25个以上的例子。
解释
- H0:两个样本是独立的。
- H1:样本之间有dependency。
Python Code
# Example of the Chi-Squared Test
from scipy.stats import chi2_contingency
table = [[10, 20, 30],[6, 9, 17]]
stat, p, dof, expected = chi2_contingency(table)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably independent')
else:
print('Probably dependent')
More Information
3. 平稳性检验 Stationary Tests
This section lists statistical tests that you can use to check if a time series is stationary or not.
Augmented Dickey-Fuller Unit Root Test
Python Code
# Example of the Augmented Dickey-Fuller unit root test
from statsmodels.tsa.stattools import adfuller
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
stat, p, lags, obs, crit, t = adfuller(data)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably not Stationary')
else:
print('Probably Stationary')
More Information
Kwiatkowski-Phillips-Schmidt-Shin
-
检验一个时间序列是否是趋势平稳的。
假设
解释
- H0:时间序列不是趋势稳定的。
- H1:时间序列是趋势稳定的。
Python Code
# Example of the Kwiatkowski-Phillips-Schmidt-Shin test
from statsmodels.tsa.stattools import kpss
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
stat, p, lags, crit = kpss(data)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably not Stationary')
else:
print('Probably Stationary')
More Information
4. 参数统计假设检验 Parametric Statistical Hypothesis Tests
本节列出了您可以用来比较数据样本的统计测试。
学生T检验 Student’s t-test
-
检验两个独立样本的均值是否有显著差异。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
- 每个样本中的观测值都是正态分布。
- 每个样本中的观测值具有相同的方差。
解释
Python Code
# Example of the Student's t-test
from scipy.stats import ttest_ind
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
stat, p = ttest_ind(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
配对学生T检验 Paired Student’s t-test
-
检验两个配对样本的均值是否有显著差异。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
- 每个样本中的观测值都是正态分布。
- 每个样本中的观测值具有相同的方差。
- 每个样本中的观测值都是成对的。
解释
Python Code
# Example of the Paired Student's t-test
from scipy.stats import ttest_rel
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
stat, p = ttest_rel(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
方差分析检验(ANOVA) Analysis of Variance Test (ANOVA)
-
检验两个或多个独立样本的均值是否有显著差异。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
- 每个样本中的观测值都是正态分布。
- 每个样本中的观测值具有相同的方差。
解释
- H0:样本的均值相等。
- H1:一个或多个样本的均值不相等。
Python Code
# Example of the Analysis of Variance Test
from scipy.stats import f_oneway
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
data3 = [-0.208, 0.696, 0.928, -1.148, -0.213, 0.229, 0.137, 0.269, -0.870, -1.204]
stat, p = f_oneway(data1, data2, data3)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
重复计量方差分析检验 Repeated Measures ANOVA Test
检验两个或多个配对样本的均值是否有显著差异。
假设
- 每个样本中的观测值都是独立和相同分布的(iid)。
- 每个样本中的观测值都是正态分布。
- 每个样本中的观测值具有相同的方差。
- 每个样本中的观测值都是成对的。
解释
- H0:样本的均值相等。
- H1: 一个或多个样本的均值不相等。
目前无法在python中实现
More Information
5. 非参数统计假设检验 Nonparametric Statistical Hypothesis Tests
Mann-Whitney U Test
-
检验两个独立样本的分布是否相等。
假设
- 每个样本中的观测值都是独立的、同分布的(iid)。
- 每个样本中的观测值可以进行排序。
解释
- H0:两个样本的分布相等。
- H1:两个样本的分布不相等。
Python Code
# Example of the Mann-Whitney U Test
from scipy.stats import mannwhitneyu
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
stat, p = mannwhitneyu(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
Wilcoxon Signed-Rank Test
-
检验两个成对样本的分布是否相等。
假设
- 每个样本中的观测值都是独立的、同分布的(iid)。
- 每个样本中的观测值可以进行排序。
- 每个样本中的观测值都是成对的。
解释
- H0:两个样本的分布相等。
- H1:两个样本的分布不相等。
Python Code
# Example of the Wilcoxon Signed-Rank Test
from scipy.stats import wilcoxon
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
stat, p = wilcoxon(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
Kruskal-Wallis H Test
-
检验两个或多个独立样本的分布是否相等。
假设
- 每个样本中的观测值都是独立的、同分布的(iid)。
- 每个样本中的观测值可以进行排序。
解释
- H0:所有样本的分布都相等。
- H1:一个或多个样本的分布不相等。
Python Code
# Example of the Kruskal-Wallis H Test
from scipy.stats import kruskal
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
stat, p = kruskal(data1, data2)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
Friedman Test
-
检验两个或多个成对样本的分布是否相等。
假设
- 每个样本中的观测值都是独立的、同分布的(iid)。
- 每个样本中的观测值可以进行排序。
- 每个样本中的观测值都是成对的。
解释
- H0:所有样本的分布都相等。
- H1:一个或多个样本的分布不相等。
Python Code
# Example of the Friedman Test
from scipy.stats import friedmanchisquare
data1 = [0.873, 2.817, 0.121, -0.945, -0.055, -1.436, 0.360, -1.478, -1.637, -1.869]
data2 = [1.142, -0.432, -0.938, -0.729, -0.846, -0.157, 0.500, 1.183, -1.075, -0.169]
data3 = [-0.208, 0.696, 0.928, -1.148, -0.213, 0.229, 0.137, 0.269, -0.870, -1.204]
stat, p = friedmanchisquare(data1, data2, data3)
print('stat=%.3f, p=%.3f' % (stat, p))
if p > 0.05:
print('Probably the same distribution')
else:
print('Probably different distributions')
More Information
更多资料
如果你想深入了解这个话题,本节提供更多资料。
总结
在本教程中,你发现了机器学习项目中可能需要用到的关键统计假设检验。
具体来说,您学到了
- 在不同情况下使用的测试类型,如正态性检查、变量之间的关系和样本之间的差异。
- 每个测试的关键假设以及如何解释测试结果。
- 如何使用Python API实现测试。