Keras time series can I predict next 6 month in one time










0














I use keras for time series prediction. My code can predict next 6 months by predict next one month and then get it to be input for predict next month again untill complete 6 months. That means predict one month 6 times. Can I predict next 6 month in one time.



import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import LSTM
from pandas.tseries.offsets import MonthEnd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.backend as K
from keras.layers import Bidirectional
from keras.layers import Embedding
from keras.layers import GRU

df = pd.read_csv('D://data.csv',
engine='python')

df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
df = df.set_index('DATE_')
df.head()

split_date = pd.Timestamp('03-01-2015')

train = df.loc[:split_date, ['data']]
test = df.loc[split_date:, ['data']]
sc = MinMaxScaler()

train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)

X_train = train_sc[:-1]
y_train = train_sc[1:]

X_test = test_sc[:-1]
y_test = test_sc[1:]

K.clear_session()
model = Sequential()
model.add(Dense(12, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()

model.fit(X_train, y_train, epochs=200, batch_size=2)

y_pred = model.predict(X_test)

real_pred = sc.inverse_transform(y_pred)
real_test = sc.inverse_transform(y_test)

print("Predict Value")
print(real_pred)

print("Test Value")
print(real_test)









share|improve this question




























    0














    I use keras for time series prediction. My code can predict next 6 months by predict next one month and then get it to be input for predict next month again untill complete 6 months. That means predict one month 6 times. Can I predict next 6 month in one time.



    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from keras.layers import LSTM
    from pandas.tseries.offsets import MonthEnd
    from sklearn.preprocessing import MinMaxScaler
    from keras.models import Sequential
    from keras.layers import Dense, Dropout
    import keras.backend as K
    from keras.layers import Bidirectional
    from keras.layers import Embedding
    from keras.layers import GRU

    df = pd.read_csv('D://data.csv',
    engine='python')

    df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
    df = df.set_index('DATE_')
    df.head()

    split_date = pd.Timestamp('03-01-2015')

    train = df.loc[:split_date, ['data']]
    test = df.loc[split_date:, ['data']]
    sc = MinMaxScaler()

    train_sc = sc.fit_transform(train)
    test_sc = sc.transform(test)

    X_train = train_sc[:-1]
    y_train = train_sc[1:]

    X_test = test_sc[:-1]
    y_test = test_sc[1:]

    K.clear_session()
    model = Sequential()
    model.add(Dense(12, input_dim=1, activation='relu'))
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.summary()

    model.fit(X_train, y_train, epochs=200, batch_size=2)

    y_pred = model.predict(X_test)

    real_pred = sc.inverse_transform(y_pred)
    real_test = sc.inverse_transform(y_test)

    print("Predict Value")
    print(real_pred)

    print("Test Value")
    print(real_test)









    share|improve this question


























      0












      0








      0







      I use keras for time series prediction. My code can predict next 6 months by predict next one month and then get it to be input for predict next month again untill complete 6 months. That means predict one month 6 times. Can I predict next 6 month in one time.



      import pandas as pd
      import numpy as np
      import matplotlib.pyplot as plt
      from keras.layers import LSTM
      from pandas.tseries.offsets import MonthEnd
      from sklearn.preprocessing import MinMaxScaler
      from keras.models import Sequential
      from keras.layers import Dense, Dropout
      import keras.backend as K
      from keras.layers import Bidirectional
      from keras.layers import Embedding
      from keras.layers import GRU

      df = pd.read_csv('D://data.csv',
      engine='python')

      df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
      df = df.set_index('DATE_')
      df.head()

      split_date = pd.Timestamp('03-01-2015')

      train = df.loc[:split_date, ['data']]
      test = df.loc[split_date:, ['data']]
      sc = MinMaxScaler()

      train_sc = sc.fit_transform(train)
      test_sc = sc.transform(test)

      X_train = train_sc[:-1]
      y_train = train_sc[1:]

      X_test = test_sc[:-1]
      y_test = test_sc[1:]

      K.clear_session()
      model = Sequential()
      model.add(Dense(12, input_dim=1, activation='relu'))
      model.add(Dense(1))
      model.compile(loss='mean_squared_error', optimizer='adam')
      model.summary()

      model.fit(X_train, y_train, epochs=200, batch_size=2)

      y_pred = model.predict(X_test)

      real_pred = sc.inverse_transform(y_pred)
      real_test = sc.inverse_transform(y_test)

      print("Predict Value")
      print(real_pred)

      print("Test Value")
      print(real_test)









      share|improve this question















      I use keras for time series prediction. My code can predict next 6 months by predict next one month and then get it to be input for predict next month again untill complete 6 months. That means predict one month 6 times. Can I predict next 6 month in one time.



      import pandas as pd
      import numpy as np
      import matplotlib.pyplot as plt
      from keras.layers import LSTM
      from pandas.tseries.offsets import MonthEnd
      from sklearn.preprocessing import MinMaxScaler
      from keras.models import Sequential
      from keras.layers import Dense, Dropout
      import keras.backend as K
      from keras.layers import Bidirectional
      from keras.layers import Embedding
      from keras.layers import GRU

      df = pd.read_csv('D://data.csv',
      engine='python')

      df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
      df = df.set_index('DATE_')
      df.head()

      split_date = pd.Timestamp('03-01-2015')

      train = df.loc[:split_date, ['data']]
      test = df.loc[split_date:, ['data']]
      sc = MinMaxScaler()

      train_sc = sc.fit_transform(train)
      test_sc = sc.transform(test)

      X_train = train_sc[:-1]
      y_train = train_sc[1:]

      X_test = test_sc[:-1]
      y_test = test_sc[1:]

      K.clear_session()
      model = Sequential()
      model.add(Dense(12, input_dim=1, activation='relu'))
      model.add(Dense(1))
      model.compile(loss='mean_squared_error', optimizer='adam')
      model.summary()

      model.fit(X_train, y_train, epochs=200, batch_size=2)

      y_pred = model.predict(X_test)

      real_pred = sc.inverse_transform(y_pred)
      real_test = sc.inverse_transform(y_test)

      print("Predict Value")
      print(real_pred)

      print("Test Value")
      print(real_test)






      python machine-learning keras lstm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 16:21

























      asked Nov 11 at 19:02









      user58519

      224




      224






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).



          Best of luck.






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252152%2fkeras-time-series-can-i-predict-next-6-month-in-one-time%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









            0














            Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).



            Best of luck.






            share|improve this answer

























              0














              Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).



              Best of luck.






              share|improve this answer























                0












                0








                0






                Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).



                Best of luck.






                share|improve this answer












                Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).



                Best of luck.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 11 at 20:33









                Christian Sloper

                1,083213




                1,083213



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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.

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





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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.

                    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%2fstackoverflow.com%2fquestions%2f53252152%2fkeras-time-series-can-i-predict-next-6-month-in-one-time%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

                    Kleinkühnau

                    Makov (Slowakei)

                    Deutsches Schauspielhaus