How to Handle Missing Timesteps in Sequence Prediction Problems with Python

It is common to have missing observations from sequence data.

Data may be corrupt or unavailable, but it is also possible that your data has variable length sequences by definition. Those sequences with fewer timesteps may be considered to have missing values.

In this tutorial, you will discover how you can handle data with missing values for sequence prediction problems in Python with the Keras deep learning library.

After completing this tutorial, you will know:

  • How to remove rows that contain a missing timestep.
  • How to mark missing timesteps and force the network to learn their meaning.
  • How to mask missing timesteps and exclude them from calculations in the model.

Discover how to build models for multivariate and multi-step time series forecasting with LSTMs and more in my new book, with 25 step-by-step tutorials and full source code.

Let’s get started.

A Gentle Introduction to Linear Algebra

A Gentle Introduction to Linear Algebra
Photo by Steve Corey, some rights reserved.

Overview

This section is divided into 3 parts; they are:

  1. Echo Sequence Prediction Problem
  2. Handling Missing Sequence Data
  3. Learning With Missing Sequence Values

Environment

This tutorial assumes you have a Python SciPy environment installed. You can use either Python 2 or 3 with this example.

This tutorial assumes you have Keras (v2.0.4+) installed with either the TensorFlow (v1.1.0+) or Theano (v0.9+) backend.

This tutorial also assumes you have scikit-learn, Pandas, NumPy, and Matplotlib installed.

If you need help setting up your Python environment, see this post:

Echo Sequence Prediction Problem

The echo problem is a contrived sequence prediction problem where the objective is to remember and predict an observation at a fixed prior timestep, called a lag observation.

For example, the simplest case is to predict the observation from the previous timestep that is, echo it back. For example:

The question is, what do we do about timestep 1?

We can implement the echo sequence prediction problem in Python.

This involves two steps: the generation of random sequences and the transformation of random sequences into a supervised learning problem.

Generate Random Sequence

We can generate sequences of random values between 0 and 1 using the random() function in the random module.

We can put this in a function called generate_sequence() that will generate a sequence of random floating point values for the desired number of timesteps.

This function is listed below.

Need help with Deep Learning for Time Series?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Download Your FREE Mini-Course

Frame as Supervised Learning

Sequences must be framed as a supervised learning problem when using neural networks.

That means the sequence needs to be divided into input and output pairs.

The problem can be framed as making a prediction based on a function of the current and previous timesteps.

Or more formally:

Where y(t) is the desired output for the current timestep, f() is the function we are seeking to approximate with our neural network, and X(t) and X(t-1) are the observations for the current and previous timesteps.

The output could be equal to the previous observation, for example, y(t) = X(t-1), but it could as easily be y(t) = X(t). The model that we train on this problem does not know the true formulation and must learn this relationship.

This mimics real sequence prediction problems where we specify the model as a function of some fixed set of sequenced timesteps, but we don’t know the actual functional relationship from past observations to the desired output value.

We can implement this framing of an echo problem as a supervised learning problem in python.

The Pandas shift() function can be used to create a shifted version of the sequence that can be used to represent the observations at the prior timestep. This can be concatenated with the raw sequence to provide the X(t-1) and X(t) input values.

We can then take the values from the Pandas DataFrame as the input sequence (X) and use the first column as the output sequence (y).

Putting this all together, we can define a function that takes the number of timesteps as an argument and returns X,y data for sequence learning called generate_data().

Sequence Problem Demonstration

We can tie the generate_sequence() and generate_data() code together into a worked example.

The complete example is listed below.

Running this example generates a sequence, converts it to a supervised representation, and prints each X,y pair.

We can see that we have NaN values on the first row.

This is because we do not have a prior observation for the first value in the sequence. We have to fill that space with something.

But we cannot fit a model with NaN inputs.

Handling Missing Sequence Data

There are two main ways to handle missing sequence data.

They are to remove rows with missing data and to fill the missing timesteps with another value.

For more general methods for handling missing data, see the post:

The best approach for handling missing sequence data will depend on your problem and your chosen network configuration. I would recommend exploring each method and see what works best.

Remove Missing Sequence Data

In the case where we are echoing the observation in the previous timestep, the first row of data does not contain any useful information.

That is, in the example above, given the input:

and the output:

There is nothing meaningful that can be learned or predicted.

The best case here is to delete this row.

We can do this during the formulation of the sequence as a supervised learning problem by removing all rows that contain a NaN value. Specifically, the dropna() function can be called prior to splitting the data into X and y components.

The complete example is listed below:

