【问题标题】:Why do I get only one parameter from a statsmodels OLS fit为什么我只能从 statsmodels OLS 拟合中获得一个参数
【发布时间】:2014-01-09 04:32:56
【问题描述】:

这是我正在做的事情:

$ python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> import statsmodels.api as sm
>>> statsmodels.__version__
'0.5.0'
>>> import numpy 
>>> y = numpy.array([1,2,3,4,5,6,7,8,9])
>>> X = numpy.array([1,1,2,2,3,3,4,4,5])
>>> res_ols = sm.OLS(y, X).fit()
>>> res_ols.params
array([ 1.82352941])

我期待一个包含两个元素的数组?!? 截距和斜率系数?

【问题讨论】:

  • Docs:默认不包含拦截,应由用户添加。请参阅 statsmodels.tools.add_constant。
  • 这里add_constant()的意义是什么。当我在线性正则中生成模型时,我希望有一个截距,y = mX + C。让某人在输入向量之上添加常数的额外操作的意图是什么。
  • 有趣的是,如果您在 statsmodels 中使用类似 R 的公式 api,默认情况下会为您提供截距。

标签: python pandas linear-regression statsmodels


【解决方案1】:

试试这个:

X = sm.add_constant(X)
sm.OLS(y,X)

documentation

默认情况下不包含拦截,应由用户添加

statsmodels.tools.tools.add_constant

【讨论】:

  • 我正在查看 ols 示例 ate the wls page 所以我想这就是我忽略 add_constant() 的原因,因为该页面上没有提到它。
  • @behzad-nouri,如果你能看看这个,我将不胜感激:stackoverflow.com/questions/44747203/…
  • 对此我感到很困惑。为什么默认不添加拦截?为什么要运行线性回归没有血腥常数?这对我来说毫无意义。
【解决方案2】:

为了完整起见,这是可行的:

>>> import numpy 
>>> import statsmodels.api as sm
>>> y = numpy.array([1,2,3,4,5,6,7,8,9])
>>> X = numpy.array([1,1,2,2,3,3,4,4,5])
>>> X = sm.add_constant(X)
>>> res_ols = sm.OLS(y, X).fit()
>>> res_ols.params
array([-0.35714286,  1.92857143])

它确实给了我一个不同的斜率系数,但我猜我们现在的数字确实有一个截距。

【讨论】:

    【解决方案3】:

    试试这个,它对我有用:

    import statsmodels.formula.api as sm
    
    from statsmodels.api import add_constant
    
    X_train = add_constant(X_train)
    
    X_test = add_constant(X_test)
    
    
    model = sm.OLS(y_train,X_train)
    
    results = model.fit()
    
    y_pred=results.predict(X_test)
    
    results.params
    

    【讨论】:

    • 请改用import statsmodels.api as sm。下一版本formula.api将不再有OLS(大写),只有ols(公式界面小写)
    【解决方案4】:

    我正在运行 0.6.1,看起来“add_constant”函数已移至 statsmodels.tools 模块中。这是我运行的有效方法:

    res_ols = sm.OLS(y, statsmodels.tools.add_constant(X)).fit()
    

    【讨论】:

      【解决方案5】:

      我确实添加了代码 X = sm.add_constant(X),但 python 没有返回截距值,所以我决定使用一点代数自己在代码中完成:

      此代码计算 35 个样本的回归,7 个特征加上一个截距值,我将其作为特征添加到方程中:

      import statsmodels.api as sm
      from sklearn import datasets ## imports datasets from scikit-learn
      import numpy as np
      import pandas as pd
      
      x=np.empty((35,8)) # (numSamples, oneIntercept + numFeatures))
      feature_names = np.empty((8,))
      y = np.empty((35,))
      
      dbfv = open("dataset.csv").readlines()
      
      
      interceptConstant = 1;
      i = 0
      # reading data and writing in numpy arrays
      while i<len(dbfv):
          cells = dbfv[i].split(",")
          j = 0
          x[i][j] = interceptConstant
          feature_names[j] = str(j)
          while j<len(cells)-1:
              x[i][j+1] = cells[j]
              feature_names[j+1] = str(j+1)
              j += 1
          y[i] = cells[len(cells)-1]
          i += 1
      # creating dataframes
      df = pd.DataFrame(x, columns=feature_names)
      
      target = pd.DataFrame(y, columns=["TARGET"])
      
      X = df
      y = target["TARGET"]
      
      model = sm.OLS(y, X).fit()
      
      print(model.params)
      
      # predictions = model.predict(X) # make the predictions by the model
      
      
      # Print out the statistics
      print(model.summary())
      

      【讨论】:

        猜你喜欢
        • 2022-06-08
        • 2017-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 2012-01-11
        相关资源
        最近更新 更多