0
$\begingroup$

I have been trying to model Time Series forecast using Keras LSTM algorithm. My dataset consists of weekly sales data from Jan-2016 and I also have external features such as Festivals/Events each modeled as a flag (0/1) for each week. So for e.g. let's say 2016W01 has sales of 100 units and has flag value 1 for the NewYearFlag. Likewise, I have created flags for many such events such as Independence Day, Diwali (this is in a different week every year) and so on.

Based on the Time Series decomposition analysis,I also know that 5 week lags are signficant. So it is Week-1, Week-2, Week-3,Week-4 and Week-5 variables.

Essentially,my dataframe row looks like Week Number (Row Index), SalesQty, SalesQtyW-1,SalesQtyW-2, SalesQtyW-3,SalesQtyW-4,SalesQtyW-5,NewYearFlag,DiwaliFlag, IndepdenceDayFlag, ChristmasFlag.

So I have X = {SalesQtyW-1,SalesQtyW-2, SalesQtyW-3,SalesQtyW-4,SalesQtyW-5,NewYearFlag,DiwaliFlag, IndepdenceDayFlag, ChristmasFlag} Y = {SalesQty}

So when I pass my dataframe to Keras LSTM layer, I pass (144,9) as there 144 rows (52+52+40) in my training data.

I always get the error - Expected 3 dimensions but got only two (144,9)

I am not sure where I am going wrong. Please help. I checked most of the questions of similar nature but not able to find the right answer. Dont know where am I going wrong!!!

$\endgroup$
1
$\begingroup$

An LSTM will expect the data to be of the format (samples, time steps, features). In a univariate case where we will focus on just the time series with the sales data, where you say five lags are required as input:

sales data = [100, 150, 345, 5, 800, 655, 100, 200, 300, 400]

You could format your data like this using a sliding window of step size one:

[100, 150, 345, 5, 800]
[150, 345, 5, 800, 655]
[345, 5, 800, 655, 100]
[5, 800, 655, 100, 200]
[800, 655, 100, 200, 300]
[655, 100, 200, 300, 400]

this would give you six samples each of five time steps and one feature (the feature being the sales data).

So the shape you would take the 2d array above and reshape it to a 3D array of (samples, time steps, features) which you could also think about like (rows, columns, features), giving (6, 5, 1).

Let's say now you have another feature holidays like in your example, in this case you have two features, the sales data and whether or not it's a holiday or not. So you would need dimensions of (6, 5, 2), six samples each containing five time steps of data each with two series each (or features).

$\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.