如何使用LSTM模型预测未来的预测?

如何使用该模型预测未来值?我尝试将时间步长窗口更改为大于股票数据库的值,但只得到一个错误,说元组索引超出范围。我该如何预测未来值,而不是在现有数据上测试模型?这是我的代码:

import numpy as npimport matplotlib.pyplot as pltimport pandas as pddataset_train = pd.read_csv(r'/path', error_bad_lines = False)training_set = dataset_train.iloc[:, 1:2].valuesfrom sklearn.preprocessing import MinMaxScalersc = MinMaxScaler(feature_range = (0, 1))sc_training_set = sc.fit_transform(training_set)X_train = []y_train = []for i in range (1, 220):    X_train.append(sc_training_set[i-1:i, 0])    y_train.append(sc_training_set[i, 0])X_train, y_train = np.array(X_train), np.array(y_train)X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))from keras.models import Sequentialfrom keras.layers import Densefrom keras.layers import LSTMfrom keras.layers import Dropoutregressor = Sequential()regressor.add(LSTM(units = 64, return_sequences = True, input_shape = (X_train.shape[1], 1)))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 128, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 256, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 512, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 256, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 128, return_sequences = True))regressor.add(Dropout(0.2))regressor.add(LSTM(units = 64))regressor.add(Dropout(0.2))regressor.add(Dense(units = 1))regressor.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])regressor.fit(X_train, y_train, epochs = 10, batch_size = 32)dataset_test = []X_test = []for i in range(220, 500):    X_test.append(sc_training_set[i-1:i, 0])X_test = np.array(X_test)X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))pred_stock = regressor.predict(X_test)pred_stock = sc.inverse_transform(pred_stock)

回答:

这里是预测未来的伪代码。基本上,您需要不断将最新的预测值添加到时间序列中。

您不能仅仅增加时间步长的大小,否则您将试图访问超出范围的索引。

predictions = []last_x = (the last x value in your data)while len(predictions) < #_of_predictions_you_want:    p = model.predict(last_x)    predictions.append(p)    last_x = np.roll(x, -1)    last_x[-1] = p

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注