确定两个独立数据集之间统计显著性的方法

Ways to determine the statistical significance between two independent datasets

提问人:Marian 提问时间:10/3/2023 最后编辑:Sandipan DeyMarian 更新时间:10/5/2023 访问量:111

问:

假设 和 是两个数据集。每个数据集可能都具有特征。如何对这些独立数据集进行假设检验以比较统计显著性?AB100

我尝试用 Python 编写代码。我已经预处理了这两个数据集,并且考虑到列已归一化,我尝试使用 Student 的测试。数据集是具有连续值的表格数据,并对分类特征进行了一次热编码。我尝试对两个数据集中的数字列进行执行。但我似乎无法弄清楚如何在整个数据集上执行。我使用了图书馆。tt-testscipy.stats

Python 统计 分析 假设检验 Kolmogorov-Smirnov

评论

0赞 lastchance 10/3/2023
那么,你的零假设是什么?
0赞 Marian 10/3/2023
我的零假设是,这两个样本是从同一分布中抽取的。我是统计学的新手,还在弄清楚!
1赞 Community 10/3/2023
请提供足够的代码,以便其他人可以更好地理解或重现问题。
0赞 Suraj Shourie 10/3/2023
您将如何将一个发行版的 100 个特征与另一个发行版进行比较。一列一列?

答:

0赞 Suraj Shourie 10/3/2023 #1

这可能是 https://stats.stackexchange.com/ 的问题。

但我会尝试使用 python 代码给出一种方法。这使用学生的 T 检验或 Welch T 检验,这是一种更严格的检验,因为它不假设两个分布的方差相似。

请注意,这将检查两个分布的均值在统计上是否相似。

python 中虚拟数据的示例代码:

import numpy as np
from scipy import stats
arr1 = np.random.normal(loc=1,size=(10000,2))
arr2 = np.random.normal(loc=1,size=(10000,2))
print(stats.ttest_ind(arr1, arr2, equal_var=True, axis=0))

输出:

TtestResult(statistic=array([-2.13993016,  0.87158797]), pvalue=array([0.03237248, 0.38344366]), df=array([19998., 19998.]))

上面的代码逐列比较相等均值,并给出每个值的 p 值(和 t 统计量)。

0赞 Arun Singh Babal 10/4/2023 #2

您可以对数据集的所有特征应用 t 检验,如下所示:

p_values = []
for i in range(df1.shape[1]):
    _, p_value = stats.ttest_ind(df1[:, i], df2[:, i])
    p_values.append(p_value)
0赞 Sandipan Dey 10/4/2023 #3

该检验是一种非参数统计检验,可用于确定两个样本是否来自同一分布。Kolmogorov-Smirnov

您可以采取的一种方法是针对数据集中的每个特征(列),并执行测试以检查它们是否来自同一分布(使用函数)。ABKSscipy.stats.ks_2samp()

下面的代码显示了一个示例,其中它使用了几个 -column 数据集,即 和 。数据集的第一个特征(列)来自相同的(标准正态)分布,但第二个特征来自不同的(正态)分布(具有不同的参数)。2ABAB

import numpy as np
from scipy.stats import ks_2samp

n = 100 # number of samples

A = np.hstack((np.random.normal(loc=0, scale=1, size=n).reshape(-1,1), \
               np.random.normal(loc=0, scale=1, size=n).reshape(-1,1)))

B = np.hstack((np.random.normal(loc=0, scale=1, size=n).reshape(-1,1), \
               np.random.normal(loc=20, scale=5, size=n).reshape(-1,1)))

如果绘制数据集要素的直方图,将获得如下图:

enter image description here

显然,第二个特征很可能是从不同的发行版中选择的。让我们通过测试进行验证。KS

for i in range(A.shape[1]):
    print(f'Kolmogorov-Smirnov test for feature column {i}')
    statistic, pvalue = ks_2samp(A[:,i], B[:,i])
    print(f"Test statistic: {statistic}")
    print(f"P-value: {pvalue}")

# Kolmogorov-Smirnov test for feature column 0
# Test statistic: 0.13
# P-value: 0.36818778606286096  # can't reject H0

# Kolmogorov-Smirnov test for feature column 1
# Test statistic: 1.0
# P-value: 2.2087606931995054e-59 # reject H0

从上面可以看出,使用测试,KS

  • 我们不能拒绝原假设(在显著性水平上),即数据集的第一个特征来自同一分布,因为 是高 (),5%ABp-value0.368 > 0.05
  • 我们可以正确地否定原假设,即数据集的第二个特征来自同一分布,因为 几乎是 .ABp-value0

您可以对 -column 数据集使用相同的方法,方法是逐向比较它们。100