Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多数据列比较的循环T检验_Python_Pandas_Python Unittest - Fatal编程技术网

Python 多数据列比较的循环T检验

Python 多数据列比较的循环T检验,python,pandas,python-unittest,Python,Pandas,Python Unittest,我有一只熊猫,有11列数据。我想通过测试将每一列与其他每一列进行比较(见下文)。如何创建一个自动比较所有列的循环,而无需手动编写每个列对组合的代码 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

我有一只熊猫,有11列数据。我想通过测试将每一列与其他每一列进行比较(见下文)。如何创建一个自动比较所有列的循环,而无需手动编写每个列对组合的代码

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')

是否可以用矩阵或图形显示结果?提前谢谢你

让我们使用嵌套的dict理解来计算每个可能的列组合的
t-test
,然后从嵌套的dict初始化一个新的数据帧,以创建格式良好的矩阵表示:

dct = {x: {y: 's={:.2f}, p={:.2f}'.format(
          *ttest_ind(df[x], df[y])) for y in df} for x in df}
mat = pd.DataFrame(dct)


print(mat)
                 data1           data2
data1   s=0.00, p=1.00  s=0.33, p=0.75
data2  s=-0.33, p=0.75  s=0.00, p=1.00

如果需要仅包含
p值的矩阵

dct = {x: {y: ttest_ind(df[x], df[y]).pvalue for y in df} for x in df}
mat = pd.DataFrame(dct)

print(mat)
         data1    data2
data1  1.00000  0.74847
data2  0.74847  1.00000
要计算所有
p值的平均值,请使用:

mat.to_numpy().mean()
0.8742349436807844

注意:
df
是包含列
data1
data2

的数据框如果我答对了你的问题,你可以这样做: (数据只是一个示例数据帧。 照片中的结果还在继续,我只是没有全部展示。)


在本例中,对于稀疏数据,直方图将显示某些内容,但不是所有内容

from scipy.stats import ttest_ind
import matplotlib.pyplot as plt
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')
plt.hist(data1, bins=5, color='c', edgecolor='k', alpha=0.5)
plt.hist(data2, bins=5, color='g', edgecolor='k', alpha=0.5)
你会得到这样的情节


要真正了解学生的T-测试,请看一下

谢谢,这很有帮助!有没有办法从矩阵中计算所有p值的平均值?是的,我们可以,但在这种情况下,我们必须修改dict理解。您是否也对结果中的
s
值感兴趣?如果没有,我们可以从矩阵表示中删除
s
值,那么计算平均值就容易多了。@CSBossmann请检查编辑。@CSBossmann check
np.nanmean(mat.to_numpy())
from scipy.stats import ttest_ind
import matplotlib.pyplot as plt
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')
plt.hist(data1, bins=5, color='c', edgecolor='k', alpha=0.5)
plt.hist(data2, bins=5, color='g', edgecolor='k', alpha=0.5)