我编写了一些直接输入解释器时可以工作的代码,但在调用时会失败。
这是一些代码(这里有很多代码可以重现):
import scikits.statsmodels.api as sm
import pandas as pd
data = sm.datasets.longley.load()
df = pd.DataFrame(data.exog, columns=data.exog_name)
y = data.endog
df['intercept'] = 1.
olsresult = sm.OLS(y, df).fit()
olsresult2 = sm.OLS(y, df[['GNP', 'UNEMP', 'ARMED']]).fit()
olsresult3 = sm.OLS(y, df[['GNP', 'POP', 'ARMED', 'YEAR']]).fit()
models = [olsresult, olsresult2, olsresult3]
class generateTable(object):
def __init__(self, output, models, center='True', parens='se', var_names=None):
self.output = output
self.models = models
self.center = center
self.parens = parens
self.var_names = var_names
def createModel(self):
results = []
for model in self.models:
params = dict(model.params)
bse = dict(model.bse)
pvals = dict(model.pvalues)
results.append(dict((k, [params[k], bse[k], pvals[k]]) for k in sorted(params.iterkeys())))
tempModel = {}
for key in results[0]:
tempModel[key] = [results[0][key]]
for model in results[1:len(results)]:
for key in model:
if key not in tempModel:
tempModel[key] = [['', '', '']]
for i in range(1,len(results)):
diff = set(tempModel) - set(results[i])
for key in results[i]:
tempModel[key].append(results[i][key])
for key in diff:
tempModel[key].append(['','',''])
if self.var_names == None:
self.inputModel = tempModel
elif type(self.var_names) == list:
replace = self.var_names
newResults = []
resultsList = sorted(tempModel.iteritems())
for item in resultsList:
newVar = list(item)
newResults.append(newVar)
for i in range(len(newResults)):
newResults[i][0] = replace[i]
self.inputModel = dict(newResults)
每当我尝试运行脚本时,都会收到错误消息AttributeError: 'generateTable' object has no attribute 'model'
。IPython 指出了这一点bse = dict(model.bse)
。同样,当我以交互方式运行它时(即逐块运行,无类),这有效,但是当我导入文件并尝试运行它时,我收到错误。
编辑:
1)它是如何创建的?
import project
a = project.generateTable('/path/to/test.tex', models, center='True', parens='se', var_names=None)
a.createModel()
2)整个回溯是:
In [26]: a.createModel()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/path/to/project/<ipython-input-26-6774b6d1804c> in <module>()
----> 1 a.createModel()
/path/to/project/project.py in createModel(self)
39 for model in models:
40 params = dict(model.params)
---> 41 bse = dict(model.bse)
42 pvals = dict(model.pvalues)
43 results.append(dict((k, [params[k], bse[k], pvals[k]]) for k in sorted(params.iterkeys())))
3)我将尝试将 var 名称更改为model
其他名称。
4) 抱歉语法错误。谢谢你,asmeurer,为修复。
编辑2:
上面的 3(将 var 名称更改为model
其他名称)不起作用。
编辑 3:
现在可以了。这是最新的代码:
def __init__(self, output, models, center='True', parens='se', var_names=None):
self.output = output
self.models = models
self.center = center
self.parens = parens
self.var_names = var_names
def createModel(self):
results = []
for test_model in self.models:
params = dict(test_model.params)
bse = dict(test_model.bse)
pvals = dict(test_model.pvalues)
results.append(dict((k, [params[k], bse.get(k), pvals.get(k)]) for k in sorted(params.iterkeys())))
唯一不同的是 for 循环中的模型已更改为 test_model。我之前尝试过,但没有成功,所以我不太确定它是如何发生的。
感谢大家的帮助!如果有人能指出为什么这个改变摆脱了那个特定的错误信息,那将不胜感激。我想真正知道我在做什么,而不是继续前进并接受它。