亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

LSTM X 值在預測上發生了變化?

LSTM X 值在預測上發生了變化?

飲歌長嘯 2022-05-11 16:36:09
簡介和問題:我對 Keras 和深度學習有些陌生,我正在嘗試使用 LSTM 預測特斯拉的股價。我絕對是一個機器學習/深度學習的初學者,所以我希望有更多知識和經驗的人可以幫助指導我朝著正確的方向前進。我的網絡在 y 值預測上表現良好。但似乎 x 值向原點左移太遠了。似乎如果我將數據向右移動,預測實際上會非常好。下面是預測圖的圖片:我很確定錯誤來自我制作 X_test 值數組時。以下是將以下所有代碼組織成部分:數據集:我的火車數據是特斯拉從 2014 年到 2018 年的 4 年收盤價。我要預測的數據是 2019 年的收盤價。# get 2014-2018 data to train our modelstart = datetime.datetime(2014,1,1)end = datetime.datetime(2018,12,30)df = web.DataReader("TSLA", 'yahoo', start, end) # get 2019 data to test our model on start = datetime.datetime(2019,1,1)end = datetime.date.today()test_df = web.DataReader("TSLA", 'yahoo', start, end) # sort by datedf = df.sort_values('Date')test_df = test_df.sort_values('Date')# fix the date df.reset_index(inplace=True)df.set_index("Date", inplace=True)test_df.reset_index(inplace=True)test_df.set_index("Date", inplace=True)df.tail()                  High         Low        Open       Close   Volume  Date                                                                  2014-01-02  152.479996  146.550003  149.800003  150.100006  6188400   2014-01-03  152.190002  148.600006  150.000000  149.559998  4695000   2014-01-06  150.399994  145.240005  150.000000  147.000000  5361100   2014-01-07  150.399994  145.250000  147.619995  149.360001  5034100   2014-01-08  153.699997  148.759995  148.850006  151.279999  6163200   ...                ...         ...         ...         ...      ...   2018-12-24  314.500000  295.200012  313.500000  295.390015  5559900   2018-12-26  326.970001  294.089996  300.000000  326.089996  8163100   2018-12-27  322.170013  301.500000  319.839996  316.130005  8575100   2018-12-28  336.239990  318.410004  323.100006  333.869995  9939000   2018-12-31  339.209991  325.260010  337.790009  332.799988  6302300創建火車數據:# create train set of adj close prices data:train_data = df.loc[:,'Adj Close'].as_matrix()print(train_data.shape) # 1258 for 循環前 train_data 的 len 為 1258,for 循環后為 1222。
查看完整描述

1 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

解決方案:

如果有人從谷歌找到這個我想通了。我將訓練和測試數據創建代碼更改為:


'''Function to create a dataset to feed into an LSTM'''

def create_dataset(dataset, look_back):

    dataX, dataY = [], []

    for i in range(len(dataset)-look_back):

        a = dataset[i:(i + look_back), 0]

        dataX.append(a)

        dataY.append(dataset[i + look_back, 0])

    return np.array(dataX), np.array(dataY)


# Create the data to train our model on:

time_steps = 36

X_train, y_train = create_dataset(train_data, time_steps)


# reshape it [samples, time steps, features]

X_train = np.reshape(X_train, (X_train.shape[0], 36, 1))


print(X_train.shape) # 1222, 36, 1

測試數據:

# Get the stock prices for 2019 to have our model make the predictions

test_data = test_df['Adj Close'].values

test_data = test_data.reshape(-1,1)

test_data = scaler.transform(test_data)


# Create the data to test our model on:

time_steps = 36

X_test, y_test = create_dataset(test_data, time_steps)


# store the original vals for plotting the predictions 

y_test = y_test.reshape(-1,1)

org_y = scaler.inverse_transform(y_test)


# reshape it [samples, time steps, features]

X_test = np.reshape(X_test, (X_test.shape[0], 36, 1))


# Predict the prices with the model

predicted_y = model.predict(X_test)

predicted_y = scaler.inverse_transform(predicted_y)

新預測:

我將其更改為繪制存儲的原始 y val org_y,然后繪制我們預測的 y val


plt.plot(org_y, color = 'red', label = 'Real Tesla Stock Price')

plt.plot(predicted_y, color = 'blue', label = 'Predicted Tesla Stock Price')

plt.title('Tesla Stock Price Prediction')

plt.xlabel('Time')

plt.ylabel('Tesla Stock Price')

plt.legend()

plt.show()

http://img1.sycdn.imooc.com//627b75ca0001ec2903900279.jpg

查看完整回答
反對 回復 2022-05-11
  • 1 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號