gpt4 book ai didi

python 统计模型 : tukey HSD plot not working

转载 作者:太空狗 更新时间:2023-10-30 01:33:49 25 4
gpt4 key购买 nike

试图找出如何使用 statsmodel 计算 Tukey 的 HSD。我可以使它工作并且结果看起来很棒但是有一个我看不到的均值差异图。一定是我在做的傻事。

它是对象 TukeyHSDResults 中的方法 plot_simultaneous(参见 doc)。

这是我用来尝试的代码:

import pandas as pd
import numpy as np
from sklearn.cross_validation import train_test_split
from scipy import stats
from statsmodels.stats.multicomp import (pairwise_tukeyhsd,
MultiComparison)

red_wine = pd.DataFrame.from_csv(".../winequality-red.csv",
sep=';', header=0, index_col=False)
white_wine = pd.DataFrame.from_csv(".../winequality-white.csv",
sep=';', header=0, index_col=False)
white1, white2 = train_test_split(white_wine['quality'], test_size=0.5, random_state=1812)

# compute anova
f, p = stats.f_oneway(red_wine['quality'], white1, white2)
print("F value: " + str(f))
print("p value: " + str(p))

# tukey HSD
red = pd.DataFrame(red_wine['quality'], columns=['quality'])
red['wine'] = map(lambda x: 'red', red['quality'])
w1 = pd.DataFrame(white1, columns=['quality'])
w1['wine'] = map(lambda x: 'white1', w1['quality'])
w2 = pd.DataFrame(white2, columns=['quality'])
w2['wine'] = map(lambda x: 'white2', w2['quality'])
total = pd.concat([red, w1, w2], axis=0)

res = pairwise_tukeyhsd(endog=total['quality'], groups=total['wine'], alpha=0.01)
print(res.summary())
res.plot_simultaneous()
mod = MultiComparison(total['quality'], total['wine'])
results = mod.tukeyhsd(0.01)
## plot does not work!
results.plot_simultaneous()

csv 文件是公共(public)数据集,可以从here 获得。 .代码的一些解释:我随机分开白 Wine ,这样我就知道这 2 个样本来自同一群体,而红 Wine 的第三个样本来自不同的群体。只需一个简单的设置即可试用该库。

我已经尝试过 pydevipython notebook 以防万一。在 pydev 中,我得到了沉默,没有图表,在 notebook 中,我得到了这个简洁的输出:

In [3]: results.plot_simultaneous('red')
Out[3]: <matplotlib.figure.Figure at 0x1105b2b90>

在 python/pandas 方面经验不多,但如果我坚持足够长的时间,我通常会看到情节。

我也试过文档中的示例(上面的链接):

In [3]:

cylinders = np.array([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 6, 6, 6, 4, 4,
4, 4, 4, 4, 6, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 6, 6, 6, 6, 4, 4, 4, 4, 6, 6,
6, 6, 4, 4, 4, 4, 4, 8, 4, 6, 6, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4])
cyl_labels = np.array(['USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'France',
'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'Japan', 'USA', 'USA', 'USA', 'Japan',
'Germany', 'France', 'Germany', 'Sweden', 'Germany', 'USA', 'USA', 'USA', 'USA', 'USA', 'Germany',
'USA', 'USA', 'France', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'Germany',
'Japan', 'USA', 'USA', 'USA', 'USA', 'Germany', 'Japan', 'Japan', 'USA', 'Sweden', 'USA', 'France',
'Japan', 'Germany', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA', 'USA',
'Germany', 'Japan', 'Japan', 'USA', 'USA', 'Japan', 'Japan', 'Japan', 'Japan', 'Japan', 'Japan', 'USA',
'USA', 'USA', 'USA', 'Japan', 'USA', 'USA', 'USA', 'Germany', 'USA', 'USA', 'USA'])
from statsmodels.stats.multicomp import MultiComparison
cardata = MultiComparison(cylinders, cyl_labels)
results = cardata.tukeyhsd()
results.plot_simultaneous()
Out[3]:
<matplotlib.figure.Figure at 0x10b5bb610>

相同的结果。

最佳答案

您可能需要告诉 matplotlib 使用哪个后端。

在 ipython 笔记本中尝试在导入 matplotlib 之前添加一行 %matplotlib inline

在您在笔记本之外运行的模块中尝试添加:

import matplotlib
matplotlib.use('qtagg')

参见 here有关后端的更多信息。

关于 python 统计模型 : tukey HSD plot not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29368634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com