pandas 使用 statsmodel 从 Python 中的 GLM 中提取系数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29165601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:04:33  来源:igfitidea点击:

Extracting coefficients from GLM in Python using statsmodel

pythonpandasstatsmodels

提问by user2844485

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

I can grab the values of the Interceptand Bby doing model.params.Interceptand model.params.Bbut I can't get the values of each Cand D.

我可以抓住的值Intercept,并B通过做model.params.Interceptmodel.params.B,但我不能让每个值CD

I have tried model.params.C[T.foo]for example, and I get and error.

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

How would I get particular values from the model?

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

回答by Josef

model.paramsis is a pandas.Series. Accessing as attribute is only possible if the name of the entry is a valid python name.

model.params是一个pandas.Series。只有当条目的名称是有效的 python 名称时,才能访问 as 属性。

In this case you need to index with the name in quotes, i.e. model.params["C[T.foo]"]

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

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

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