How to extract the regression coefficient from statsmodels.api?

前端 未结 3 887
余生分开走
余生分开走 2021-02-07 05:34
 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,

3条回答
  •  逝去的感伤
    2021-02-07 06:00

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

    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.

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

提交回复
热议问题