使用statsmodel从Python中的GLM中提取系数 [英] Extracting coefficients from GLM in Python using statsmodel
问题描述
我有一个定义如下的模型:
I have a model which is defined as follows:
import statsmodels.formula.api as smf
model = smf.glm(formula="A ~ B + C + D", data=data, family=sm.families.Poisson()).fit()
模型具有如下所示的系数:
The model has coefficients which look like so:
Intercept 0.319813
C[T.foo] -1.058058
C[T.bar] -0.749859
D[T.foo] 0.217136
D[T.bar] 0.404791
B 0.262614
我可以通过执行model.params.Intercept
和model.params.B
来获取Intercept
和B
的值,但是我无法获取每个C
和D
的值.
I can grab the values of the Intercept
and B
by doing model.params.Intercept
and model.params.B
but I can't get the values of each C
and D
.
例如,我尝试过model.params.C[T.foo]
,但我却得到并出错.
I have tried model.params.C[T.foo]
for example, and I get and error.
如何从模型中获取特定值?
How would I get particular values from the model?
推荐答案
model.params
是pandas.Series.仅当条目名称是有效的python名称时,才可以作为属性访问.
model.params
is is a pandas.Series. Accessing as attribute is only possible if the name of the entry is a valid python name.
在这种情况下,您需要用引号将该名称索引,即model.params["C[T.foo]"]
In this case you need to index with the name in quotes, i.e. model.params["C[T.foo]"]
请参阅 http://pandas.pydata.org/pandas-docs/dev/indexing.html
这篇关于使用statsmodel从Python中的GLM中提取系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!