【问题标题】:How do I fix the 'TypeError: hasattr(): attribute name must be string' error?如何修复“TypeError: hasattr(): attribute name must be string”错误?
【发布时间】:2015-07-14 06:02:56
【问题描述】:

我有以下代码:

import pymc as pm
from matplotlib import pyplot as plt
from pymc.Matplot import plot as mcplot
import numpy as np
from matplotlib import rc

res = [18.752, 12.450, 11.832]

v = pm.Uniform('v', 0, 20)

errors = pm.Uniform('errors', 0, 100, size = 3)

taus = 1/(errors ** 2)

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = True)

model=pm.Model([mydist, errors, taus, v, res])
mcmc=pm.MCMC(model) # This is line 19 where the TypeError originates
mcmc.sample(20000,10000)

mcplot(mcmc.trace('mydist'))

由于某种原因它不起作用,我收到“TypeError: hasattr(): attribute name must be string”错误,并带有以下跟踪:

 Traceback (most recent call last):

  File "<ipython-input-49-759ebaf4321c>", line 1, in <module>
runfile('C:/Users/Paul/.spyder2-py3/temp.py', wdir='C:/Users/Paul/.spyder2-py3')

  File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "C:/Users/Paul/.spyder2-py3/temp.py", line 19, in <module>
mcmc=pm.MCMC(model)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\MCMC.py", line 82, in __init__
**kwds)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 197, in __init__
Model.__init__(self, input, name, verbose)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Model.py", line 99, in __init__
ObjectContainer.__init__(self, input)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 606, in __init__
conservative_update(self, input_to_file)

  File "C:\Users\Paul\Miniconda3\lib\site-packages\pymc\Container.py", line 549, in conservative_update
if not hasattr(obj, k):

TypeError: hasattr(): attribute name must be string

如何让它工作并输出“mydist”?

编辑:我一开始不小心发错了踪迹。

Edit2:一定是因为 res 没有名字,因为它是一个数组,但是我不知道如何给它命名,所以它会起作用。

【问题讨论】:

  • 回溯 sn-p 似乎不包含该错误。
  • @Daniel,一不小心发错了,不好意思,我改了

标签: python numpy pymc


【解决方案1】:

我必须承认我不熟悉pymc,但是将其更改为以下内容至少可以使应用程序运行:

mydist = pm.Normal('mydist', mu = v, tau = taus, value = res, observed = False)

mcmc=pm.MCMC([mydist, errors, taus, v, res])

这似乎是因为您将所有内容包装在一个 Model 中,它是 ObjectContainer 的扩展,但由于您传递了一个列表,MCMC file_itemsContainer.py 中尝试使用 replace 将列表中的索引 4 分配给某物,但由于 ModelObjectContainer,它在 __dict__ 中分配了键 4导致你得到奇怪的TypeError。删除包装 Model 导致 MCMC 正确使用 ListContainer

现在,line 543 上的 Model.py 中可能存在一个错误,其中可观察的随机变量未存储在数据库中 - 表达式为 for object in self.stochastics | self.deterministics: 但我怀疑它应该包括 self.observable_stochastics太 - 所以我需要将 observable 更改为 False 否则最后一行会抛出 KeyError

我对 pymc 不够熟悉,无法确定它实际上是错误还是期望的行为,所以我留给你提交一个 issue 关于它。

【讨论】:

  • 对不起,但这是错误的,因为observed = False,表示你没有考虑res,意思是它不看值(来自表达式的值value = res)跨度>
  • 对不起,我能做的就这么多了。如果mydistobserved 设置为True,则在尝试提取其踪迹时会得到KeyError
【解决方案2】:

您只需将res 定义为一个numpy 数组:

res = np.array([18.752, 12.450, 11.832])

那么你会在这里得到一个错误mcmc.trace('mydist')因为mydist是观察到的数据,因此没有被采样。您可能想要绘制其他变量...

【讨论】:

    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2012-05-15
    • 2016-10-10
    • 1970-01-01
    • 2012-03-03
    • 2013-09-24
    相关资源
    最近更新 更多