1
$\begingroup$

I want to forecast the future sales for products.

My Questions:

  1. How I have to set up the dataset for time series forecasting?
  2. What I have to change, to get int values from output?

Model and additional infos

X.shape(68, 108, 124)

y.shape(68, 108, 1)

X
array([[2008,    0,    0, ...,    0,    0,    0],
        [2008,    0,    0, ...,    0,    0,    5],
        [2008,    0,    0, ...,    0,    0,    0],
        ...,
        [2008,    0,    0, ...,    0,    1,    0],
        [2008,    0,    1, ...,    0,    0,    7],
        [2008,    1,    0, ...,    0,    0,    8]],

       [[2009,    0,    0, ...,    0,    0,    0],
        [2009,    0,    0, ...,    0,    0,    0],
        [2009,    0,    0, ...,    0,    1,    10],
        ...,
        [2009,    0,    0, ...,    0,    0,    20],
        [2009,    0,    1, ...,    0,    2,    0],
        [2009,    1,    0, ...,    0,    0,    5]]])

the hole dataset have 68 sequences (each historic year = one sequence), each sequence 108 rows and 124 features. Features: year(int) + countries are one hot encoded (108 different) + 15 product categories (one hot encoded)

output

model.predict(X_train_lstm)

       [[-0.00230993, -0.5376476 , -0.15334567, ...,  0.09862291,
          0.16895522, -0.15376607],
        [ 0.09281429, -0.61930186, -0.30336168, ...,  0.03798292,
          0.19980578, -0.11670589],
        [ 0.10739403, -0.6318171 , -0.32635477, ...,  0.02868856,
          0.20453428, -0.11102566],
        ...,
        [ 0.10971781, -0.63381183, -0.33001956, ...,  0.02720715,
          0.2052879 , -0.11012031],
        [ 0.10971781, -0.63381183, -0.33001956, ...,  0.02720715,
          0.20528793, -0.11012033],
        [ 0.1097179 , -0.63381183, -0.33001956, ...,  0.02720721,
          0.2052879 , -0.1101203 ]],

       ...,
X.shape(68, 108, 124)
y.shape(68, 108, 1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=seed, shuffle=False)
X_validation, X_test, y_validation, y_test = train_test_split(X_test, y_test, test_size=0.5, random_state=seed, shuffle=False)

model = Sequential()
model.add(LSTM(units=50, input_shape=(108,124), name="LSTM", return_sequences=True))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=10, batch_size=20, verbose=1, validation_data=(X_validation, y_validation))
$\endgroup$

1 Answer 1

1
$\begingroup$

keras LSTM time series uses vectors of data looking back or forward. you create the dataset with overlapping vectors.

def create_dataset(dataset, look_back=3):
    dataX, dataY = [], []
    for i in range(len(dataset)-look_back-1):
        a = dataset[i:(i+look_back)]
        dataX.append(a)
        dataY.append(dataset[i + look_back])
    return np.array(dataX), np.array(dataY)


COLUMNS=['open']
dataset=df[COLUMNS]
#dataset=np.array(dataset).reshape(-1,1)
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(np.array(dataset).reshape(-1,1))
#dataset=np.array(pd.DataFrame({"C1":[1,2,3,4,5,6],"C2":[-1,-2,-3,-4,-5,-6],"Y":[7,8,9,10,11,12]})).reshape(-1,1)

train_size = int(len(dataset) * 0.60)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size], dataset[train_size:len(dataset)]
#print(len(train), len(test))

look_back=3
trainX=[]
testX=[]
y_train=[]

trainX, y_train = create_dataset(train, look_back)
testX, y_test = create_dataset(test, look_back)

#print(y_train)

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


n_future = 1
features=2
timeSteps=4

model = Sequential()

model.add(Bidirectional(LSTM(units=50, return_sequences=True, 
                             input_shape=(X_train.shape[1], 1))))

model.add(LSTM(units= 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units= 50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units= 50))
model.add(Dropout(0.2))
model.add(Dense(units = n_future))

model.compile(optimizer="adam", loss="mean_squared_error", metrics=["acc"])



plot_model(model, to_file='model.png')
img=plt.imread('model.png')
plt.imshow(img)
plt.show()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.