I can't figure out how to predict next 100 values in future? I have array of values(1000) and I need to predict next 100 values.
Please check my code
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)
def create_dataset(dataset, look_back=1):
# prepare data
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i look_back), 0]
dataX.append(a)
dataY.append(dataset[i look_back, 0])
return np.array(dataX), np.array(dataY)
Code is work but I receive only redicted data with same length, not future prediction.
Also here is result what I received enter image description here
CodePudding user response:
First of all, you need to prepare your output data (y_train, y_test) to have the shape of (number_of_rows, 100). The last layer of your network should be 100, not 1.
model.add(Dense(100, activation='linear'))
Also, the lock_back means how many of the last days to use which is 1 in this case, it should be more, so First look at prepare data function and change there in a way to have outputs of shape 100. Fix the output layer of your network to have 100
CodePudding user response:
I would assume that look_back
refers to the number of lags of the time-series you are using, which in this case is 1. Not sure where do you import the create_dataset
function, but I would assume that it creates your datasets so the X contains the values of the time-series at time t-1
and the Y contains the values at time t
.
That being said you have two options for generating a forecast for 100 time steps.
You could train your model to predict 1 time step as you have done in your code. In order to generate a forecast for 100 time steps ahead you need to iteratively input into the model your last forecast in order to produce a forecast for the next time step.
The other option is to use this
create_dataset
function to set up the dataset so that your Y datasets contain 100 time steps. This means that the model be set up to output a sequence of 100 values.
Hope this helps!