-I want to make a python program using the LSTM model to predict an output value that is 1 or 0.

-My data is stored in a .csv file of the form: (Example of the line)

Date time temperature wind value-output

10-02-2020 10:00 25 10 1

-I found several courses, several examples of LSTM but I don't find my classification problem to do the same thing, there are many examples on translation.

-I am stuck on how to prepare my program my data to give them to the LSTM model.

-I want to take into consideration my temperature and wind inputs in addition to the time to predict the output value.

  • (I have already made a python program based on a simple ANN to predict my output value by following a tutorial), but for the LSTM I find it difficult.

Thanks in advance for your help.


1 Answer 1


You have to prepare your data as a numpy array with the following shape:

( Number of observations , Input length , Number of variables )

Assuming you are working with Keras, the input of the LSTM() layer is as above, but you don't need to report the number of observations: input_shape = (Input length , Number of variables). Input length is an hyperparameter of your choice.

I pushed this Notebook on GitHub that contains a function to preprocess your dataset for RNNs:

def univariate_processing(variable, window):
    ''' RNN preprocessing for single variables.
    Can be iterated for multidimensional datasets. '''
    import numpy as np

    # create empty 2D matrix from variable
    V = np.empty((len(variable)-window+1, window))

    # take each row/time window
    for i in range(V.shape[0]):
        V[i,:] = variable[i : i+window]

    V = V.astype(np.float32) # set common data type

    return V

def RNN_regprep(df, y, len_input, len_pred): #, test_size):
    RNN preprocessing for multivariate regression. Builds multidimensional 
    dataset by iterating univariate preprocessing steps.
    Requires univariate_processing() function.
    Args:  df, y:                X and y data in numpy.array() format
           len_input, len_pred:  length of input and prediction sequences
    Returns:  X, Y matrices
    import numpy as np

    # create 3D matrix for multivariate input
    X = np.empty((df.shape[0]-len_input+1, len_input, df.shape[1]))

    # Iterate univariate preprocessing on all variables - store them in XM
    for i in range(df.shape[1]):
        X[ : , : , i ] = univariate_processing(df[:,i], len_input)

    # create 2D matrix of y sequences
    y = y.reshape((-1,))  # reshape to 1D if needed
    Y = univariate_processing(y, len_pred)

    ## Trim dataframes as explained
    X = X[ :-(len_pred + 1) , : , : ]
    Y = Y[len_input:-1 , :]

    # Set common datatype
    X = X.astype(np.float32)
    Y = Y.astype(np.float32)

    return X, Y

Let me know if that's what you were looking for.

  • $\begingroup$ Hello, I followed this example machinelearningmastery.com/… because I have the same number of input only the output that I have worth (0 or 1) in output $\endgroup$
    – ami2284
    Apr 8, 2020 at 18:48
  • $\begingroup$ Ok, then you can keep the processing function for X, and write your own processing function for Y. You need one-hot encoding if your output layer has 2 nodes + softmax activation, or a dummy variable if you output layer has 1 node + sigmoid activation. $\endgroup$
    – Leevo
    Apr 8, 2020 at 21:01
  • $\begingroup$ I make the changes my results are: rmse = sqrt (mean_squared_error (inv_y, inv_yhat)) print (‘RMSE Test:% .3f’% rmse) RMSE test: 0.090 and scores = model.evaluate (test_X, test_y, verbose = 0) print (“Accuracy:% .2f %%”% (scores [1] * 100)) Accuracy: 99.19% Is my model good ??? how evaluate my model with each valors? if i want to compare with an other moel $\endgroup$
    – ami2284
    Apr 9, 2020 at 21:17
  • 1
    $\begingroup$ Please remember to always assess the quality of your model on TEST data. If that 99.19% accuracy is achieved on the Train set we can't really say, it could be good or it could be overfitting. Also, whether some percentage is good or not depends strongly on the specific domain, unfortunately I have no information on your task. Can you ask some domain expert if that's good or not, or maybe compare your result with state-of-the-art applications? $\endgroup$
    – Leevo
    Apr 9, 2020 at 21:28
  • $\begingroup$ Yes you evaluate on test data; what is the difference between Train and Test accuracies? Remember to always compare models performance on Test data. I'm sorry I don't know about ANFIS. $\endgroup$
    – Leevo
    Apr 9, 2020 at 22:03

Your Answer

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

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