Running the example results in 9 X,y pairs instead of 10, with the first row removed.

Replace Missing Sequence Data

In the case when the echo problem is configured to echo the observation at the current timestep, then the first row will contain meaningful information.

For example, we can change the definition of y from values[:, 0] to values[:, 1] and re-run the demonstration to produce a sample of this problem, as follows:

We can see that the first row is given the input:

and the output:

Which could be learned from the input.

The problem is, we still have a NaN value to handle.

Instead of removing the rows with NaN values, we can replace all NaN values with a specific value that does not appear naturally in the input, such as -1. To do this, we can use the fillna() Pandas function.

The complete example is listed below:

Running the example, we can see that the NaN value in the first column of the first row was replaced with a -1 value.

Learning with Missing Sequence Values

There are two main options when learning a sequence prediction problem with marked missing values.

The problem can be modeled as-is and we can encourage the model to learn that a specific value means “missing.” Alternately, the special missing values can be masked and explicitly excluded from the prediction calculations.

We will take a look at both cases for the contrived “echo the current observation” problem with two inputs.

Learning Missing Values

We can develop an LSTM for the prediction problem.

The input is defined by 2 timesteps with 1 feature. A small LSTM with 5 memory units in the first hidden layer is defined and a single output layer with a linear activation function.

The network will be fit using the mean squared error loss function and the efficient ADAM optimization algorithm with default configuration.

To ensure that the model learns a generalized solution to the problem, that is to always returns the input as output (y(t) == X(t)), we will generate a new random sequence every epoch. The network will be fit for 500 epochs and updates will be performed after each sample in each sequence (batch_size=1).

Once fit, another random sequence will be generated and the predictions from the model will be compared to the expected values. This will provide a concrete idea of the skill of the model.

Tying all of this together, the complete code listing is provided below.

Running the example prints the loss each epoch and compares the expected vs. the predicted output at the end of a run for one sequence.

Reviewing the final predictions, we can see that the network learned the problem and predicted “good enough” outputs, even in the presence of missing values.

You could experiment further with this example and mark 50% of the t-1 observations for a given sequence as -1 and see how that affects the skill of the model over time.

Masking Missing Values

The marked missing input values can be masked from all calculations in the network.

We can do this by using a Masking layer as the first layer to the network.

When defining the layer, we can specify which value in the input to mask. If all features for a timestep contain the masked value, then the whole timestep will be excluded from calculations.

This provides a middle ground between excluding the row completely and forcing the network to learn the impact of marked missing values.

Because the Masking layer is the first in the network, it must specify the expected shape of the input, as follows:

We can tie all of this together and re-run the example. The complete code listing is provided below.

Again, the loss is printed each epoch and the predictions are compared to expected values for a final sequence.

Again, the predictions appear good enough to a few decimal places.

Which Method to Choose?

These one-off experiments are not sufficient to evaluate what would work best on the simple echo sequence prediction problem.

They do provide templates that you can use on your own problems.

I would encourage you to explore the 3 different ways of handling missing values in your sequence prediction problems. They were:

  • Removing rows with missing values.
  • Mark and learn missing values.
  • Mask and learn without missing values.

Try each approach on your sequence prediction problem and double down on what appears to work best.

Summary

It is common to have missing values in sequence prediction problems if your sequences have variable lengths.

In this tutorial, you discovered how to handle missing data in sequence prediction problems in Python with Keras.

Specifically, you learned:

  • How to remove rows that contain a missing value.
  • How to mark missing values and force the model to learn their meaning.
  • How to mask missing values to exclude them from calculations in the model.

Do you have any questions about handling missing sequence data?
Ask your questions in the comments and I will do my best to answer.


Develop Deep Learning models for Time Series Today!

Deep Learning for Time Series Forecasting

Develop Your Own Forecasting models in Minutes

…with just a few lines of python code

Discover how in my new Ebook:
Deep Learning for Time Series Forecasting

It provides self-study tutorials on topics like: CNNs, LSTMs,
Multivariate Forecasting, Multi-Step Forecasting and much more…

Finally Bring Deep Learning to your Time Series Forecasting Projects

Skip the Academics. Just Results.

Click to learn more.


