I have a list of time series with variable length, such that,

[ts.shape for ts in ts_list]
# output
# [(50, 1), (56, 1), (120, 1), ...]

and the label indicating whether the time series is normal or abnormal,

# output
# [0, 1, 1, 0, ...]
len(label_list) == len(ts_list)
# True

I searched for examples of time series classification using LSTM, but got few results. Is there an example showing how to do LSTM time series classification using keras? In my case, how should I process the original data and feed into the LSTM model in keras?


closed as off-topic by Sycorax, Michael Chernick, kjetil b halvorsen, Ferdi, mdewey Jul 4 '18 at 15:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Sycorax, Michael Chernick, kjetil b halvorsen, Ferdi, mdewey
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ If time-series values are discrete, you can try to train a Markov Model on your "normal" examples. Given a new time-series, the model can output a probability of this time-series being "normal" or "abnormal". $\endgroup$ – Vladislavs Dovgalecs Jul 2 '18 at 4:02
  • $\begingroup$ @Vladislavs Dovgalecs It's a good idea. But the time-series values are continuous, so I have to use other models such as LSTM or CNN. $\endgroup$ – Koho Jul 2 '18 at 6:42
  • $\begingroup$ What do you mean by whether the series is normal or abnormal? $\endgroup$ – Jan Kukacka Jul 2 '18 at 9:37
  • 1
    $\begingroup$ @Jan Kukacka It's an anomaly detection problem from time series. $\endgroup$ – Koho Jul 2 '18 at 10:02

there are examples out there, like from machinelearningmastery, from a kaggle kernel, another kaggle example.

The things you should do before going for LSTMs in keras is you should pad the input sequences, you can see that your inputs have varying sequence length 50,56,120 etc. So that you would get uniform length, let's say you are going to fix on sequence length 120. the sequence with less than 120 get's filled with 0s (default) and greater than 120 get stripped off.

And LSTM accepts a 3D tensor as input, meaning you need an extra dimension called timestep, which handles on how long you are giving importance. In case of stocks based details, you'd have observations in relevance to a minute. So your input tensor should be of dimensions: (batch_size, timestep, sequence_length). So you have to convert your padded (batch_size, sequence_length). You can use an Embedding Layer for that, which takes 2D sparse vector and converts into a 3D tensor, but I have used them only on text based time series classification.

Code snippet:

github gist

I guess that's it. Hope it helps.

  • $\begingroup$ Can you give a code snippet on how to do this preprocessing and modeling using keras? $\endgroup$ – Koho Jul 2 '18 at 6:52
  • $\begingroup$ @Koho just did. $\endgroup$ – tenshi Jul 2 '18 at 8:04
  • $\begingroup$ Thank you for sharing your code. It works well on text sequence, but what about numerical sequence, which points in sequence are continuous number. Do I just pad each sequence into fixed length and replace the Embedding layer with Masking layer? $\endgroup$ – Koho Jul 2 '18 at 9:16
  • $\begingroup$ Masking layer is usually for missing values and pad them with some other value while passing it to the net. If you want pad them earlier as I did for text, in text each word is replaced with some integer by Tokenizer, but you already have integer values. So that's good. Pad them, pass them, but if you want LSTM to work, you have to make the 2D tensor input to 3D tensor according to the timestep (how long). $\endgroup$ – tenshi Jul 2 '18 at 9:24
  • $\begingroup$ I have a little confusion about the timestep. So after padding, we have data shape like (batch_size, sequence_length). For each sequence(row), we have timestep < sequence_length, and extract a new sequence from the start of the sequence with length timestep, next, we start from the second point of the sequence..., it acts like a window with size timestep moving from left to right and extracts many sequences from a single sequence, all the extracted sequences have the same target label of the original sequence. $\endgroup$ – Koho Jul 2 '18 at 9:55

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