当前位置:   article > 正文

基于SVM的时间序列预测-python实现(附源码)_svm时间序列预测python

svm时间序列预测python

首先我们需要一些数据来对其进行预测:

第0列是我们的序列号,single1,single2,single3是我们需要预测的数据,我们首先需要对其进行读取:

  1. def read_20180829():
  2. fname = "20180829.xlsx"
  3. bk = xlrd.open_workbook(fname)
  4. # shxrange = range(bk.nsheets)
  5. try:
  6. sh = bk.sheet_by_name("Sheet1")
  7. except:
  8. print("no sheet in %s named Sheet1" % fname)
  9. # 获取行数
  10. nrows = sh.nrows
  11. # 获取列数
  12. ncols = sh.ncols
  13. # 获取第一行第一列数据
  14. cell_value = sh.cell_value(1, 0)
  15. time = []
  16. single1 = []
  17. single2 = []
  18. single3 = []
  19. # 获取各行数据
  20. for i in range(1, nrows):
  21. row_data = sh.cell_value(i, 0)
  22. time.append(row_data)
  23. for i in range(1, nrows):
  24. row_data = sh.cell_value(i, 1)
  25. single1.append(row_data)
  26. for i in range(1, nrows):
  27. row_data = sh.cell_value(i, 2)
  28. single2.append(row_data)
  29. for i in range(1, nrows):
  30. row_data = sh.cell_value(i, 3)
  31. single3.append(row_data)
  32. return time,single1,single2,single3

得到数据之后我们就可以对其进行预测了:

  1. import numpy as np
  2. from matplotlib import pyplot as plt
  3. from sklearn.svm import SVR
  4. from read_data import read_20180829
  5. time,single1,single2,single3 = read_20180829()
  6. # 需要预测的长度是多少
  7. long_predict = 40
  8. def svm_timeseries_prediction(c_parameter,gamma_paramenter):
  9. X_data = time
  10. Y_data = single1
  11. print(len(X_data))
  12. # 整个数据的长度
  13. long = len(X_data)
  14. # 取前多少个X_data预测下一个数据
  15. X_long = 1
  16. error = []
  17. svr_rbf = SVR(kernel='rbf', C=c_parameter, gamma=gamma_paramenter)
  18. # svr_rbf = SVR(kernel='rbf', C=1e5, gamma=1e1)
  19. # svr_rbf = SVR(kernel='linear',C=1e5)
  20. # svr_rbf = SVR(kernel='poly',C=1e2, degree=1)
  21. X = []
  22. Y = []
  23. for k in range(len(X_data) - X_long - 1):
  24. t = k + X_long
  25. X.append(Y_data[k:t])
  26. Y.append(Y_data[t + 1])
  27. y_rbf = svr_rbf.fit(X[:-long_predict], Y[:-long_predict]).predict(X[:])
  28. for e in range(len(y_rbf)):
  29. error.append(Y_data[X_long + 1 + e] - y_rbf[e])
  30. return X_data,Y_data,X_data[X_long+1:],y_rbf,error
  31. X_data,Y_data,X_prediction,y_prediction,error = svm_timeseries_prediction(10,1)
  32. figure = plt.figure()
  33. tick_plot = figure.add_subplot(2, 1, 1)
  34. tick_plot.plot(X_data, Y_data, label='data', color='green', linestyle='-')
  35. tick_plot.axvline(x=X_data[-long_predict], alpha=0.2, color='gray')
  36. # tick_plot.plot(X_data[:-X_long-1], y_rbf, label='data', color='red', linestyle='--')
  37. tick_plot.plot(X_prediction, y_prediction, label='data', color='red', linestyle='--')
  38. tick_plot = figure.add_subplot(2, 1, 2)
  39. tick_plot.plot(X_prediction,error)
  40. plt.show()

相关数据、代码网址:https://github.com/ZhiqiangHo/code-of-csdn/tree/master/time_series_prediction/SVM

我的微信公众号名称:小小何先生
公众号介绍:主要研究强化学习、计算机视觉、深度学习、机器学习等相关内容,分享学习过程中的学习笔记和心得!期待您的关注,欢迎一起学习交流进步!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/397117?site
推荐阅读
相关标签
  

闽ICP备14008679号