【发布时间】:2018-01-29 06:07:09
【问题描述】:
抱歉交叉发布但无法通过它我无法从预测函数获得输出:
我有一个 OLS 模型,它曾经与 SM .6 一起工作,但现在不能在 .8 中工作,而且 Pandas 从 19.2 增加到 20.3,所以这可能是问题吗?
我只是不明白我需要为 predict 方法提供什么。 所以我的模型创建看起来像:
def fit_line2(x, y):
X = sm.add_constant(x, prepend=True) #Add a column of ones to allow the calculation of the intercept
ols_test = sm.OLS(y, X,missing='drop').fit()
"""Return slope, intercept of best fit line."""
X = sm.add_constant(x)
return ols_test
效果很好,我得到了一个模型,可以很好地看到摘要。 我曾经这样做是为了通过使用我在 SM .6 中工作的最新值(我想向前预测)来提前一个时期进行预测 预测调用如下:
yrahead=ols_test.predict(ols_input)
ols 输入是从 pandas DF 创建的:
ols_input=(sm.add_constant(merged2.lastqu[-1:], prepend=True))
lastqu
2018-12-31 13209.0
type:
<class 'pandas.core.frame.DataFrame'>
调用预测为:
yrahead=ols_test.predict(ols_input)
这给了我一个错误: ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
我尝试通过将 ols_input 更改为:
13209.0
Type:
<class 'numpy.float64'>
这给了我一个类似的错误: ValueError:形状 (1,1) 和 (2,) 未对齐:1 (dim 1) != 2 (dim 0)
不知道去哪里?
上面的基本 DataFrame 表(merged2)看起来像这样,最后一行 lastqu 列包含我想要预测 Units 的值:
Units lastqu Uperchg lqperchg
2000-12-31 19391.000000 NaN NaN NaN
2001-12-31 35068.000000 5925.0 80.85 NaN
2002-12-31 39279.000000 8063.0 12.01 36.08
2003-12-31 47517.000000 9473.0 20.97 17.49
2004-12-31 51439.000000 11226.0 8.25 18.51
2005-12-31 59674.000000 11667.0 16.01 3.93
2006-12-31 58664.000000 14016.0 -1.69 20.13
2007-12-31 55698.000000 13186.0 -5.06 -5.92
2008-12-31 42235.000000 11343.0 -24.17 -13.98
2009-12-31 40478.333333 7867.0 -4.16 -30.64
2010-12-31 38721.666667 8114.0 -4.34 3.14
2011-12-31 36965.000000 8361.0 -4.54 3.04
2012-12-31 39132.000000 8608.0 5.86 2.95
2013-12-31 43160.000000 9016.0 10.29 4.74
2014-12-31 44520.000000 9785.0 3.15 8.53
2015-12-31 49966.000000 10351.0 12.23 5.78
2016-12-31 53752.000000 10884.0 7.58 5.15
2017-12-31 57571.000000 12109.0 7.10 11.26
2018-12-31 NaN 13209.0 NaN 9.08
所以我使用 OLS 针对 lastqu 来预测 2018 年的单位
我坦率地承认,我并不真正理解为什么 SM .6 会以它的方式工作,但确实如此!
【问题讨论】:
标签: python pandas statsmodels