title: Time series data preparation for LSTM classification
body: <p>I am trying to fit simple LSTM model to perform binary classification on multivariate time series data. I am confused with the time series data preparation steps to fed into the model. Most of the online materials covered data preparation for prediction(regression problem) using LSTM. For my case, I am trying to prepare data for classification using LSTM. How to make sure that I am not messing with the temporal sequence and class balance while performing train and test split?  My class labels are highly imbalanced. Is my code correct for LSTM classification? I appreciate your time. Thanks!</p>
  <p>Sample data:</p>
  <p><a href="https://i.stack.imgur.com/I1HQN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I1HQN.png" alt="enter image description here" /></a></p>
  <pre><code>sequence_length = 10
  def generate_data(X, y, sequence_length = 10, step = 1):
      X_local = []
      y_local = []
      for start in range(0, len(data) - sequence_length, step):
          end = start + sequence_length
          X_local.append(X[start:end])
          y_local.append(y[end-1])
      return np.array(X_local), np.array(y_local)
  
  X_sequence, y = generate_data(data.loc[:, &quot;V1&quot;:&quot;V4&quot;].values, data.Class)
  </code></pre>
  <p>Shape of the sequence:</p>
  <pre><code>X_sequence.shape, y.shape
  ((12237889, 10, 4), (12237889,))
  </code></pre>
  <p>Training and test data split:</p>
  <pre><code>training_size = int(len(X_sequence) * 0.7)
  X_train, y_train = X_sequence[:training_size], y[:training_size]
  X_test, y_test = X_sequence[training_size:], y[training_size:]
  </code></pre>
  <p>Shape of the training and test data:</p>
  <pre><code>X_train.shape, X_test.shape
  ((8566522, 10, 4), (3671367, 10, 4))
  </code></pre>
  <pre><code>y_train.shape, y_test.shape
  ((8566522,), (3671367,))
  </code></pre>