【问题标题】:Stock analysis with python SVM/R for prediction of future values. Issue when running code使用 python SVM/R 进行股票分析以预测未来价值。运行代码时的问题
【发布时间】:2021-05-20 10:25:55
【问题描述】:

这只是代码的一部分。它运行但它永远不会完成或停止。这只是一个持续的过程。谁能看到我哪里出错了?

我相信这可能与数据重塑和对重塑数据的 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', C=1000.0, 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()

【问题讨论】:

    标签: python machine-learning svm analysis stock


    【解决方案1】:

    当您只发布部分代码而不是全部代码时,很难知道问题出在哪里。在下面试试我的代码示例。

    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

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 2019-03-17
      • 2019-05-12
      • 1970-01-01
      • 2020-06-19
      • 2021-09-08
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多