55 Responses to How to Handle Missing Timesteps in Sequence Prediction Problems with Python

  1. Nader June 21, 2017 at 9:57 am #

    Fantastic !

  2. James Mashiyane June 23, 2017 at 7:21 am #

    I really like your books, they have really helped me, I’m using 4 of them Time Series Forecasting, Machine Learning, Deep Learning, and, Machine Learning from scratch. Especially the Machine Learning from scratch has helped a lot with my python skills. I hope the Deep Learning from scratch, not using Tensor Flow and Keras will be coming soon. Thanks a lot.

  3. Adam July 28, 2017 at 1:33 pm #

    If I want to normalize input data, I should replace Missing data first or normalizing input data?

    • Jason Brownlee July 29, 2017 at 8:02 am #

      Yes, I would impute before scaling.

      • Josefine Wilms June 6, 2019 at 5:23 pm #

        Hi Jason

        Thanks for the tutorials.

        Not sure if I understand your answer to Adam correctly: Do you recommend that we first replace the nan’s with a value (say “-1”) and then scale?

        1) If so, the data will be scaled taking the -1 value into account: i.e.: If my data has a range of 10 to 50, but contains NaNs, then 10 will no longer be the minimum; -1 would be.

        2) Also, if I replace prior to scaling, I will need to change the “mask_value=-1” from -1 to the value to which -1 has now been scaled. Is that correct?

        Would it not be better to first scale and then replace the NaN’s?

        • Jason Brownlee June 7, 2019 at 7:52 am #

          If you replace missing with a value, like the average, do it first then scale.

          If you want to mask them out, scale first in a way that ignores the missing values.

  4. Jeff Lim August 29, 2017 at 12:36 pm #

    On Replace Missing Sequence Data, we should change the definition of y from values[:, 0] to values[:, 1], right?

    • Jason Brownlee August 29, 2017 at 5:13 pm #

      Yes, from the post:

      In the case when the echo problem is configured to echo the observation at the current timestep

  5. baojia li September 1, 2017 at 8:37 pm #

    if we change generate_sequence:
    def generate_sequence(n_timesteps):
    return random.randint(34,156,n_timesteps)

    The results and the real value will be a lot of error
    why?

    • Jason Brownlee September 2, 2017 at 6:08 am #

      Because neural networks cannot predict a pseudo random series.

  6. Darius December 24, 2017 at 2:02 pm #

    What if I collect values from internet every 5 minutes, but sometimes there are server issues and I miss some values. Could solution be to add another feature as input as timestamp of time when each set of features were captured? Would LSTM would make sense than features usually go every 300n but somtimes number is different.

    • Jason Brownlee December 25, 2017 at 5:23 am #

      You could add zero values to get the required length and use a mask in your model to ignore them.

  7. Andreas Pfrengle April 5, 2018 at 10:29 am #

    I am missing methods to sensibly impute missing data of uni- or multivariate time series and their pythonic implementation. I’m thinking of interpolation, autocorrelation or maybe other sophisticated unsupervised learning methods. Have you written about this somewhere?

  8. Gedas May 3, 2018 at 1:25 am #

    Just noting that stateless Sequential model (RNN) in Keras can be constructed with unspecified batch size. This allows train / validate / predict with different batch sizes:
    https://stackoverflow.com/questions/43702481/why-does-keras-lstm-batch-size-used-for-prediction-have-to-be-the-same-as-fittin

  9. Divya June 16, 2018 at 4:25 am #

    Does masking work for missing values in output sequence?

    • Jason Brownlee June 16, 2018 at 7:32 am #

      Not in the same way. You can use a “I don’t know” output, e.g. predict a 0 or something. Very useful in NLP problems.

  10. ChengHung September 6, 2018 at 3:55 am #

    “When defining the layer, we can specify which value in the input to mask. If all features for a timestep contain the masked value, then the whole timestep will be excluded from calculations.”

    Does this mean all featrures would be excluded? Or features with Nan value only?

    • Jason Brownlee September 6, 2018 at 5:41 am #

      We tell the Masking layer what to ignore, e.g. 0.0 by default.

  11. Nestak October 4, 2018 at 8:45 pm #

    Thanks, helpful post! Though in your examples you have a relatively small gap, compared to the total amount of data. I am facing a problem where I have a data set of 450 vectors and a gap between them of 250 consequent missing vectors. Do you have a recommended templates, examples, some other blog posts you would point at in such a case?

    • Jason Brownlee October 5, 2018 at 5:35 am #

      Perhaps try zero padding with a masking layer?
      Perhaps try ignoring the gap?
      Perhaps try imputing?
      Perhaps try splitting samples in such a way that the missing space is one sample you can skip?

      Let me know how you go.

  12. Bob October 24, 2018 at 7:48 pm #

    For example, we can change the definition of y from values[:, 0] to values[:, 0] and re-run the demonstration to produce a sample of this problem, as follows:

    It should be revised as below:

    For example, we can change the definition of y from values[:, 0] to values[:, -1] and re-run the demonstration to produce a sample of this problem, as follows:

    Is it right?

    • Jason Brownlee October 25, 2018 at 7:53 am #

      Nearly, I think values[:,1] from the complete example.

      Thanks, fixed.

  13. White October 26, 2018 at 3:41 pm #

    Great tutorial. I have a question. I am using keras to do a sequence tagging work (Bi-LSTM + CRF model) with different sequence lengths. I use masking layer to mask 0 value and sequence.pad_sequences() to pad training data with 0. I trained the model successfully, however, I met a problem when I predict the test data.
    I pad the test instances with 0, e.g., 23 -> 100(maxlen). In theory, the model will ignore the 77 “0” and only predict the 23 timesteps. But I get 100 prediction results and the latter 77 results are not 0 or null. I am confused. Have you met this situation before ? Is the masking layer in use ? Or I just need to ignore the latter 77 results. Thanks.

    • Jason Brownlee October 27, 2018 at 5:56 am #

      I’m not sure I follow, are you talking about masking inputs or making predictions with padding or both?

      • White October 28, 2018 at 12:55 pm #

        Both. If you mask inputs when you train your model, you must mask the test data in the same way when the model makes the prediction. Here is part of my model code:
        model = Sequential()
        model.add(Masking(mask_value=0, input_shape=(seq_length, features_length)))
        model.add(Bidirectional(LSTM(lstm_units_num, return_sequences=True)))
        model.add(Dropout(dropout_rate))
        model.add(Bidirectional(LSTM(lstm_units_num, return_sequences=True)))
        model.add(Dropout(dropout_rate))
        model.add(TimeDistributed(Dense(num_class, activation=”softmax”)))
        crf_layer = CRF(num_class)
        model.add(crf_layer).
        When I use the model to make the prediction, I get 100(seq_length) prediction results(all are not 0 or null), however, 77 of these 100 input timesteps are masked, they should not be predicted with a not-0 result. So I am very confused. I am not sure whether the prediction results are correct…

        • Jason Brownlee October 29, 2018 at 5:51 am #

          Predictions that are all 0 might suggest that the model has not yet learned the problem. Perhaps try training longer or an alternate model configuration?

          • White October 30, 2018 at 8:19 pm #

            Sorry, what you said is not my question… Let’s take an example, in my experiment, maxlen is 100, now the model has already been trained successfully (with masking layer). Assume there is a test instance(length is 23) and the model wants to predict it. First I use padding to pad this test case with 0 and then length of the test instance becomes 100 (latter 77 values are all 0). Then the model will get the prediction results with length 100. The model masks 0 value, so in theory, the latter 77 of these 100 prediction results should be all 0, because they should not be predicted (being masked). However, in my experiment, the latter 77 prediction results are not 0, it seems they are also predicted and the masking has no effect. Have you met this problem before ? Or in your experiments, the latter “77” prediction results are all 0 ?
            Here is a link (https://groups.google.com/forum/#!topic/keras-users/M7BVggL7cG0) talking about the same question. Thanks.

          • Jason Brownlee October 31, 2018 at 6:26 am #

            We cannot mask predictions, only pad them.

            Perhaps your model requires further tuning.

      • White October 28, 2018 at 1:02 pm #

        I searched in google and found someone has the same question with me. (https://groups.google.com/forum/#!topic/keras-users/M7BVggL7cG0). Hope it can help you to understand my question. Many thanks.

  14. mk December 22, 2018 at 7:44 pm #

    evaluate model on new data
    X, y = generate_data(n_timesteps)
    so.in this caes ,you should know 10 data to evaluate model , but if you know the result, Why Predict?
    THIS model how to predict new data

  15. Joe Maisog January 8, 2019 at 9:06 am #

    Hi Jason, in the function generate_data(), there is a line that looks like this:

    X, y = values, values[:, 1]

    It appears that this includes the value that we want to predict, ‘y’, as the 2nd column in X. Doesn’t that make it very easy for the model to predict ‘y’ (it would simply need to pull out the value in the 2nd column of X)? Shouldn’t this line look like this instead?:

    X, y = values[:, 0], values[:, 1]

    Of course, we’d need to change the input_shape() for the first layer in the Keras model.

  16. Joe Maisog January 8, 2019 at 10:18 am #

    Here is a CMU paper that uses a modified version of LSTM called “Phased LSTM”, with various manipulations of the data.
    https://www.cs.cmu.edu/~epxing/Class/10708-17/project-reports/project8.pdf
    One of the data manipulations involves construction of a mask (Equation 1), and then adds this mask as NEW COLUMNS in the input matrix of predictor values ‘X’. This is CMU’s “PLSTM-Masking” model, (see Table 1 in the paper). This effectively DOUBLES the number of columns in the matrix X. (This may be very similar to an earlier comment you made in this thread: https://machinelearningmastery.com/handle-missing-timesteps-sequence-prediction-problems-python/#comment-424701 in response to Darius’ question. It may also be related to White’s earlier question: https://machinelearningmastery.com/handle-missing-timesteps-sequence-prediction-problems-python/#comment-452953)

    In the section of the above tutorial entitled “Masking Missing Values”, a Masking layer is added. If this were essentially doing the same thing as PLSTM-Masking in the CMU paper, I would have expected the output of the Masking layer to have double the number of columns, i.e., 4. But when I run

    model.summary()

    to inspect the shape of the output, I see that the Masking layer’s output still has only 2 columns: “(None, 2, 1)”. Am I right to infer that the Masking layer is not implementing the same masking approach as is done in the PLSTM-Masking model in the CMU paper? I think the answer is “yes”, that the Masking layer is simply “skipping over” rows in the input matrix X where all values have the masking value “-1”.

    Even though a row of missing values is skipped, does the LSTM “know” that it must still cause its memory to decay by one time step? (Values further back in time should be weighted less than more recent values.)

    • Jason Brownlee January 8, 2019 at 11:15 am #

      I believe the masked inputs are excluded from all forward/backward computation through each lstm unit.

      You could consult the Keras API/code to confirm.

  17. Mike Tauber February 12, 2019 at 4:11 am #

    Hey Jason – many thanks for the article. You mention that that masking is somewhere in between completely removing missing values/rows and Imputing/learning missing values. Could you please explain why masking is any different to just removing the values from the time series? The way I understand it so far is that: if all values in input tensor equal the mask value then that time step will be skipped and the state transferred (if stateful is true). How is this different to excluding the row from the time series?

    • Jason Brownlee February 12, 2019 at 8:07 am #

      Yes, masked values are skipped.

      But the row is not skipped if it contains sparse values.

  18. Sriram February 22, 2019 at 12:24 am #

    Hey Jason,
    Consider I have a dataset wherein there are 4 input features what if there are Nans only in 2 of the 4 input features, I don’t want the rows to be skipped and also I do not want to replace Nans with a value that is out of range. Forward filling can be done for the data points which lie in between. How to handle the missing values if they are at the beginning of the dataset without backfilling.

    • Jason Brownlee February 22, 2019 at 6:20 am #

      You fill or skip them. After that you’re out of options I think.

      • Sriram February 25, 2019 at 11:38 pm #

        Thanks, Jason

        I was wondering the same, And I do have another question wrt to the above example, if I am replacing Nans with a value that is out of range, will the model(Assuming LSTM) recognise that 2 of 4 input features are meaningless and use the other 2 input features.

        • Jason Brownlee February 26, 2019 at 6:24 am #

          It may, if you mark them with a special value or mark them as missing and use a masking layer. Try it and see.

  19. Michael April 30, 2019 at 12:23 am #

    Hello Master Brownlee, was trying to implement a mask in a multi-headed MLP, after flattening the inputs, I keep getting the error Layer dense does not support masking but was passed an input_mask…any idea on how to get over the problem? Thanks in advance

  20. Rajat May 3, 2019 at 7:44 pm #

    Hello ,
    Could you please clarify one thing
    You defined the timesteps to be 2,
    But in the code you are generating the sequences of 10 timesteps for fitting the model. What is the difference between this things

  21. Christophe June 14, 2019 at 1:53 am #

    Hi Jason,
    thank you very much for this. That’s always fantastic !!
    One question about time-series and lstm :
    I work with time-series (daily physical values from sensors from factory in fonction of time) and I have to deal with missing data. That’s not “real’ missing data, we don’t have values because factory is stopped…cleaning for example. I have long periods with no values (several days). For you, what’s the best solution to deal with that ?
    Thank you
    Christophe

  22. liang zhao August 3, 2019 at 9:34 pm #

    Hi Jason,

    I have a time series data, but there are several interrupts in the time series, and the interrupts are actually quite long, which means it is not reasonable to say a data can be forecasted by the data before an interrupt.

    If I want to train an LSTM using this data. How can I deal with those interrupts?

Leave a Reply