gpt4 book ai didi

python - 将 PyMC3 traceplot 子图保存到图像文件

转载 作者:行者123 更新时间:2023-11-28 21:44:13 27 4
gpt4 key购买 nike

我尝试非常简单地将 PyMC3 traceplot 函数(参见 here)生成的子图绘制到一个文件中。

该函数生成子图的 numpy.ndarray (2d)。

我需要将这些子图移动或复制到 matplotlib.figure 中以保存图像文件。我能找到的所有内容都显示了如何首先生成图形的子图,然后构建它们。

作为一个最小示例,我从 Here 中提取了示例 PyMC3 代码, 并向其中添加了几行以尝试处理子图。

from pymc3 import *
import theano.tensor as tt
from theano import as_op
from numpy import arange, array, empty

### Added these three lines relative to source #######################
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

__all__ = ['disasters_data', 'switchpoint', 'early_mean', 'late_mean', 'rate', 'disasters']

# Time series of recorded coal mining disasters in the UK from 1851 to 1962
disasters_data = array([4, 5, 4, 0, 1, 4, 3, 4, 0, 6, 3, 3, 4, 0, 2, 6,
3, 3, 5, 4, 5, 3, 1, 4, 4, 1, 5, 5, 3, 4, 2, 5,
2, 2, 3, 4, 2, 1, 3, 2, 2, 1, 1, 1, 1, 3, 0, 0,
1, 0, 1, 1, 0, 0, 3, 1, 0, 3, 2, 2, 0, 1, 1, 1,
0, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0, 0, 1, 1, 0, 2,
3, 3, 1, 1, 2, 1, 1, 1, 1, 2, 4, 2, 0, 0, 1, 4,
0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1])
years = len(disasters_data)

@as_op(itypes=[tt.lscalar, tt.dscalar, tt.dscalar], otypes=[tt.dvector])
def rateFunc(switchpoint, early_mean, late_mean):
out = empty(years)
out[:switchpoint] = early_mean
out[switchpoint:] = late_mean
return out


with Model() as model:

# Prior for distribution of switchpoint location
switchpoint = DiscreteUniform('switchpoint', lower=0, upper=years)
# Priors for pre- and post-switch mean number of disasters
early_mean = Exponential('early_mean', lam=1.)
late_mean = Exponential('late_mean', lam=1.)

# Allocate appropriate Poisson rates to years before and after current switchpoint location
rate = rateFunc(switchpoint, early_mean, late_mean)

# Data likelihood
disasters = Poisson('disasters', rate, observed=disasters_data)

# Initial values for stochastic nodes
start = {'early_mean': 2., 'late_mean': 3.}

# Use slice sampler for means
step1 = Slice([early_mean, late_mean])
# Use Metropolis for switchpoint, since it accomodates discrete variables
step2 = Metropolis([switchpoint])

# njobs>1 works only with most recent (mid August 2014) Thenao version:
# https://github.com/Theano/Theano/pull/2021
tr = sample(1000, tune=500, start=start, step=[step1, step2], njobs=1)

### gnashing of teeth starts here ################################
fig, axarr = plt.subplots(3,2)

# This gives a KeyError
# axarr = traceplot(tr, axarr)

# This finishes without error
trarr = traceplot(tr)

# doesn't work
# axarr[0, 0] = trarr[0, 0]

fig.savefig("disaster.png")

我尝试了 subplot() 和 add_subplot() 行的一些变体,但无济于事——所有错误都指向一个事实,即必须首先为图形创建空子图,而不是分配给预先存在的子图.

一个不同的例子(参见 here ,大约 80% 的下降,从

开始
### Mysterious code to be explained in Chapter 3.

) 完全避免了实用程序并手动构建子图,所以也许对此没有好的答案? pymc3.traceplot 输出确实是无法使用的孤立的子图的 ndarray 吗?

最佳答案

我遇到了同样的问题。我正在使用 pymc3 3.5 和 matplotlib 2.1.2。

我意识到可以通过以下方式导出跟踪图:

trarr = traceplot(tr)

fig = plt.gcf() # to get the current figure...
fig.savefig("disaster.png") # and save it directly

关于python - 将 PyMC3 traceplot 子图保存到图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40993620/

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