首页 文章

用scikit学习时间序列预测

提问于
浏览
5

我是基于SVM的预测的完全新手,所以在这里寻找一些指导 . 我正在尝试使用scikit-learn的SVM库来设置用于预测时间序列的python代码 .

我的数据包含过去24小时间隔30分钟的X值,我需要预测下一个时间戳的y值 . 这就是我设置的 -

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

但是为了使这个预测起作用,我需要下一个时间戳的X值,这是不可用的 . 如何设置此值以预测未来的y值?

1 回答

  • 3

    你应该这样使用 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)
    

    所以,这里的问题是你有一套训练集,但没有测试集来测量模型的准确性 . 唯一的解决方案是使用训练集的一部分作为测试集 ex: 80% for train 20% for test

    EDIT

    希望我很好地理解你的意见 .

    因此,您想预测火车组中最后一小时的下一个标签,以下是您想要的示例:

    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
    

相关问题