Python 2.7 股票价格的错误预测
我用Python编写了一段代码,分析了谷歌金融公司过去1年苹果的历史数据。下面是数据集的链接。我正在使用的代码如下所示:Python 2.7 股票价格的错误预测,python-2.7,machine-learning,scikit-learn,stock,Python 2.7,Machine Learning,Scikit Learn,Stock,我用Python编写了一段代码,分析了谷歌金融公司过去1年苹果的历史数据。下面是数据集的链接。我正在使用的代码如下所示: import csv import numpy as np from sklearn.svm import SVR import matplotlib.pyplot as plt import time t=time.time() dates=[] prices=[] f1=open('rbf.txt','wb') f2=open('lin.txt','wb') f3=o
import csv
import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
import time
t=time.time()
dates=[]
prices=[]
f1=open('rbf.txt','wb')
f2=open('lin.txt','wb')
f3=open('poly.txt','wb')
def get_data(fname):
with open(fname,'rb') as csvfile:
csvFileReader=csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
l=row[0].split()
s=l[1].strip(',')
dates.append(int(s))
prices.append(float(row[1]))
return
def predict_prices(dates,prices,x):
dates=np.reshape(dates,(len(dates),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_lin.fit(dates,prices)
svr_poly.fit(dates,prices)
svr_rbf.fit(dates,prices)
f1.write(svr_rbf.predict(dates))
print type(svr_rbf.predict(dates))
print type(svr_rbf.predict(dates)[0])
print type(svr_rbf.predict(dates)[0])
f2.write(svr_lin.predict(dates))
f3.write(svr_poly.predict(dates))
'''
plt.scatter(dates,prices,color='black',label='Data')
plt.plot(dates,svr_rbf.predict(dates),color='red',label='RBF model')
plt.plot(dates,svr_lin.predict(dates),color='green',label='Linear model')
plt.plot(dates,svr_poly.predict(dates),color='blue',label='Polynomial model')
plt.xlabel('Dates')
plt.ylabel('Prices')
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('aapl.csv')
predicted_price=predict_prices(dates,prices,6)
f1.close()
f2.close()
f3.close()
print predicted_price
print time.time()-t
我需要解释系统返回的最终预测值。我作为输出得到的值,是我调用预测函数时作为硬编码参数传递的日期的股票价格,还是其他什么?如果是,我得到的输出是由35点。当前数据集的顺序与此相反,即最新优先。如果我先让它变老,它会给我正确的预测吗?这个类比的原因是,它的预测结果是大约109,这是1年前的股票价格,也是文件最后一行的股票价值,有1年前的数据。我该如何着手纠正它?请给我指一下正确的方向。提前谢谢。我得到的结果是:日期的数据类型是什么?@VivekKumar日期和价格都是数据类型列表。顺便说一句,我尝试反转列表,然后在predict_prices函数中使用它。它仍然给出了同样的预测。前一天的开盘价为138.8,但今天的预测为109.29。这个错误是由于小数据集还是其他原因造成的?这取决于。您可以这样做,也可以进行一次热编码。尝试不同的方法,看看哪种方法效果好。你所说的训练参数可以是一系列不同的特性。请参阅示例文档如何使用编号。只需将它们组合成一个向量X(带有形状[n_样本,n_特征]),price应该是您的y(目标),然后将X,y(只有两个参数)传递到函数中