python - 为什么我只能从 statsmodels OLS 拟合中获得一个参数

标签 python pandas linear-regression statsmodels

这是我正在做的事情:

$ 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])

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

最佳答案

试试这个:

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

documentation :

An intercept is not included by default and should be added by the user

statsmodels.tools.tools.add_constant

关于python - 为什么我只能从 statsmodels OLS 拟合中获得一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20701484/

相关文章:

python - 合并 pandas 中的条目

两个自变量(x,y 坐标)的带约束条件的 Matlab 最小二乘近似

python - 尝试使用 sheet_name=None 为文档中的每个工作表运行 python pandas 脚本但不工作

python - 为什么 dbus 服务无法正常运行?

python - 通过 Wizard Odoo 创造新的 One2many 记录

python - 发现一串数字中的模式

python - 使用规范化展平三重嵌套 JSON

python - 如何对 pandas 中的列进行多数投票

python - 如何计算python pandas数据框中行之间的相关性

python - 使用 .score() 方法时出错 : shapes (10719, 1) 和 (16,1) 未对齐 : 1 (dim 1) ! = 16 (dim 0)