Python 如何从 statsmodels.api 中提取回归系数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47388258/
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-08-19 18:11:45  来源:igfitidea点击:

How to extract the regression coefficient from statsmodels.api?

pythonpandaslinear-regressionstatsmodels

提问by JOHN

 result = sm.OLS(gold_lookback, silver_lookback ).fit()

After I get the result, how can I get the coefficient and the constant?

得到结果后,如何得到系数和常数?

In other words, if y = ax + chow to get the values aand c?

换句话说,如果 y = ax + c如何获取值ac

回答by David Dale

You can use the paramsproperty of a fitted model to get the coefficients.

您可以使用params拟合模型的属性来获取系数。

For example, the following code:

例如,以下代码:

import statsmodels.api as sm
import numpy as np
np.random.seed(1)
X = sm.add_constant(np.arange(100))
y = np.dot(X, [1,2]) + np.random.normal(size=100)
result = sm.OLS(y, X).fit()
print(result.params)

will print you a numpy array [ 0.89516052 2.00334187]- estimates of intercept and slope respectively.

将打印一个 numpy 数组[ 0.89516052 2.00334187]- 分别估计截距和斜率。

If you want more information, you can use the object result.summary()that contains 3 detailed tables with model description.

如果您需要更多信息,您可以使用result.summary()包含 3 个详细表格和模型描述的对象。

回答by Idiot Tom

Cribbing from this answer Converting statsmodels summary object to Pandas Dataframe, it seems that the result.summary() is a set of tables, which you can export as html and then use Pandas to convert to a dataframe, which will allow you to directly index the values you want.

从这个答案Converting statsmodels summary object to Pandas Dataframe 抄袭,似乎 result.summary() 是一组表,您可以将其导出为 html,然后使用 Pandas 转换为数据帧,这将允许您直接索引你想要的价值观。

So, for your case (putting the answer from the above link into one line):

因此,对于您的情况(将上述链接中的答案放入一行):

df = pd.read_html(result.summary().tables[1].as_html(),header=0,index_col=0)[0]

And then

进而

a=df['coef'].values[1]
c=df['coef'].values[0]

回答by B Furtado

Adding up details on @IdiotTom answer.

添加有关@IdiotTom 答案的详细信息。

You may use:

您可以使用:

head = pd.read_html(res.summary2().as_html())[0]
body = pd.read_html(res.summary2().as_html())[1]

Not as nice, but the info is there.

不是很好,但信息在那里。