预测:使用SVR模块进行未来事件的时间序列预测

3
我想使用scikit-learn中的SVR模块执行未来事件的时间序列预测。以下是我试图使用的源代码:
import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt


plt.switch_backend('newbackend')

seq_num=[]
win=[]

def get_data(filename):
    with open(filename, 'r') as csvfile:
        csvFileReader = csv.reader(csvfile)
        next(csvFileReader) # skipping column names
        for row in csvFileReader:
            seq_num.append(int(row[0])
            win.append(int(row[6]))
    return

def predict_win(X, y, x):
    win = np.reshape(X,(len(X), 1)) 

    svr_lin = SVR(kernel= 'linear', C= 1e3)
    svr_poly = SVR(kernel= 'poly', C= 1e3, degree= 2)
    svr_rbf = SVR(kernel= 'rbf', C= 1e3, gamma= 0.1) 
    svr_rbf.fit(X, y) 
    svr_lin.fit(X, y)
    svr_poly.fit(X, y)

    plt.scatter(X, y, color= 'black', label= 'Data') 
    plt.plot(y, svr_rbf.predict(X), color= 'red', label= 'RBF model') 
    plt.plot(y,svr_lin.predict(X), color= 'green', label= 'Linear model') 
    plt.plot(y,svr_poly.predict(X), color= 'blue', label= 'Polynomial model') 
    plt.xlabel('X, other features')
    plt.ylabel('win')
    plt.title('Support Vector Regression')
    plt.legend()
    plt.show()

    return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]

get_data('net_data.csv') 


predicted_win = predict_win(X, y, 29)

我的数据集非常庞大,因此在结尾处仅包含了部分csv数据集。我对第7列感兴趣。我想预测第7列的值何时增加或减少。是否可能只查看第7列并进行时间序列预测?任何帮助都将不胜感激。谢谢!

0.007804347,10.0.0.11:49438,10.0.12.12:5001,32,3796291040,3796277984,10,2147483647,28960,3034,29312
0.007856739,10.0.0.11:49438,10.0.12.12:5001,32,3796293936,3796278008,11,2147483647,29056,2999,29312
0.010605189,10.0.0.11:49438,10.0.12.12:5001,32,3796320000,3796291040,20,2147483647,55040,2969,29312
0.010850907,10.0.0.11:49438,10.0.12.12:5001,32,3796348960,3796305520,30,2147483647,84096,2946,29312
0.013598458,10.0.0.11:49438,10.0.12.12:5001,32,3796377920,3796320000,40,2147483647,113024,2951,29312
0.01368011,10.0.0.11:49438,10.0.12.12:5001,32,3796434392,3796348960,60,2147483647,170880,2956,29312
0.015104265,10.0.0.11:49438,10.0.12.12:5001,32,3796434392,3796363440,70,2147483647,199936,2940,29312
0.016406964,10.0.0.11:49438,10.0.12.12:5001,32,3796490864,3796377920,80,2147483647,220160,2943,29312
0.016465876,10.0.0.11:49438,10.0.12.12:5001,32,3796537200,3796432944,81,80,330240,2925,29312
0.018355321,10.0.0.11:49438,10.0.12.12:5001,32,3796547336,3796434392,81,80,333056,2914,29312
0.020171945,10.0.0.11:49438,10.0.12.12:5001,32,3796603808,3796490864,83,80,382336,2956,29312
0.237314523,10.0.0.11:49438,10.0.12.12:5001,32,3810417728,3809658976,529,396,1775360,7109,29312
0.237409075,10.0.0.11:49438,10.0.12.12:5001,44,3810417728,3809700968,530,397,1859328,7381,29312
0.237486647,10.0.0.11:49438,10.0.12.12:5001,44,3810417728,3809700968,371,371,1960704,7365,29312
0.237807596,10.0.0.11:49438,10.0.12.12:5001,44,3810417728,3809700968,371,371,1980928,7362,29312
0.237989588,10.0.0.11:49438,10.0.12.12:5001,44,3810417728,3809700968,371,371,1989632,7400,29312
0.259123971,10.0.0.11:49438,10.0.12.12:5001,32,3811590608,3811251776,261,260,2267648,5885,29312
0.259174008,10.0.0.11:49438,10.0.12.12:5001,32,3811655768,3811289424,261,260,2267648,5918,29312
0.262546461,10.0.0.11:49438,10.0.12.12:5001,32,3811720928,3811354584,261,260,2267648,5823,29312
1个回答

