我试图用SVM训练和预测如下:
for i in range(numStocks):
y_p = []
X, y, X_p, scaler, stockUsed = randomStockData(stockList)
clf = svm.SVR(kernel='rbf', C=1, gamma=0.1)
clf.fit(X, y)
reshapedX_p = X_p.reshape(1,-1)
for j in range(5):
y_p.append(clf.predict(reshapedX_p))
reshapedX_p = np.append(reshapedX_p[0][1:],y_p[-1])
reshapedX_p = reshapedX_p.reshape(1, reshapedX_p.shape[0])
y_p = [x*scaler for x in y_p]
rescaledX_p = [x*scaler for x in X_p]
print(y_p)
plt.plot(rescaledX_p, label='closep test')
plt.plot(range(len(rescaledX_p),len(rescaledX_p)+len(y_p)), y_p, label='predicted')
plt.legend(loc='lower left', shadow=True)
pylab.savefig(homePath+'predictResults/'+stockUsed+'.png', facecolor='#ffffff', edgecolor='#ffffff')
plt.close()
查看最后几个"预测输入" (reshapedX_p)在每次迭代时,也使用y_p:
内部for循环中的 print(reshapedX_p[0][-5:], y_p)
(array([ 0.00458431, 0.00465051, 0.00470016, 0.00466706, 0.00462568]), [])
(array([ 0.00465051, 0.00470016, 0.00466706, 0.00462568, 0.00415898]), [array([ 0.00415898])])
(array([ 0.00470016, 0.00466706, 0.00462568, 0.00415898, 0.00415898]), [array([ 0.00415898]), array([ 0.00415898])])
(array([ 0.00466706, 0.00462568, 0.00415898, 0.00415898, 0.00415898]), [array([ 0.00415898]), array([ 0.00415898]), array([ 0.00415898])])
(array([ 0.00462568, 0.00415898, 0.00415898, 0.00415898, 0.00415898]), [array([ 0.00415898]), array([ 0.00415898]), array([ 0.00415898]), array([ 0.00415898])])
很明显,每一步的预测输入实际上都在变化,但SVM只是吐出与第一次预测后的最后一步完全相同的值。
我是否每次都需要重新安装SVM?我不认为我会这么做。