I need to implement a deep learning algorithm to predict an ordinal value, called 'Entity', using longitudinal health records data. I read a few articles and guides but I couldn't find a clear explanation or example on how to organize input data; the only thing that I've understood is that I need to use an LSTM node which is designed exactly for this kind of problem. To clarify let me bring an example, let's say that I have this table:

ID N1 C1 B1 Out
2 25 3 0 1
2 32 4 1 2
2 52 1 0 1
3 12 2 1 2
3 56 4 0 1
3 73 3 1 3
3 12 2 1 2

The table contains variables of different kinds like numerical, cardinal, and boolean; I need to teach my algorithm to predict the future output with longitudinal data (for example if I input the first two rows with ID=2 the output should be 1 which is in the third row with ID=2).
The rows with the same ID are different data from the same person, checked at different times.
I've understood that LSTM are layers useful when there is the need to catch time relations and the input is in shape (samples, timesteps, features); given that i have few doubts about how to treat and model the training data:

  1. My dataset is not homogeneous in the timesteps dimension (persons may have different number of rows), but I think it's ok to have tensors with different timesteps length, is that correct?
  2. if the answer at the previous question is yes, can i train my network with tensors of growing dimension of the same patient? For istance(notice that i want to predict the future Out):
train1 x:[25, 3, 0] -> label:2;  
train2 x:[[25, 3, 0],[32, 4, 1]] -> label:1;

BONUS) Since is a complicated problem I was even wondering if I am looking at it in the wrong way and I should treat it like a kinda-regression problem like the one in that example https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/

To give a different perspective of the problem let's say doctors classify Alice by looking at the current situation and at previous visits, I would like to implement this behaviour in an algorithm so that when I have Bob current situation and clinical history I can make a prediction for him too based on the "rules" that i learned from Alice examples. The alternative behaviour, that I expressed in the BONUS question, is if I want to reproduce the learning pattern of a Doctor which look at a graph like the one in the figure below and learn how to predict the trend of the y value in another patient based on the "rules" learned in Alice's graph. enter image description here I'm implementing that on Python using Keras, but I am stuck in this preprocessing phase and I can't proceed.

  • $\begingroup$ What are you trying to achieve? What do you want to predict? Do you want to predict a fact about a single person? Is there any reason to think that there is any sequential ordering to the people, i.e., information about past people will help you make predictions about a new person, and that the order of the data for past people (the order in which past people appear in the data) is critical to making good predictions? What does "different timesteps length" mean? $\endgroup$
    – D.W.
    Apr 17, 2021 at 19:22
  • $\begingroup$ I have removed the same question from computer science. I want to predict the Output value given a variable number of input where the variable number depends on the number of visit that the patient made, the sequential order is given by the fact that sequential rows are sequential medical follow-up and, if the ID is the same, the data is from the same person. To put it in simple words i would like an algorithm that can learn how to predict the Output (that is cardinal, can assume 3 values) given a series of medical exams of the same person (which may be a variable number of visits). $\endgroup$
    – francesco
    Apr 17, 2021 at 20:50
  • $\begingroup$ Rather than adding clarifications in the comments, please edit the question to have all information necessary to answer it, and to read well for someone who encounters it for the first time. I know you want to predict an output from an input, but that's not what I was asking. It's not clear why a sequence of medical records from Alice, Bob, and Charlie are useful for predicting the next record for Dave. I can see how prior records for Dave might be helpful, and how the order of Dave's records is important, but is the order of past patients (that Alice visited before Bob) relevant? $\endgroup$
    – D.W.
    Apr 17, 2021 at 21:31
  • $\begingroup$ I have added informations to make my questions more clear, hope it helps to better understand my problem. $\endgroup$
    – francesco
    Apr 17, 2021 at 22:47

1 Answer 1


Use a LSTM, where the input is a sequence, specifically the sequence of records for that patient. You can train the LSTM to predict anything you want -- e.g., the next step. In LSTMs, the sequence normally is fixed-length (the same length for all people; you pad shorter sequences); each element of the sequence is a feature vector of same length (the same length for each element in the sequence.) Each person in your dataset gives you at least one training instance for the LSTM.

It's beyond the scope of this question how to encode inputs, but you can read about standard methods; it's not specific to a LSTM. For instance, you can encode categorical variables with a one-hot vector, and continuous variables directly. Similarly for outputs, but you'll need to choose an appropriate loss function (e.g., cross-entropy loss for categorical variables, MSE for continuous values, etc.). None of that is specific to LSTMs, so read up on how that is done with neural networks and the same methods will apply.

You'll probably need a lot of data to get the LSTM to work well.

  • $\begingroup$ Thank you for the response, i am still unsure about the fixed length since, as i said in question 1, i thought that at least one axis could be of size = None. is that correct? $\endgroup$
    – francesco
    Apr 18, 2021 at 10:06
  • $\begingroup$ @francesco, I don't understand what you are asking. Sorry. $\endgroup$
    – D.W.
    Apr 18, 2021 at 15:23

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.