如何解决使用 python SVM/R 进行股票分析以预测未来价值运行代码时出现问题
这只是代码的一部分。它运行但永远不会完成或停止。这只是一个持续的过程。谁能看出我哪里出错了?
我相信这可能与数据重塑有关,然后是对重塑数据的 svr 分析,因为我已尝试通过注释掉行和查看错误来手动排除故障。一切似乎都在运行,直到到达 'lin_svr'。
代码使用数据集并创建两个 numpy 数组:'index_list' 和 'adj_close_prices'。指数和收盘价分别附加到这些数组,然后代码尝试根据收盘价绘制指数。这是代码的目的。
你能指出代码中的错误可能在哪里吗?
index_list = np.array([])
adj_close_prices = np.array([])
##get the index and adjusted close prices
df_days = df.loc[:,'index']
df_adj_close = df.loc[:,'Adj Close']
for day in df_days:
index_list = np.append(index_list,float(day))
for adj_close_price in df_adj_close:
adj_close_prices = np.append(adj_close_prices,float(adj_close_price))
index_list = index_list.reshape(-1,1)
lin_svr = SVR(kernel='linear',C=1000.0)
lin_svr.fit(index_list,adj_close_prices)
poly_svr = SVR(kernel='poly',C=1000.0,degree=2)
poly_svr.fit(index_list,adj_close_prices)
rbf_svr = SVR(kernel='rbf',gamma=0.15)
rbf_svr.fit(index_list,adj_close_prices)
##plot
plt.figure(figsize=(16,8))
plt.scatter(index_list,adj_close_prices,color='red',label='Data')
plt.plot(index_list,rbf_svr.predict(index_list),color='green',label='RBF Model')
plt.plot(index_list,poly_svr.predict(index_list),color='orange',label='Polynomial Model')
plt.plot(index_list,lin_svr.predict(index_list),color='blue',label='Linear Model')
plt.legend()
plt.show()
解决方法
当您只发布部分代码而不是全部代码时,很难知道问题出在哪里。试试我下面的代码示例。
from utils import *
import time
import numpy as np
import pandas as pd
from mxnet import nd,autograd,gluon
from mxnet.gluon import nn,rnn
import mxnet as mx
import datetime
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import math
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
import xgboost as xgb
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore")
context = mx.cpu(); model_ctx=mx.cpu()
mx.random.seed(1719)
def parser(x):
return datetime.datetime.strptime(x,'%Y-%m-%d')
import yfinance as yf
# Get the data for the stock AAPL
start = '2019-06-30'
end = '2020-06-30'
data = yf.download('SBUX',start,end)
data = data.reset_index()
data
data.dtypes
# re-name field from 'Adj Close' to 'Adj_Close'
data = data.rename(columns={"Adj Close": "Adj_Close"})
data
data = data.loc[:,['Date','Adj_Close']]
# Plot all the close prices
# ((data.pct_change()+1).cumprod()).plot(figsize=(10,7))
plt.figure(figsize=(14,5),dpi=100)
plt.plot(data['Date'],data['Adj_Close'],label='Starbucks Stock Price')
plt.xlabel('Date')
plt.ylabel('USD')
plt.title('Figure 2: Starbucks Stock Price')
plt.legend()
plt.show()
这应该能让你指明正确的方向。
您可以在此处找到更多类似的代码示例。
https://github.com/ASH-WICUS/Notebooks/blob/master/ARIMA%20Stock%20Price%20Prediction.ipynb
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。