import numpy as np
data = np.random.normal(0, 1, size=30)
from scipy.stats import t
def t_test(data, mu=0):
mean_val = np.mean(data)
se = np.std(data) / np.sqrt(len(data))
df = len(data) - 1 # 自由度
p_val = (mean_val - mu) / se
t_val = t.cdf(p_val, df)
return t_val
mu = 0
t_val = t_test(data, mu)
print(t_val)
t_val > 0.05
说明我们不能拒绝零假设,所以该样本均值为0