# python做单因素方差分析

• 组间平方和=每一组的均值减去样本均值
• 组内平方和=个体减去每组平方和

python实现：

```from scipy import stats
from statsmodels.formula.api import ols
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import pairwise_tukeyhsd
import warnings
warnings.filterwarnings("ignore")

import itertools

df2=pd.DataFrame()
df2['group']=list(itertools.repeat(-1.,9))+ list(itertools.repeat(0.,9))+list(itertools.repeat(1.,9))

df2['noise_A']=0.0
for i in data['A'].unique():
df2.loc[df2['group']==i,'noise_A']=data.loc[data['A']==i,['1','2','3']].values.flatten()

df2['noise_B']=0.0
for i in data['B'].unique():
df2.loc[df2['group']==i,'noise_B']=data.loc[data['B']==i,['1','2','3']].values.flatten()

df2['noise_C']=0.0
for i in data['C'].unique():
df2.loc[df2['group']==i,'noise_C']=data.loc[data['C']==i,['1','2','3']].values.flatten()

df2```

```# for A
anova_reA= anova_lm(ols('noise_A~C(group)',data=df2[['group','noise_A']]).fit())
print(anova_reA)
#B
anova_reB= anova_lm(ols('noise_B~C(group)',data=df2[['group','noise_B']]).fit())
print(anova_reB)
#C
anova_reC= anova_lm(ols('noise_C~C(group)',data=df2[['group','noise_C']]).fit())
print(anova_reC)```

从结果可以看出，A、B两样本，在每个组间均值显著无差异，C样本的组间均值是有差异的。

原文作者：weixin_30715523
原文地址: https://blog.csdn.net/weixin_30715523/article/details/96558398
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。