3
votes

I have created an LSTM sales prediction model that works really well on the train and test sets. I would now like to predict beyond the dates in the entire dataset.

I have tried following this answer how to use the Keras model to forecast for future dates or events? but I really can't figure out how to adjust my code to do future predictions.

Also, I changed my code from

X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0:1]
X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0:1]
X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])

to

X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 1:8]
X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 1:8]
X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])

after trying the solution in Keras time series can I predict next 6 month in one time

Here is the code where training and modelling happens:

# changed to initial
for df in m:
    train_set, test_set = m[df][0:-6].values, m[df][-6:].values

    #apply Min Max Scaler
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler = scaler.fit(train_set)

    # reshape training set
    train_set = train_set.reshape(train_set.shape[0], train_set.shape[1])
    train_set_scaled = scaler.transform(train_set)

    # reshape test set
    test_set = test_set.reshape(test_set.shape[0], test_set.shape[1])
    test_set_scaled = scaler.transform(test_set)

    #build the LSTM Model
    X_train, y_train = train_set_scaled[:, 1:], train_set_scaled[:, 0:1]
    X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
    X_test, y_test = test_set_scaled[:, 1:], test_set_scaled[:, 0:1]
    X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])

    print('Fitting model for: {}'.format(df))

    #fit our LSTM Model
    model = Sequential()
    model.add(LSTM(4, batch_input_shape=(1, X_train.shape[1], X_train.shape[2]), stateful=True))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(X_train, y_train, nb_epoch=500, batch_size=1, verbose=1, shuffle=False)

#     model.save('lstm_model.h5')


    print('Predictions for: {}'.format(df))

    #check prediction
    y_pred = model.predict(X_test,batch_size=1)

    print('Inverse Transform for: {}'.format(df))

    #inverse transformation to see actual sales
    #reshape y_pred
    y_pred = y_pred.reshape(y_pred.shape[0], 1, y_pred.shape[1])
    #rebuild test set for inverse transform
    pred_test_set = []
    for index in range(0,len(y_pred)):
        print (np.concatenate([y_pred[index],X_test[index]],axis=1))
        pred_test_set.append(np.concatenate([y_pred[index],X_test[index]],axis=1))
    #reshape pred_test_set
    pred_test_set = np.array(pred_test_set)
    pred_test_set = pred_test_set.reshape(pred_test_set.shape[0], pred_test_set.shape[2])
    #inverse transform
    pred_test_set_inverted = scaler.inverse_transform(pred_test_set)


I would like the predictions to go beyond the data in the dataset.

UPDATE: I trained the model and took its predictions on the test set. Use these as input for another LSTM model to fit and predict for 12 months. It worked for me. Also changed my last Dense layer (above) to predict 1 point at a time instead of 7 as I had before. Below is the code:

from numpy import array
for df in d:
    if df in list_df:
        # df_ADIDAS DYN PUL DEO 150 FCA5421
        #KEEP

        result_list = []
        sales_dates = list(d["{}".format(df)][-7:].Month)
        act_sales = list(d["{}".format(df)][-7:].Sale)
        for index in range(0,len(pred_test_set_inverted)):
            result_dict = {}
            result_dict['pred_value'] = int(pred_test_set_inverted[index][0] + act_sales[index]) #change to 0 ffrom act_sales[index]
            result_dict['date'] = sales_dates[index] #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>REVIEW
            result_list.append(result_dict)

        df_result = pd.DataFrame(result_list)
        predictions = list(df_result['pred_value'])

        forecasts = []
        result_list
        for i in range(len(result_list)):
            forecasts.append(result_list[i]['pred_value'])

        def split_sequence(sequence, n_steps):
            X, y = list(), list()
            for i in range(len(sequence)):
                # find the end of this pattern
                end_ix = i + n_steps
                # check if we are beyond the sequence
                if end_ix > len(sequence)-1:
                    break
                # gather input and output parts of the pattern
                seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
                X.append(seq_x)
                y.append(seq_y)
            return array(X), array(y)

        # choose a number of time steps
        n_steps = 4
        # split into samples
        X, y = split_sequence(forecasts, n_steps)
        # summarize the data
        #     for i in range(len(X)):
        #         print(X[i], y[i])

        n_features = 1
        X = X.reshape((X.shape[0], X.shape[1], n_features))
        # define model
        model = Sequential()
        model.add(LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
        model.add(Dense(1))
        model.compile(optimizer='adam', loss='mse')
        # fit model
        model.fit(X, y, epochs=200, verbose=0)
        # demonstrate prediction
        x_input = array(predictions[-4:])
        x_input = x_input.reshape((1, n_steps, n_features))
        yhat = model.predict(x_input, verbose=0)
        #print(yhat)

        currentStep = yhat[:, -1:]

        print('Twelve Month Prediction for {}'.format(df))
        for i in range(12):
            if i == 0:
                x_input = x_input.reshape((1, n_steps, n_features))
                yhat = model.predict(x_input, verbose=0)
                print(yhat)
            else:
                x0_input = np.append(x_input, [currentStep[i-1]])
                x0_input = x0_input.reshape((1, n_steps+1, n_features))
                x_input = x0_input[:,1:]
                yhat = model.predict(x_input)
                currentStep = np.append(currentStep, yhat[:,-1:])
                print(yhat)
1
Hi, i am a newbie to this. I have cerated my model to predict, but unable to pass it to new model to predict. Can you share your code please.Ankit
could you please start a new question with your code on it?Job Collins
I don't understand your solution. The new model that you have only has the predictions done for the test set that you already had? How can this have any accuracy, when you don'T actually have any data in the model.fit() from the original data?Snow

1 Answers

0
votes

Your last Dense layer says that you are predicting 7 points at a time. Save those predictions and feed them to the model again to predict next 7. That makes it 14 predictions simultaneously. And so on. Or change the number of nodes and shape of y from 7 to corresponding number and train again.