2015-12-29 27 views
-3

我有以下的数据帧,我称之为main_frame:Python/Scikit-learn - 如何真正预测?

   Value  Value  1lag  2lag  3lag  4lag 
Date                  
2005-04-01 0.824427 0.892308 1.000000 0.000000 0.000000 0.000000 
2005-05-01 0.778626 0.953846 0.892308 1.000000 0.000000 0.000000 
2005-06-01 0.717557 1.000000 0.953846 0.892308 1.000000 0.000000 
2005-07-01 0.725191 0.000000 1.000000 0.953846 0.892308 1.000000 
2005-08-01 0.717557 1.000000 0.000000 1.000000 0.953846 0.892308 
2005-09-01 0.740458 0.861538 1.000000 0.000000 1.000000 0.953846 
2005-10-01 0.732824 0.877193 0.861538 1.000000 0.000000 1.000000 
2005-11-01 0.732824 1.000000 0.877193 0.861538 1.000000 0.000000 
2005-12-01 0.641221 1.000000 1.000000 0.877193 0.861538 1.000000 
2006-01-01 0.709924 0.614035 1.000000 1.000000 0.877193 0.861538 
2006-02-01 0.770992 0.649123 0.614035 1.000000 1.000000 0.877193 

我已经建立了以下模型:

predictor=main_frame.iloc[:,1:] 
target=main_frame.iloc[:,0] 

model=LinearRegression() 
model.fit(X=predictor,y=target) 

我知道,来预测,我现在应该model.predict使用()但是我很难理解预测函数的参数是如何工作的。我试图使用:

prediction=model.predict(target) 
print predict 

但是,这不断让我错误,我相信我误解了有关论点的事情。

如何设置预测工作的命令?

编辑

我加入了回溯

/Library/Python/2.7/site-packages/sklearn/utils/validation.py:386:  DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. 
DeprecationWarning) 
Traceback (most recent call last): 
File "/Users/file.py", line 61, in <module> 
prediction=model.predict(target) 
File "/Library/Python/2.7/site-packages/sklearn/linear_model/base.py", line 200, in predict 
return self._decision_function(X) 
File "/Library/Python/2.7/site-packages/sklearn/linear_model/base.py", line 185, in _decision_function 
dense_output=True) + self.intercept_ 
File "/Library/Python/2.7/site-packages/sklearn/utils/extmath.py", line 184, in safe_sparse_dot 
return fast_dot(a, b) 
ValueError: shapes (1,127) and (144,) not aligned: 127 (dim 1) != 144 (dim 0) 

EDIT 2

试图把我的问题,换句话说因此获得更好的回答:

考虑在上述模型中,我如何找出目标变量下一个周期的预测值?

+4

打印预测? (预测未定义) – Untitled123

+3

不要只说“错误”。始终在您的问题中包含完整的错误信息。 – BrenBarn

+3

你不应该根据来自'目标'的数据来预测,而是从新的样本(你之前称之为'预测') –

回答

2

您正在将错误的参数传递给预测函数。试试这个:

prediction=model.predict(predictor) 
print prediction 

请注意,模型已经使用“predictor”变量进行了训练。因此,您只能预测与“预测变量”变量具有完全相同数量的列的数据。

+0

谢谢。然而,预测让我阵列很长,我只想找出n + 1时期的预测值。我如何发现? – abutremutante

+0

您需要传入n + 1时段的预测值。因此,您需要获取第二个Value列(1lag,2lag,3lag和4lag列)的列值。然后模型可以为您提供第一个Value列的预测值。 因此,在你的例子中的数据,你应该训练模型除了最后一个(除了期间2006-02-01以外的所有行)。然后你使用你的模型来预测最后一行。 –

+0

非常感谢,那是我在任何地方找不到的细节。因此,我进入“预测”的那一长串数据是考虑到适合所创造的回归的每个时期的预测值,对吗? – abutremutante