【问题标题】:Extracting coefficients from GLM in Python using statsmodel使用 statsmodel 从 Python 中的 GLM 中提取系数
【发布时间】:2015-05-23 19:22:53
【问题描述】:

我有一个定义如下的模型:

import statsmodels.formula.api as smf
model = smf.glm(formula="A ~ B + C + D", data=data, family=sm.families.Poisson()).fit()

该模型的系数如下所示:

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.Interceptmodel.params.B 来获取InterceptB 的值,但我无法获取每个CD 的值。

例如,我尝试过model.params.C[T.foo],但我得到了错误。

如何从模型中获取特定值?

【问题讨论】:

标签: python pandas statsmodels


【解决方案1】:

model.params 是 pandas.Series。仅当条目的名称是有效的 python 名称时,才能作为属性访问。

在这种情况下,您需要使用引号中的名称进行索引,即model.params["C[T.foo]"]

http://pandas.pydata.org/pandas-docs/dev/indexing.html

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 2021-02-12
    • 2018-11-19
    • 1970-01-01
    • 2014-07-13
    • 2013-10-01
    • 1970-01-01
    • 2013-03-06
    相关资源
    最近更新 更多