How to feed a table per timestamp to LSTM neural network?












1












$begingroup$


I have a time-series dataframe like this



         feat1  feat2  target
date id
0 1 12 16 192
2 15 6 90
3 2 9 18
1 1 0 3 0
2 0 9 0
3 56 9 504
2 1 5 9 45
2 6 9 54
3 5 8 40


and my problem is for regression.



What I know about the LSTM sequences, is that normally, the row_id is the date, so you build sequences of n rows.



In my data, as you can see, it's different. In each row that refers to the date, I have 3 more rows representing a product.



What I have thinked for make the sequence to my problem, my sequence will be of 2 days:



sequence = [day0,day1],[day1,day2]


For every date, I have



date0 = [id1,id2,id3].


For every id, I have:



id = [feat1,feat2].


the sequence[0], and it would be something like



[
[
[12,16],
[15,6],
[2,9]
],[
[0,3],
[0.9],
[56,9]
]
]


Is this valid?



Will the LSTM layer understand this? Or do I have to do some kind of extra transformation?










share|improve this question









New contributor




Extermis 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$


    I have a time-series dataframe like this



             feat1  feat2  target
    date id
    0 1 12 16 192
    2 15 6 90
    3 2 9 18
    1 1 0 3 0
    2 0 9 0
    3 56 9 504
    2 1 5 9 45
    2 6 9 54
    3 5 8 40


    and my problem is for regression.



    What I know about the LSTM sequences, is that normally, the row_id is the date, so you build sequences of n rows.



    In my data, as you can see, it's different. In each row that refers to the date, I have 3 more rows representing a product.



    What I have thinked for make the sequence to my problem, my sequence will be of 2 days:



    sequence = [day0,day1],[day1,day2]


    For every date, I have



    date0 = [id1,id2,id3].


    For every id, I have:



    id = [feat1,feat2].


    the sequence[0], and it would be something like



    [
    [
    [12,16],
    [15,6],
    [2,9]
    ],[
    [0,3],
    [0.9],
    [56,9]
    ]
    ]


    Is this valid?



    Will the LSTM layer understand this? Or do I have to do some kind of extra transformation?










    share|improve this question









    New contributor




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







    $endgroup$















      1












      1








      1





      $begingroup$


      I have a time-series dataframe like this



               feat1  feat2  target
      date id
      0 1 12 16 192
      2 15 6 90
      3 2 9 18
      1 1 0 3 0
      2 0 9 0
      3 56 9 504
      2 1 5 9 45
      2 6 9 54
      3 5 8 40


      and my problem is for regression.



      What I know about the LSTM sequences, is that normally, the row_id is the date, so you build sequences of n rows.



      In my data, as you can see, it's different. In each row that refers to the date, I have 3 more rows representing a product.



      What I have thinked for make the sequence to my problem, my sequence will be of 2 days:



      sequence = [day0,day1],[day1,day2]


      For every date, I have



      date0 = [id1,id2,id3].


      For every id, I have:



      id = [feat1,feat2].


      the sequence[0], and it would be something like



      [
      [
      [12,16],
      [15,6],
      [2,9]
      ],[
      [0,3],
      [0.9],
      [56,9]
      ]
      ]


      Is this valid?



      Will the LSTM layer understand this? Or do I have to do some kind of extra transformation?










      share|improve this question









      New contributor




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







      $endgroup$




      I have a time-series dataframe like this



               feat1  feat2  target
      date id
      0 1 12 16 192
      2 15 6 90
      3 2 9 18
      1 1 0 3 0
      2 0 9 0
      3 56 9 504
      2 1 5 9 45
      2 6 9 54
      3 5 8 40


      and my problem is for regression.



      What I know about the LSTM sequences, is that normally, the row_id is the date, so you build sequences of n rows.



      In my data, as you can see, it's different. In each row that refers to the date, I have 3 more rows representing a product.



      What I have thinked for make the sequence to my problem, my sequence will be of 2 days:



      sequence = [day0,day1],[day1,day2]


      For every date, I have



      date0 = [id1,id2,id3].


      For every id, I have:



      id = [feat1,feat2].


      the sequence[0], and it would be something like



      [
      [
      [12,16],
      [15,6],
      [2,9]
      ],[
      [0,3],
      [0.9],
      [56,9]
      ]
      ]


      Is this valid?



      Will the LSTM layer understand this? Or do I have to do some kind of extra transformation?







      neural-network time-series lstm recurrent-neural-net






      share|improve this question









      New contributor




      Extermis 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




      Extermis 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 14 hours ago









      Esmailian

      3,311420




      3,311420






      New contributor




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









      asked 2 days ago









      ExtermisExtermis

      83




      83




      New contributor




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





      New contributor





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






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






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          Your data format is



                                   feature1   feature2   target
          product1 1 12 2
          timestamp product2 2 6 3
          product3 4 3 4


          There are two designs for two assumptions:




          1. Products are not related to each other. Therefore, each product could be modeled separately. That is, each timestamp is X(t) = [feature1, feature2] or, including the target, X(t)|y(t) = [feature1, feature2, target]. And we build a model for each product separately. In summary, LSTM receives two 1 x 3 sequences for t-1 and t, and outputs a 1 x 1 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 3}, overbrace{X_{t}|y_{t}}^{1 times 3}) rightarrow overbrace{y_{t+1}}^{1 times 1}$$



          2. Products are related to each other, meaning product1 can help product2 to predict its target. For this, we just need to flatten the 3 x 2 matrix to a 1 x 6 vector, where the order of values does not matter. That is,



            X(t) = [product1_feature1, product1_feature2, ..., product3_feature2]


            or



            X(t) = [product1_feature1, product2_feature1, ..., product3_feature2]


            We can also add the targets, for example



            X(t)|y(t) = [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3]


            This way, dimension of each timestamp would be 9 (6 + 3), and a sequence of two timestamps would be



            [
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3], # t-1
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3] # t
            ]


            corresponding to a three dimensional target [target1, ..., target3] at t + 1.



            In summary, LSTM receives two 1 x 9 sequences for t-1 and t, and outputs a 1 x 3 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 9}, overbrace{X_{t}|y_{t}}^{1 times 9}) rightarrow overbrace{y_{t+1}}^{1 times 3}$$








          share|improve this answer











          $endgroup$













          • $begingroup$
            But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
            $endgroup$
            – Extermis
            2 days ago












          • $begingroup$
            @Extermis my bad! updated.
            $endgroup$
            – Esmailian
            2 days ago












          Your Answer








          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
          });


          }
          });






          Extermis 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%2f49236%2fhow-to-feed-a-table-per-timestamp-to-lstm-neural-network%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$

          Your data format is



                                   feature1   feature2   target
          product1 1 12 2
          timestamp product2 2 6 3
          product3 4 3 4


          There are two designs for two assumptions:




          1. Products are not related to each other. Therefore, each product could be modeled separately. That is, each timestamp is X(t) = [feature1, feature2] or, including the target, X(t)|y(t) = [feature1, feature2, target]. And we build a model for each product separately. In summary, LSTM receives two 1 x 3 sequences for t-1 and t, and outputs a 1 x 1 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 3}, overbrace{X_{t}|y_{t}}^{1 times 3}) rightarrow overbrace{y_{t+1}}^{1 times 1}$$



          2. Products are related to each other, meaning product1 can help product2 to predict its target. For this, we just need to flatten the 3 x 2 matrix to a 1 x 6 vector, where the order of values does not matter. That is,



            X(t) = [product1_feature1, product1_feature2, ..., product3_feature2]


            or



            X(t) = [product1_feature1, product2_feature1, ..., product3_feature2]


            We can also add the targets, for example



            X(t)|y(t) = [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3]


            This way, dimension of each timestamp would be 9 (6 + 3), and a sequence of two timestamps would be



            [
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3], # t-1
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3] # t
            ]


            corresponding to a three dimensional target [target1, ..., target3] at t + 1.



            In summary, LSTM receives two 1 x 9 sequences for t-1 and t, and outputs a 1 x 3 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 9}, overbrace{X_{t}|y_{t}}^{1 times 9}) rightarrow overbrace{y_{t+1}}^{1 times 3}$$








          share|improve this answer











          $endgroup$













          • $begingroup$
            But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
            $endgroup$
            – Extermis
            2 days ago












          • $begingroup$
            @Extermis my bad! updated.
            $endgroup$
            – Esmailian
            2 days ago
















          1












          $begingroup$

          Your data format is



                                   feature1   feature2   target
          product1 1 12 2
          timestamp product2 2 6 3
          product3 4 3 4


          There are two designs for two assumptions:




          1. Products are not related to each other. Therefore, each product could be modeled separately. That is, each timestamp is X(t) = [feature1, feature2] or, including the target, X(t)|y(t) = [feature1, feature2, target]. And we build a model for each product separately. In summary, LSTM receives two 1 x 3 sequences for t-1 and t, and outputs a 1 x 1 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 3}, overbrace{X_{t}|y_{t}}^{1 times 3}) rightarrow overbrace{y_{t+1}}^{1 times 1}$$



          2. Products are related to each other, meaning product1 can help product2 to predict its target. For this, we just need to flatten the 3 x 2 matrix to a 1 x 6 vector, where the order of values does not matter. That is,



            X(t) = [product1_feature1, product1_feature2, ..., product3_feature2]


            or



            X(t) = [product1_feature1, product2_feature1, ..., product3_feature2]


            We can also add the targets, for example



            X(t)|y(t) = [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3]


            This way, dimension of each timestamp would be 9 (6 + 3), and a sequence of two timestamps would be



            [
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3], # t-1
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3] # t
            ]


            corresponding to a three dimensional target [target1, ..., target3] at t + 1.



            In summary, LSTM receives two 1 x 9 sequences for t-1 and t, and outputs a 1 x 3 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 9}, overbrace{X_{t}|y_{t}}^{1 times 9}) rightarrow overbrace{y_{t+1}}^{1 times 3}$$








          share|improve this answer











          $endgroup$













          • $begingroup$
            But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
            $endgroup$
            – Extermis
            2 days ago












          • $begingroup$
            @Extermis my bad! updated.
            $endgroup$
            – Esmailian
            2 days ago














          1












          1








          1





          $begingroup$

          Your data format is



                                   feature1   feature2   target
          product1 1 12 2
          timestamp product2 2 6 3
          product3 4 3 4


          There are two designs for two assumptions:




          1. Products are not related to each other. Therefore, each product could be modeled separately. That is, each timestamp is X(t) = [feature1, feature2] or, including the target, X(t)|y(t) = [feature1, feature2, target]. And we build a model for each product separately. In summary, LSTM receives two 1 x 3 sequences for t-1 and t, and outputs a 1 x 1 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 3}, overbrace{X_{t}|y_{t}}^{1 times 3}) rightarrow overbrace{y_{t+1}}^{1 times 1}$$



          2. Products are related to each other, meaning product1 can help product2 to predict its target. For this, we just need to flatten the 3 x 2 matrix to a 1 x 6 vector, where the order of values does not matter. That is,



            X(t) = [product1_feature1, product1_feature2, ..., product3_feature2]


            or



            X(t) = [product1_feature1, product2_feature1, ..., product3_feature2]


            We can also add the targets, for example



            X(t)|y(t) = [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3]


            This way, dimension of each timestamp would be 9 (6 + 3), and a sequence of two timestamps would be



            [
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3], # t-1
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3] # t
            ]


            corresponding to a three dimensional target [target1, ..., target3] at t + 1.



            In summary, LSTM receives two 1 x 9 sequences for t-1 and t, and outputs a 1 x 3 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 9}, overbrace{X_{t}|y_{t}}^{1 times 9}) rightarrow overbrace{y_{t+1}}^{1 times 3}$$








          share|improve this answer











          $endgroup$



          Your data format is



                                   feature1   feature2   target
          product1 1 12 2
          timestamp product2 2 6 3
          product3 4 3 4


          There are two designs for two assumptions:




          1. Products are not related to each other. Therefore, each product could be modeled separately. That is, each timestamp is X(t) = [feature1, feature2] or, including the target, X(t)|y(t) = [feature1, feature2, target]. And we build a model for each product separately. In summary, LSTM receives two 1 x 3 sequences for t-1 and t, and outputs a 1 x 1 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 3}, overbrace{X_{t}|y_{t}}^{1 times 3}) rightarrow overbrace{y_{t+1}}^{1 times 1}$$



          2. Products are related to each other, meaning product1 can help product2 to predict its target. For this, we just need to flatten the 3 x 2 matrix to a 1 x 6 vector, where the order of values does not matter. That is,



            X(t) = [product1_feature1, product1_feature2, ..., product3_feature2]


            or



            X(t) = [product1_feature1, product2_feature1, ..., product3_feature2]


            We can also add the targets, for example



            X(t)|y(t) = [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3]


            This way, dimension of each timestamp would be 9 (6 + 3), and a sequence of two timestamps would be



            [
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3], # t-1
            [product1_feature1, product2_feature1, ..., product3_feature2, target1, ..., target3] # t
            ]


            corresponding to a three dimensional target [target1, ..., target3] at t + 1.



            In summary, LSTM receives two 1 x 9 sequences for t-1 and t, and outputs a 1 x 3 target for t + 1. In notation:
            $$(overbrace{X_{t-1}|y_{t-1}}^{1 times 9}, overbrace{X_{t}|y_{t}}^{1 times 9}) rightarrow overbrace{y_{t+1}}^{1 times 3}$$









          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          EsmailianEsmailian

          3,311420




          3,311420












          • $begingroup$
            But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
            $endgroup$
            – Extermis
            2 days ago












          • $begingroup$
            @Extermis my bad! updated.
            $endgroup$
            – Esmailian
            2 days ago


















          • $begingroup$
            But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
            $endgroup$
            – Extermis
            2 days ago












          • $begingroup$
            @Extermis my bad! updated.
            $endgroup$
            – Esmailian
            2 days ago
















          $begingroup$
          But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
          $endgroup$
          – Extermis
          2 days ago






          $begingroup$
          But I have 3 target on fact. [product1_feature1, product1_feature2,..., product3_feature2, target1,target2,target3] # each t
          $endgroup$
          – Extermis
          2 days ago














          $begingroup$
          @Extermis my bad! updated.
          $endgroup$
          – Esmailian
          2 days ago




          $begingroup$
          @Extermis my bad! updated.
          $endgroup$
          – Esmailian
          2 days ago










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










          draft saved

          draft discarded


















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













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












          Extermis 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%2f49236%2fhow-to-feed-a-table-per-timestamp-to-lstm-neural-network%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

          Callistus I

          Tabula Rosettana

          How to label and detect the document text images