How to reshape data for LSTM training in multivariate sequence prediction












1












$begingroup$


I want to build an LSTM model for customer behaviour. It's the first time for me working on a timeseries, so some concepts are not clear to me at all.



My prediction problem is multidimensional, meaning that I also want to predict many informations associated to an action for each customer.



The dataset is currently shaped as a list of 2d padded arrays of one-hot encoded features (customer actions + other informations), for example:



   customer_id               encoded_features
0 25464205 [[0,1,0],..,[1,1,1],[1,0,1],..,[1,0,1]]
1 56456574 [[0,1,1],..,[1,0,1],[1,0,1],..,[1,1,1]]


where each element in the encoded_features entries represents a specific timestep.



My idea here is to use keras input shape



(n. customers, n. timesteps, length of features encoding)


In the example above it would be (2,#timesteps,3).



I have two main questions:




  1. Is this whole setting rigth for the prediction of next single customer action? I would like to simply give a new sequence of features for a certain customer and predict all features in the next timestep.


  2. I am thinking about splitting the data (according to a certain ratio) into sequential training and test sets, in order to test the trained model on unseen feature vectors. In the example above it would be:



  customer_id       X_train          y_train   
0 25464205 [[0,1,0],..] [1,1,1]
1 56456574 [[0,1,1],..] [1,0,1]

customer_id X_test y_test
0 25464205 [[1,0,1],..] [1,0,1]
1 56456574 [[1,0,1],..] [1,1,1]



Notice that X_train and X_test will generally contain all Train/Test events, except for the last one which has to be predicted.
Is this a correct interpretation?










share|improve this question









New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
    $endgroup$
    – Shamit Verma
    yesterday










  • $begingroup$
    My fault, I just edited the question. I hope it's clearer now.
    $endgroup$
    – ginevracoal
    yesterday
















1












$begingroup$


I want to build an LSTM model for customer behaviour. It's the first time for me working on a timeseries, so some concepts are not clear to me at all.



My prediction problem is multidimensional, meaning that I also want to predict many informations associated to an action for each customer.



The dataset is currently shaped as a list of 2d padded arrays of one-hot encoded features (customer actions + other informations), for example:



   customer_id               encoded_features
0 25464205 [[0,1,0],..,[1,1,1],[1,0,1],..,[1,0,1]]
1 56456574 [[0,1,1],..,[1,0,1],[1,0,1],..,[1,1,1]]


where each element in the encoded_features entries represents a specific timestep.



My idea here is to use keras input shape



(n. customers, n. timesteps, length of features encoding)


In the example above it would be (2,#timesteps,3).



I have two main questions:




  1. Is this whole setting rigth for the prediction of next single customer action? I would like to simply give a new sequence of features for a certain customer and predict all features in the next timestep.


  2. I am thinking about splitting the data (according to a certain ratio) into sequential training and test sets, in order to test the trained model on unseen feature vectors. In the example above it would be:



  customer_id       X_train          y_train   
0 25464205 [[0,1,0],..] [1,1,1]
1 56456574 [[0,1,1],..] [1,0,1]

customer_id X_test y_test
0 25464205 [[1,0,1],..] [1,0,1]
1 56456574 [[1,0,1],..] [1,1,1]



Notice that X_train and X_test will generally contain all Train/Test events, except for the last one which has to be predicted.
Is this a correct interpretation?










share|improve this question









New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
    $endgroup$
    – Shamit Verma
    yesterday










  • $begingroup$
    My fault, I just edited the question. I hope it's clearer now.
    $endgroup$
    – ginevracoal
    yesterday














1












1








1





$begingroup$


I want to build an LSTM model for customer behaviour. It's the first time for me working on a timeseries, so some concepts are not clear to me at all.



My prediction problem is multidimensional, meaning that I also want to predict many informations associated to an action for each customer.



The dataset is currently shaped as a list of 2d padded arrays of one-hot encoded features (customer actions + other informations), for example:



   customer_id               encoded_features
0 25464205 [[0,1,0],..,[1,1,1],[1,0,1],..,[1,0,1]]
1 56456574 [[0,1,1],..,[1,0,1],[1,0,1],..,[1,1,1]]


where each element in the encoded_features entries represents a specific timestep.



My idea here is to use keras input shape



(n. customers, n. timesteps, length of features encoding)


In the example above it would be (2,#timesteps,3).



I have two main questions:




  1. Is this whole setting rigth for the prediction of next single customer action? I would like to simply give a new sequence of features for a certain customer and predict all features in the next timestep.


  2. I am thinking about splitting the data (according to a certain ratio) into sequential training and test sets, in order to test the trained model on unseen feature vectors. In the example above it would be:



  customer_id       X_train          y_train   
0 25464205 [[0,1,0],..] [1,1,1]
1 56456574 [[0,1,1],..] [1,0,1]

customer_id X_test y_test
0 25464205 [[1,0,1],..] [1,0,1]
1 56456574 [[1,0,1],..] [1,1,1]



Notice that X_train and X_test will generally contain all Train/Test events, except for the last one which has to be predicted.
Is this a correct interpretation?










share|improve this question









New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I want to build an LSTM model for customer behaviour. It's the first time for me working on a timeseries, so some concepts are not clear to me at all.



My prediction problem is multidimensional, meaning that I also want to predict many informations associated to an action for each customer.



The dataset is currently shaped as a list of 2d padded arrays of one-hot encoded features (customer actions + other informations), for example:



   customer_id               encoded_features
0 25464205 [[0,1,0],..,[1,1,1],[1,0,1],..,[1,0,1]]
1 56456574 [[0,1,1],..,[1,0,1],[1,0,1],..,[1,1,1]]


where each element in the encoded_features entries represents a specific timestep.



My idea here is to use keras input shape



(n. customers, n. timesteps, length of features encoding)


In the example above it would be (2,#timesteps,3).



I have two main questions:




  1. Is this whole setting rigth for the prediction of next single customer action? I would like to simply give a new sequence of features for a certain customer and predict all features in the next timestep.


  2. I am thinking about splitting the data (according to a certain ratio) into sequential training and test sets, in order to test the trained model on unseen feature vectors. In the example above it would be:



  customer_id       X_train          y_train   
0 25464205 [[0,1,0],..] [1,1,1]
1 56456574 [[0,1,1],..] [1,0,1]

customer_id X_test y_test
0 25464205 [[1,0,1],..] [1,0,1]
1 56456574 [[1,0,1],..] [1,1,1]



Notice that X_train and X_test will generally contain all Train/Test events, except for the last one which has to be predicted.
Is this a correct interpretation?







python keras time-series lstm recurrent-neural-net






share|improve this question









New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday







ginevracoal













New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









ginevracoalginevracoal

1085




1085




New contributor




ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






ginevracoal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    $begingroup$
    Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
    $endgroup$
    – Shamit Verma
    yesterday










  • $begingroup$
    My fault, I just edited the question. I hope it's clearer now.
    $endgroup$
    – ginevracoal
    yesterday














  • 1




    $begingroup$
    Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
    $endgroup$
    – Shamit Verma
    yesterday










  • $begingroup$
    My fault, I just edited the question. I hope it's clearer now.
    $endgroup$
    – ginevracoal
    yesterday








1




1




$begingroup$
Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
$endgroup$
– Shamit Verma
yesterday




$begingroup$
Test / Train split seems odd. For training, model will see just 1 record for each customer id. Is that what you intend to do ?
$endgroup$
– Shamit Verma
yesterday












$begingroup$
My fault, I just edited the question. I hope it's clearer now.
$endgroup$
– ginevracoal
yesterday




$begingroup$
My fault, I just edited the question. I hope it's clearer now.
$endgroup$
– ginevracoal
yesterday










1 Answer
1






active

oldest

votes


















1












$begingroup$

This makes sense. It should work for input and first couple of layers. For output layers, you can have a softmax if you need to generate only next record in sequence.



Following Keras code has an example that :




  1. Accepts multi-dimensional inputs (Each sample is a Sequence of video frames)

  2. Predicts next few frames of video ( Multi dimensional since each pixel is a feature)


https://github.com/keras-team/keras/blob/master/examples/conv_lstm.py






share|improve this answer









$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "557"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    ginevracoal is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45867%2fhow-to-reshape-data-for-lstm-training-in-multivariate-sequence-prediction%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    This makes sense. It should work for input and first couple of layers. For output layers, you can have a softmax if you need to generate only next record in sequence.



    Following Keras code has an example that :




    1. Accepts multi-dimensional inputs (Each sample is a Sequence of video frames)

    2. Predicts next few frames of video ( Multi dimensional since each pixel is a feature)


    https://github.com/keras-team/keras/blob/master/examples/conv_lstm.py






    share|improve this answer









    $endgroup$


















      1












      $begingroup$

      This makes sense. It should work for input and first couple of layers. For output layers, you can have a softmax if you need to generate only next record in sequence.



      Following Keras code has an example that :




      1. Accepts multi-dimensional inputs (Each sample is a Sequence of video frames)

      2. Predicts next few frames of video ( Multi dimensional since each pixel is a feature)


      https://github.com/keras-team/keras/blob/master/examples/conv_lstm.py






      share|improve this answer









      $endgroup$
















        1












        1








        1





        $begingroup$

        This makes sense. It should work for input and first couple of layers. For output layers, you can have a softmax if you need to generate only next record in sequence.



        Following Keras code has an example that :




        1. Accepts multi-dimensional inputs (Each sample is a Sequence of video frames)

        2. Predicts next few frames of video ( Multi dimensional since each pixel is a feature)


        https://github.com/keras-team/keras/blob/master/examples/conv_lstm.py






        share|improve this answer









        $endgroup$



        This makes sense. It should work for input and first couple of layers. For output layers, you can have a softmax if you need to generate only next record in sequence.



        Following Keras code has an example that :




        1. Accepts multi-dimensional inputs (Each sample is a Sequence of video frames)

        2. Predicts next few frames of video ( Multi dimensional since each pixel is a feature)


        https://github.com/keras-team/keras/blob/master/examples/conv_lstm.py







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Shamit VermaShamit Verma

        57516




        57516






















            ginevracoal is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            ginevracoal is a new contributor. Be nice, and check out our Code of Conduct.













            ginevracoal is a new contributor. Be nice, and check out our Code of Conduct.












            ginevracoal is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Data Science Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45867%2fhow-to-reshape-data-for-lstm-training-in-multivariate-sequence-prediction%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to label and detect the document text images

            Vallis Paradisi

            Tabula Rosettana