Tensorflow预测未来100个值

wkyowqbh  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(152)

我不知道如何预测未来的100个值?我有一个值数组(1000),我需要预测未来的100个值。
请检查我的代码

close_arr = close_arr[::-1]
close = np.array(close_arr)
print(close)
print(len(close))
# Dataframe must have columns "ds" and "y" with the dates and values respectively.
df = pd.DataFrame({'y': close}) #'ds': timestamp,

df = df[['y']]

# prepere data for tensorflow
scaler = MinMaxScaler(feature_range=(0, 1))
yahoo_stock_prices = scaler.fit_transform(df)

train_size = int(len(yahoo_stock_prices) * 0.80)
print(f"train_size: {train_size}")
test_size = len(yahoo_stock_prices) - train_size
train, test = yahoo_stock_prices[0:train_size, :], yahoo_stock_prices[train_size:len(yahoo_stock_prices), :]
print(len(train), len(test))

# reshape into X=t and Y=t+1
look_back = 1 
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)


trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

# Step 2 Build Model
model = Sequential()
model.add(LSTM(50, activation='relu', return_sequences=True, input_shape=(1, look_back)))
model.add(LSTM(50, activation='relu', return_sequences=True))
model.add(Dense(25))
model.add(Dense(1, activation='linear'))

model.compile(loss='mse', optimizer='rmsprop')

model.fit(trainX, trainX, batch_size=128, epochs=100,validation_split=0.05)
model.evaluate(trainX, trainX, verbose=2)
predict_length = 50 
# how to predict next 50 values from testX?
# Step 1 - predict the future values
predicted = model.predict(testX)
print(f"predicted: {scaler.inverse_transform(np.array(predicted).reshape(-1, 1))}")
print(len(predicted))
# Step 2 - Plot the predictions!
plot_results_multiple(predicted, testX, predict_length)

代码是工作,但我只收到相同长度的redicted数据,而不是未来的预测。
这里也是我收到的结果enter image description here

ncecgwcz

ncecgwcz1#

首先,需要准备输出数据(y_train,y_test),使其具有(number_of_rows,100)的形状。网络的最后一层应该是100,而不是1。

model.add(Dense(100, activation='linear'))

此外,lock_back表示使用最后几天的天数,在本例中为1,应该更多,因此首先查看准备数据函数,并以某种方式更改该函数,以获得形状100的输出。将网络的输出图层固定为100

相关问题