I have created a Conv1D time series used to forecast weekly demand (my inputs are separated by 7 days). I am now trying to predict the next four weeks of demand. Below is my preprocessing function to achieve the 12 step array:
def df_to_X_y(df, window_size):
avg = df.mean()
df_as_np = df.to_numpy()
X = []
y = []
for i in range(len(df_as_np)-window_size):
row = [[a] for a in df_as_np[i:i+window_size]]
X.append(row)
label = df_as_np[i+window_size]
y.append(label)
return np.array(X), np.array(y)
I then created a function to iterate thru each dataset and form a prediction for each set:
def conv1D_Time_Series():
test_data = []
tndates = []
ttdates = []
for i in itemList:
train_dates, test_dates = train_test_split(i['WeekDate'], test_size = 0.2, random_state = 25)
tndates.append(train_dates)
ttdates.append(test_dates)
for i in itemList:
training_data, testing_data = train_test_split(i['WeightOrdered'], test_size =0.3, random_state = 25)
WINDOW_SIZE = 12
X_train, y_train = df_to_X_y(training_data, WINDOW_SIZE)
X_test, y_test = df_to_X_y(testing_data, WINDOW_SIZE)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
modelx = Sequential()
modelx.add(InputLayer((WINDOW_SIZE,1)))
modelx.add(Conv1D(64, kernel_size = 2, padding = 'same'))
modelx.add(Flatten())
modelx.add(Dense(8, 'relu'))
modelx.add(Dense(1, 'linear'))
cpx = ModelCheckpoint('modelx/', save_best_only = True)
modelx.compile(loss = MeanSquaredError(), optimizer = Adam(learning_rate = 0.0001), metrics = [RootMeanSquaredError()])
modelx.fit(X_train, y_train, epochs = 500, callbacks = [cpx])
X_FUTURE = 4
pred = np.array([])
last = X_test[-12:][0]
for i in range(X_FUTURE):
curr_prediction = modelx.predict(np.array([last])).flatten()
print(curr_prediction)
last = np.concatenate([last[1:], curr_prediction[0]]).flatten()
pred = np.concatenate([pred, curr_prediction[0]])
pred = scaler.iverse_transform([pred])[0]
return pred
The last for-loop block is my attempt at creating this new array of predictions to base further predictions from, yet I am receiving a Value error :
ValueError: all the input arrays must have same number of dimensions, but the array
at index 0 has 2 dimension(s) and the array at index 1 has 0 dimension(s)
Since my window size is 12, ideally I would like to use the last 12 to predict the first unknown, then 11+prediction to get the 2nd unknown, 10+prediction+prediction to get the 3rd unknown, and so on.