【问题标题】:Regression in PythonPython中的回归
【发布时间】:2013-09-03 15:29:32
【问题描述】:

尝试通过 pandas 和 statsmodels 进行逻辑回归。不知道为什么我会收到错误或如何解决它。

import pandas as pd
import statsmodels.api as sm
x = [1, 3, 5, 6, 8]
y = [0, 1, 0, 1, 1]
d = { "x": pd.Series(x), "y": pd.Series(y)}
df = pd.DataFrame(d)

model = "y ~ x"
glm = sm.Logit(model, df=df).fit()

错误:

Traceback (most recent call last):
  File "regress.py", line 45, in <module>
    glm = sm.Logit(model, df=df).fit()
TypeError: __init__() takes exactly 3 arguments (2 given)

【问题讨论】:

    标签: python pandas regression statsmodels


    【解决方案1】:

    您不能将公式传递给Logit。做:

    In [82]: import patsy
    
    In [83]: f = 'y ~ x'
    
    In [84]: y, X = patsy.dmatrices(f, df, return_type='dataframe')
    
    In [85]: sm.Logit(y, X).fit().summary()
    Optimization terminated successfully.
             Current function value: 0.511631
             Iterations 6
    Out[85]:
    <class 'statsmodels.iolib.summary.Summary'>
    """
                               Logit Regression Results
    ==============================================================================
    Dep. Variable:                      y   No. Observations:                    5
    Model:                          Logit   Df Residuals:                        3
    Method:                           MLE   Df Model:                            1
    Date:                Fri, 30 Aug 2013   Pseudo R-squ.:                  0.2398
    Time:                        16:56:38   Log-Likelihood:                -2.5582
    converged:                       True   LL-Null:                       -3.3651
                                            LLR p-value:                    0.2040
    ==============================================================================
                     coef    std err          z      P>|z|      [95.0% Conf. Int.]
    ------------------------------------------------------------------------------
    Intercept     -2.0544      2.452     -0.838      0.402        -6.861     2.752
    x              0.5672      0.528      1.073      0.283        -0.468     1.603
    ==============================================================================
    """
    

    这几乎直接来自the docs on how to do exactly what you're asking

    编辑:您还可以按照@user333700 的建议使用公式 API:

    In [22]: print sm.formula.logit(model, data=df).fit().summary()
    Optimization terminated successfully.
             Current function value: 0.511631
             Iterations 6
                               Logit Regression Results
    ==============================================================================
    Dep. Variable:                      y   No. Observations:                    5
    Model:                          Logit   Df Residuals:                        3
    Method:                           MLE   Df Model:                            1
    Date:                Fri, 30 Aug 2013   Pseudo R-squ.:                  0.2398
    Time:                        18:14:26   Log-Likelihood:                -2.5582
    converged:                       True   LL-Null:                       -3.3651
                                            LLR p-value:                    0.2040
    ==============================================================================
                     coef    std err          z      P>|z|      [95.0% Conf. Int.]
    ------------------------------------------------------------------------------
    Intercept     -2.0544      2.452     -0.838      0.402        -6.861     2.752
    x              0.5672      0.528      1.073      0.283        -0.468     1.603
    ==============================================================================
    

    【讨论】:

    • 或者使用公式函数import statsmodels.api as smf然后smf.logit(formula, ...)
    • 感谢 Phillip 提供更正的答案。我的评论是禁食。我想写import statsmodels.formula.api as smf,它还可以访问公式界面的快捷方式、小写函数。这些只是模型的 from_formula 方法的便利包装,例如 sm.Logit.from_formula
    • 如何定义参考类别?这不起作用: f = 'C(y,Treatment(0)) ~ x '
    【解决方案2】:

    您也可以直接在 Logit 中传递公式。

    Logit.from_formula('y ~ x',data=data).fit()
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 2013-03-04
      • 2017-05-12
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      相关资源
      最近更新 更多