假设检验步骤
建立原假设H0 和 备假设 H1
确定检验统计量
确定拒绝区域
用样本计算z分数 or t分数 以及 p值
接受原假设或者拒绝原假设
利用sklearn里的iris数据做假设验证
# 导入数据
iris = datasets.load_iris()
X = iris.data
y = iris.target
iris.target_names
>> ['setosa', 'versicolor', 'virginica']
iris.feature_names
>> ['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
setosa = X[:49] # 0:49 # set花的数据
versicolor = X[49:99] # 49:99 # ver花的数据
petal_len = X[:, 2] # 花瓣长度数据
Ho: 花瓣平均长度为4.0
H1: 花瓣平均长度不为4.0
import scipy.stats
# ttest_1samp: Calculate the T-test for the mean of ONE group of scores.
t, pval = scipy.stats.ttest_1samp(petal_len, popmean=4.0)
print(t, pval)
if pval>0.05:
print("p值大于0.05, 接受H0原假设: 花瓣平均长度为4.0")
else:
print("p值小于0.05, 拒绝H0原假设: 花瓣平均长度不为4.0")
-1.67896989467615 0.09525380636130043
p值大于0.05, 接受H0原假设: 花瓣平均长度为4.0
H0: setosa 和 versicolor两种花的花瓣长度一样长
H1: setosa 和 versicolor两种花的花瓣长度不一样
# ttest_ind: Calculate the T-test for the means of two independent samples of scores.
t, pval = scipy.stats.ttest_ind(setosa[:, 2], versicolor[:, 2])
print(t,pval)
if pval>0.05:
print("p值大于0.05, 接受H0原假设: 两种花的花瓣长度一样")
else:
print('p 值小于0.05, 拒绝H0原假设: 两种花瓣长度明显不同')
-29.82392997220894 1.1881282298705667e-50
p 值小于0.05, 拒绝H0原假设: 两种花瓣长度明显不同
如果觉得《python多元假设检验_Python中假设检验的实现 python》对你有帮助,请点赞、收藏,并留下你的观点哦!