这是我的代码
def create_dataset(data, look_back=1):
dataX, dataY = [], []
for i in range(len(data) - look_back):
dataX.append(data[i:(i + look_back), 0])
dataY.append(data[i + look_back, 0])
return np.array(dataX), np.array(dataY)
look_back = 10
df = pd.read_csv('kospi.csv')
data = df.Close.values.astype('float32')
data = data.reshape(len(df), 1)
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(data)
train_size = int(len(data) * 0.80)
test_size = len(data) - train_size
train = data[0:train_size]
test = data[train_size:len(data)]
x_train, y_train = create_dataset(train, look_back)
x_test, y_test = create_dataset(test, look_back)
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
model = Sequential()
model.add(LSTM(512, input_shape=(None, 1), return_sequences=True))
model.add(Dropout(0.3))
model.add(LSTM(512, input_shape=(None, 1)))
model.add(Dropout(0.3))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='rmsprop', metrics=['accuracy'])
model.summary()
hist = model.fit(x_train, y_train, epochs=10, batch_size=16, verbose=2)
p = model.predict(x_test)
plt.plot(y_test)
plt.plot(p)
plt.legend(['testY', 'p'], loc='upper right')
plt.title(mean_squared_error(y_test, p))
# plt.savefig(datetime.datetime.now())
# plt.savefig(name)
plt.show()
此模型predcit(x_test)
很好用
现在我要预测下100个值
我尝试了p = model.predict(x_test[-1])
但是看起来不正确的方法
如何获得下一个N个预测值?
我认为我们可以使用model.predict
解决它。
但是我不知道输入什么以及如何做出预测。
我尝试了很多,但无法解决。我该如何解决这个问题?
kospi.csv链接为https://docs.google.com/spreadsheets/d/1Q6Lh07Yg-OGtuz_Rvy979gcA65jrEqVNJNjndklCbg4/edit?usp=sharing
答案 0 :(得分:0)
您可以使用lstm预测值。 替换为以下代码:
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from keras.models import Sequential
def build_improved_model(input_dim, output_dim, return_sequences):
"""
Builds an improved Long Short term memory model using keras.layers.recurrent.lstm
:param input_dim: input dimension of model
:param output_dim: ouput dimension of model
:param return_sequences: return sequence for the model
:return: a 3 layered LSTM model
"""
model = Sequential()
model.add(LSTM(
input_shape=(None, input_dim),
units=output_dim,
return_sequences=return_sequences))
model.add(Dropout(0.2))
model.add(LSTM(
128,
return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(
units=1))
model.add(Activation('linear'))
return model
def build_basic_model(input_dim, output_dim, return_sequences):
"""
Builds a basic lstm model
:param input_dim: input dimension of the model
:param output_dim: output dimension of the model
:param return_sequences: return sequence of the model
:return: a basic lstm model with 3 layers.
"""
model = Sequential()
model.add(LSTM(
input_shape=(None, input_dim),
units=output_dim,
return_sequences=return_sequences))
model.add(LSTM(
100,
return_sequences=False))
model.add(Dense(
units=1))
model.add(Activation('linear'))
return model