2

好的,下面的svm函数存在问题:

第二行win = ...没有被使用,会导致错误。请删除它。

def predict_win(X, y, x):
    win = np.reshape(X,(len(X), 1))  # <----This line

    svr_lin = SVR(kernel= 'linear', C= 1e3)
    svr_poly = SVR(kernel= 'poly', C= 1e3, degree= 2)
    svr_rbf = SVR(kernel= 'rbf', C= 1e3, gamma= 0.1) 
    svr_rbf.fit(X, y) 
    svr_lin.fit(X, y)
    svr_poly.fit(X, y)

    plt.scatter(X, y, color= 'black', label= 'Data') 
    plt.plot(y, svr_rbf.predict(X), color= 'red', label= 'RBF model') 
    plt.plot(y,svr_lin.predict(X), color= 'green', label= 'Linear model') 
    plt.plot(y,svr_poly.predict(X), color= 'blue', label= 'Polynomial model') 
    plt.xlabel('X, other features')
    plt.ylabel('win')
    plt.title('Support Vector Regression')
    plt.legend()
    plt.show()

    return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]

第二,我不知道为什么要写一个完整的函数来读取csv文件。可以忽略它,使用pandas即可。这里有一段示例代码可以使用:
from sklearn import svm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

def predict_win(X,y,x):
    svr_lin = svm.SVR(kernel='linear',C=1e3)
    svr_poly = svm.SVR(kernel='poly',C=1e3, degree=2)
    svr_rbf = svm.SVR(kernel='rbf',C=1e3,gamma=0.1)
    svr_rbf.fit(X,y)
    svr_lin.fit(X,y)
    svr_poly.fit(X,y)

    plt.plot(y,svr_rbf.predict(X),color='red',label='RBF model')
    plt.plot(y,svr_lin.predict(X),color='green',label='Linear model')
    plt.plot(y,svr_poly.predict(X),color='blue', label='Polynomial model')
    plt.xlabel('X, other features')
    plt.ylabel('win')
    plt.title('Support Vector Regression')
    plt.legend()
    plt.show()
    return [svr_rbf.predict(x)[0],svr_lin.predict(x)[0],svr_poly.predict(x)[0]]

df = pd.read_csv('data.csv')

data_np_array = df.values

y = np.ndarray.copy(data_np_array[:,6])
Xleft = np.ndarray.copy(data_np_array[:,:6])
Xright = np.ndarray.copy(data_np_array[:,7:])
X = np.hstack((Xleft,Xright))

x0 = np.ndarray.copy(X[0,:])
xp = predict_win(X,y,x0)

percent_off = [min(data_np_array[0,2],prediction)/max(data_np_array[0,2],prediction) for prediction in xp]

在将数据导入后,您需要进行中间步骤来清理数据,将其从dataframe转换为numpy数组,并将第7列复制为回归拟合的列,然后从训练数据中删除该列,并重新构建一个新的数组,这些步骤必须在适配SVR之前完成。

df = pd.read_csv('data.csv')

data_np_array = df.values

y = np.ndarray.copy(data_np_array[:,6])
Xleft = np.ndarray.copy(data_np_array[:,:6])
Xright = np.ndarray.copy(data_np_array[:,7:])
X = np.hstack((Xleft,Xright))

如果这些可行,请告诉我。我只是从您上面的数据表中摘取了几行。


1
@Mahsolid 啊,那也许你只需要进行数组复制。我会在答案中添加这个。如果不行的话,请告诉我,我会测试运行代码。如果我无法得到相同的错误,你可以把 csv 发给我,但我应该能够让它正常工作。 - Chris
1
@mahsolid 好的,我已经复制了代码并进行了测试。看起来你原来的函数有一些问题。我不知道为什么你的源代码要使用CSV reader的包装器--应该使用pandas dataframe。这应该可以工作,让我知道结果。 - Chris
1
@Mahsolid 哦,是的,你必须要么删除那些列,要么将它们转换为数字。如果我是你,我会将它们删除,因为它们是日期。 - Chris
1
@Mahsolid 啊,是的,那么我会说它们甚至对预测来说更不相关——当你添加新数据时只会混淆一切,因为IP地址与任何事情都没有关系,是非随机的,永远不会重复——根据你的问题,在非训练预测中可能永远不会看到相同的IP地址情况。我在想,如果你在做体育运动方面的预测,日期可能是相关的。 - Chris
1
@Mahsolid 没问题,祝你好运! - Chris
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接