简体   繁体   English

用scikit学习时间序列预测

[英]Time series forecasting with scikit learn

I am a complete newbie to SVM-based forecasting and so looking for some guidance here. 我是基于SVM的预测的完全新手,所以在这里寻找一些指导。 I am trying to set-up a python code for forecasting a time-series, using SVM libraries of scikit-learn. 我正在尝试使用scikit-learn的SVM库来设置用于预测时间序列的python代码。

My data contains X values at 30 minute interval for the last 24 hours, and I need to predict y for the next timestamp. 我的数据包含过去24小时间隔30分钟的X值,我需要预测下一个时间戳的y值。 Here's what I have set up - 这就是我设置的 -

SVR(kernel='linear', C=1e3).fit(X, y).predict(X)

But for this prediction to work, I need the X value for the next timestamp, which is not available. 但是为了使这个预测起作用,我需要下一个时间戳的X值,这是不可用的。 How do I set this up to predict future y values? 如何设置此值以预测未来的y值?

You should use SVR this way: 你应该这样使用SVR

# prepare model and set parameters
svr_model = SVR(kernel='linear', C=1e3)
# fit your model with the training set
svr_model.fit(TRAINIG_SET, TAINING_LABEL)
#predict on a test set
svr_model.predict(TEST_SET)

So, the problem here is that you have a training set but not a test set to measure your model accuracy. 所以,这里的问题是你有一套训练集,但没有测试集来测量模型的准确性。 The only solution is to use a part of your training set as test set ex: 80% for train 20% for test 唯一的解决方案是使用训练集的一部分作为测试集ex: 80% for train 20% for test

EDIT 编辑

Hope I well understood what you want from your comments. 希望我很好地理解你的意见。

So you want to predict the next label for the last hour in your train set, here is an example of what you want: 因此,您想预测火车组中最后一小时的下一个标签,以下是您想要的示例:

from sklearn.svm import SVR
import random
import numpy as np

'''
data: the train set, 24 elements
label: label for each time
'''

data = [10+y for  y in [x * .5 for x in range(24)]]
label =  [z for z in [random.random()]*24]

# reshaping the train set and the label ...

DATA = np.array([data]).T
LABEL = np.array(label)

# Declaring model and fitting it

clf  = SVR(kernel='linear', C=1e3)
clf.fit(DATA, LABEL)

# predict the next label 

to_predict = DATA[DATA[23,0]+0.5]

print clf.predict(to_predict)

>> 0.94407674

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM