<p>如果原始数据是数组<code>a</code>和<code>b</code>,则可以将<a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html">^{<cd3>}</a>与参数<code>equal_var=False</code>一起使用:</p>
<pre><code>t, p = ttest_ind(a, b, equal_var=False)
</code></pre>
<p>如果只有这两个数据集的摘要统计信息,则可以使用<a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind_from_stats.html">^{<cd5>}</a>(在0.16版中添加到scipy)或公式(<a href="http://en.wikipedia.org/wiki/Welch%27s_t_test">http://en.wikipedia.org/wiki/Welch%27s_t_test</a>)计算t值。</p>
<p>下面的脚本展示了这些可能性。</p>
<pre><code>from __future__ import print_function
import numpy as np
from scipy.stats import ttest_ind, ttest_ind_from_stats
from scipy.special import stdtr
np.random.seed(1)
# Create sample data.
a = np.random.randn(40)
b = 4*np.random.randn(50)
# Use scipy.stats.ttest_ind.
t, p = ttest_ind(a, b, equal_var=False)
print("ttest_ind: t = %g p = %g" % (t, p))
# Compute the descriptive statistics of a and b.
abar = a.mean()
avar = a.var(ddof=1)
na = a.size
adof = na - 1
bbar = b.mean()
bvar = b.var(ddof=1)
nb = b.size
bdof = nb - 1
# Use scipy.stats.ttest_ind_from_stats.
t2, p2 = ttest_ind_from_stats(abar, np.sqrt(avar), na,
bbar, np.sqrt(bvar), nb,
equal_var=False)
print("ttest_ind_from_stats: t = %g p = %g" % (t2, p2))
# Use the formulas directly.
tf = (abar - bbar) / np.sqrt(avar/na + bvar/nb)
dof = (avar/na + bvar/nb)**2 / (avar**2/(na**2*adof) + bvar**2/(nb**2*bdof))
pf = 2*stdtr(dof, -np.abs(tf))
print("formula: t = %g p = %g" % (tf, pf))
</code></pre>
<p>输出:</p>
<pre><code>ttest_ind: t = -1.5827 p = 0.118873
ttest_ind_from_stats: t = -1.5827 p = 0.118873
formula: t = -1.5827 p = 0.118873
</code></pre>