I'm trying to predict the Pollution using a Multivariate and Multi-step LSTM code, I've been following this tutorial.

I've been following the code until the end, but couldn't understand where the code writer determined the Pollution column to be the code output (predicting the Pollution column instead of an other column?) I'm new to python, and it got me confused.

At first, I thought that this is the part of the code where we define it, but I was wrong:

# invert scaling for forecast
pred_scaler = MinMaxScaler(feature_range=(0, 1)).fit(dataset.values[:,0].reshape(-1, 1)) 
inv_yhat = pred_scaler.inverse_transform(yhat)
# invert scaling for actual
inv_y = pred_scaler.inverse_transform(test_y)

Any explanations on how he determined the pollution column out of the 7 other columns to be the output?


1 Answer 1


I know this tutorial, it's a good start for RNNs but it contains a lot of passages and transformations that could have been kept shorter.

First, he defines a function called series_to_supervised() to process data to be fed into an RNN. Paragraph 3, line 37:

reframed = series_to_supervised(scaled, 1, 1)

This reframed dataframe contains all data, either y columns and all the X variables to make a prediction.

In the following code block it is turned into a numpy array at line 2:

values = reframed.values

Ok, so now all our information is store in values. Now it's time to separate it in train and test:

train = values[:n_train_hours, :]
test = values[n_train_hours:, :]

And again, each train and test is separated in x and y pieces:

# split into input and outputs
train_X, train_y = train[:, :-1], train[:, -1]
test_X, test_y = test[:, :-1], test[:, -1]

Line 7-8. This is where the dependent variable is separated from the rest. Knowing it was the last column, it was extracted with index -1 (i.e. the last element).

As I said, this tutorial is a good start to learn time series prediction with RNNs. However, I find that sometimes he tried to simplify the steps so much... that he ended up with some messy parts. All those objects: reframed, values, train, test, ... there was no need to make so many of them.

That apart, I'm a fan of the blog. It provided a lot of useful tips on RNNs.

  • 2
    $\begingroup$ If you are interested, I pushed a tutorial Notebook on the same task, but with things implemented in a different way, with my own code. Hope that helps. $\endgroup$
    – Leevo
    Commented Jun 15, 2020 at 17:10
  • 1
    $\begingroup$ thank you, great tutorials and link you have given here. I can't understand why you only have only one thumb up here. Anyway, if you have time, would you please also make one LSTM Multivariate Many to Many tutorial ? And particularly, please include differencing, scaling, then reverting them to the tutorial. Because, i have been searching for so long and still can't find one. And then i have tried so hard to figure it out myself, but fail again. Please do let me know when you have one. I appreciate it. $\endgroup$
    – yts61
    Commented Jan 23, 2021 at 20:25
  • 1
    $\begingroup$ i just followed you in githhub $\endgroup$
    – yts61
    Commented Jan 23, 2021 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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