Before getting into the code, a little bit of description of the data:

Time series with 14 features, but we are focused only on forecasting the temperature which is at index 2. Also, the given data is at 10 minute intervals:

```
["Date Time","p (mbar)","T (degC)","Tpot (K)","Tdew (degC)","rh (%)","VPmax (mbar)","VPact (mbar)","VPdef (mbar)","sh (g/kg)","H2OCmmol/mol)","rho (g/m**3)","wv (m/s)","max. wv (m/s)","wd (deg)"]

```

Next, the author parses the file and stores it as a numpy array (note: `float_data` does not contain the timestamps:

```
import numpy as np

float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
    values = [float(x) for x in line.split(',')[1:]]
    float_data[i, :] = values

```

This is followed by a normalization step which I am skipping here. So far it is simple. The following step defines a generator which yields a tuple `(samples, targets)` where `samples` is one batch of input data and `targets` is the corresponding array of target temperatures. It takes the following arguments:

1. data: The original array of floating point data, which we just normalized in the code snippet above.
2. lookback: How many timesteps back should our input data go.
3. delay: How many timesteps in the future should our target be.
4. min\_index and max\_index: Indices in the data array that delimit which timesteps to draw from. This is useful for keeping a segment of the data for validation and another one for testing.
5. shuffle: Whether to shuffle our samples or draw them in chronological order.
6. batch\_size: The number of samples per batch.
7. step: The period, in timesteps, at which we sample data. We will set it 6 in order to draw one data point every hour.

```
def generator(data, lookback, delay, min_index, max_index,
      shuffle=False, batch_size=128, step=6):

    if max_index is None:
        max_index = len(data) - delay - 1
    i = min_index + lookback
    while 1:
        if shuffle:
             rows = np.random.randint(
             min_index + lookback, max_index, size=batch_size)
        else:
            if i + batch_size >= max_index:
                i = min_index + lookback
        rows = np.arange(i, min(i + batch_size, max_index))
        i += len(rows)

        samples = np.zeros((len(rows),
                   lookback // step,
                   data.shape[-1]))
        targets = np.zeros((len(rows),))
        for j, row in enumerate(rows):
            indices = range(rows[j] - lookback, rows[j], step)
            samples[j] = data[indices]
            targets[j] = data[rows[j] + delay][1]
        yield samples, targets

 lookback = 1440  # Basically, going back 10 days
 step = 6         # Taking an hourly sample     
 delay = 144      # Want to forecast temperature for the next 24 hours
 batch_size = 128

 train_gen = generator(float_data,
              lookback=lookback,
              delay=delay,
              min_index=0,
              max_index=200000,
              shuffle=True,
              step=step,
              batch_size=batch_size)

 val_gen = generator(float_data,
            lookback=lookback,
            delay=delay,
            min_index=200001,
            max_index=300000,
            step=step,
            batch_size=batch_size)

```

Now, I understand what the code does. Please correct me if I am wrong. It is going to generate input data in the following format:

Each batch will have 128 sequences where each sequence will be 240 long. For example:

Batch 0: [[0-239],[1-240],[2-241],...[127-366]]

Batch 1: [[128-367],...,.... ]

After this, the author trains a simple model:

```
from keras.models import Sequential
from keras import layers
from keras.optimizers import RMSprop

model = Sequential()
model.add(layers.Flatten(input_shape=(lookback // step,   float_data.shape[-1])))
model.add(layers.Dense(32, activation='relu'))
model.add(layers.Dense(1))

model.compile(optimizer=RMSprop(), loss='mae')
history = model.fit_generator(train_gen,
                          steps_per_epoch=500,
                          epochs=20,
                          validation_data=val_gen,
                          validation_steps=val_steps)

```

**My questions**

First, I don't understand why is the data being generated in this fashion and fed into the neural network. In other words, why can't the data look like the following?:

Batch 0: [[0-240],[241-479],...........]

Second, in `train_gen` why is the `shuffle` parameter set to `True`? It will cause the data to lose the temporal ordering.