{"id":1756,"date":"2018-01-05T00:22:14","date_gmt":"2018-01-05T00:22:14","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?page_id=1756"},"modified":"2018-04-17T23:33:08","modified_gmt":"2018-04-17T23:33:08","slug":"time-series-prediction-lstm-keras-python-source-code","status":"publish","type":"page","link":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/","title":{"rendered":"Time Series Prediction with LSTM and Keras  &#8211; Python Source Code"},"content":{"rendered":"<p>Here you can find python source code for <a href=http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/27\/time-series-prediction-lstm-keras\/  target=\"_blank\">Time Series Prediction with LSTM and Keras<\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nimport numpy as np\r\nimport pandas as pd\r\nfrom sklearn import preprocessing\r\n\r\nfrom keras.models import Sequential\r\nfrom keras.layers.core import Dense\r\nfrom keras.layers.recurrent import LSTM\r\nfrom keras.layers import  Dropout\r\n\r\nimport matplotlib.pyplot as plt\r\n\r\n\r\nfname=&quot;C:\\\\Users\\\\stock_data.csv&quot;\r\ndata_csv = pd.read_csv (fname)\r\n\r\n#how many data we will use \r\n# (should not be more than dataset length )\r\ndata_to_use= 100\r\n\r\n# number of training data\r\n# should be less than data_to_use\r\ntrain_end =70\r\n\r\n\r\ntotal_data=len(data_csv)\r\n\r\n#most recent data is in the end \r\n#so need offset\r\nstart=total_data - data_to_use\r\n\r\n\r\n#currently doing prediction only for 3 steps ahead\r\nsteps_to_predict = 3\r\ntrain_mse=[]\r\ntest_mse=[]\r\nforecast=[]\r\nfor i in range(steps_to_predict):\r\n    train_mse.append(0)\r\n    test_mse.append(0)\r\n    forecast.append(0)\r\n\r\n\r\nyt = data_csv.iloc [start:total_data ,4]    #Close price\r\nyt1 = data_csv.iloc [start:total_data ,1]   #Open\r\nyt2 = data_csv.iloc [start:total_data ,2]   #High\r\nyt3 = data_csv.iloc [start:total_data ,3]   #Low\r\nvt = data_csv.iloc [start:total_data ,6]    # volume\r\n\r\n\r\n    \r\nfor i in range(steps_to_predict):\r\n    \r\n    if i==0:\r\n        units=20\r\n        batch_size=1\r\n    if i==1:\r\n        units=15\r\n        batch_size=1\r\n    if i==2:\r\n         units=80\r\n         batch_size=1\r\n    \r\n        \r\n\r\n    yt_ = yt.shift (-i - 1  )   \r\n\r\n    data = pd.concat ([yt, yt_, vt, yt1, yt2, yt3], axis =1)\r\n    data. columns = ['yt', 'yt_', 'vt', 'yt1', 'yt2', 'yt3']\r\n    \r\n    data = data.dropna()\r\n    \r\n   \r\n    \r\n# target variable - closed price\r\n    y = data ['yt_']\r\n\r\n       \r\n#       closed,  volume,   open,  high,   low    \r\n    cols =['yt',    'vt',  'yt1', 'yt2', 'yt3']\r\n    x = data [cols]\r\n\r\n  \r\n   \r\n    scaler_x = preprocessing.MinMaxScaler ( feature_range =( -1, 1))\r\n    x = np. array (x).reshape ((len( x) ,len(cols)))\r\n    x = scaler_x.fit_transform (x)\r\n\r\n   \r\n    scaler_y = preprocessing. MinMaxScaler ( feature_range =( -1, 1))\r\n    y = np.array (y).reshape ((len( y), 1))\r\n    y = scaler_y.fit_transform (y)\r\n\r\n\r\n\r\n    \r\n    x_train = x [0: train_end,]\r\n\r\n\r\n    x_test = x[ train_end +1:len(x),]    \r\n    y_train = y [0: train_end] \r\n\r\n\r\n\r\n    y_test = y[ train_end +1:len(y)]  \r\n               \r\n    if (i == 0) :     \r\n        prediction_data=[]\r\n        for j in range (len(y_test) - 0 ) :\r\n               prediction_data.append (0)       \r\n\r\n\r\n\r\n    x_train = x_train.reshape (x_train. shape + (1,)) \r\n    x_test = x_test.reshape (x_test. shape + (1,))\r\n\r\n    \r\n    \r\n\r\n    \r\n\r\n\r\n    seed =2018 \r\n    np.random.seed (seed)\r\n    \r\n##############\r\n##  i=0\r\n##############\r\n    if i == 0 :\r\n          fit0 = Sequential ()\r\n          fit0.add (LSTM (  units , activation = 'tanh', inner_activation = 'hard_sigmoid' , input_shape =(len(cols), 1) ))\r\n          fit0.add(Dropout(0.2))\r\n          fit0.add (Dense (output_dim =1, activation = 'linear'))\r\n          fit0.compile (loss =&quot;mean_squared_error&quot; , optimizer = &quot;adam&quot;)  \r\n   \r\n          fit0.fit (x_train, y_train, batch_size =batch_size, nb_epoch =25, shuffle = False)\r\n          train_mse[i] = fit0.evaluate (x_train, y_train, batch_size =batch_size)\r\n          test_mse[i] = fit0.evaluate (x_test, y_test, batch_size =batch_size)\r\n          pred = fit0.predict (x_test) \r\n          pred = scaler_y.inverse_transform (np. array (pred). reshape ((len( pred), 1)))\r\n             # below is just fo i == 0\r\n          for j in range (len(pred) - 0 ) :\r\n                   prediction_data[j] = pred[j] \r\n                  \r\n             \r\n               \r\n          forecast[i]=pred[-1]\r\n         \r\n                  \r\n#############\r\n##  i=1\r\n#############\r\n    if i == 1 :    \r\n          fit1 = Sequential ()\r\n          fit1.add (LSTM (  units , activation = 'tanh', inner_activation = 'hard_sigmoid' , input_shape =(len(cols), 1) ))\r\n          fit1.add(Dropout(0.2))\r\n          fit1.add (Dense (output_dim =1, activation = 'linear'))\r\n          fit1.compile (loss =&quot;mean_squared_error&quot; , optimizer = &quot;adam&quot;)  \r\n          fit1.fit (x_train, y_train, batch_size =batch_size, nb_epoch =25, shuffle = False)\r\n          train_mse[i] = fit1.evaluate (x_train, y_train, batch_size =batch_size)\r\n          test_mse[i] = fit1.evaluate (x_test, y_test, batch_size =batch_size)\r\n          pred = fit1.predict (x_test) \r\n          pred = scaler_y.inverse_transform (np. array (pred). reshape ((len( pred), 1)))\r\n          forecast[i]=pred[-1]\r\n        \r\n          \r\n       \r\n#############\r\n##  i=2\r\n#############\r\n    if i==2 :\r\n          fit2 = Sequential ()\r\n          fit2.add (LSTM (  units , activation = 'tanh', inner_activation = 'hard_sigmoid' , input_shape =(len(cols), 1) ))\r\n          fit2.add(Dropout(0.2))\r\n          fit2.add (Dense (output_dim =1, activation = 'linear'))\r\n          fit2.compile (loss =&quot;mean_squared_error&quot; , optimizer = &quot;adam&quot;)  \r\n          fit2.fit (x_train, y_train, batch_size =batch_size, nb_epoch =25, shuffle = False)\r\n          train_mse[i] = fit2.evaluate (x_train, y_train, batch_size =batch_size)\r\n          test_mse[i] = fit2.evaluate (x_test, y_test, batch_size =batch_size)\r\n          pred = fit2.predict (x_test) \r\n          pred = scaler_y.inverse_transform (np. array (pred). reshape ((len( pred), 1)))\r\n              \r\n          forecast[i]=pred[-1]\r\n \r\n\r\n    x_test = scaler_x.inverse_transform (np. array (x_test). reshape ((len( x_test), len(cols))))\r\n   \r\nprediction_data = np.asarray(prediction_data)\r\nprediction_data = prediction_data.ravel()\r\n\r\n\r\n\r\nfor j in range (len(prediction_data) - 1 ):\r\n    prediction_data[len(prediction_data) - j - 1  ] =  prediction_data[len(prediction_data) - 1 - j - 1]\r\n\r\n\r\nprediction_data = np.append(prediction_data, forecast)\r\n\r\nx_test_all = yt[len(yt)-len(prediction_data)-1:len(yt)-1]\r\nx_test_all = x_test_all.ravel()                \r\n\r\nplt.plot(prediction_data, label=&quot;predictions&quot;)\r\nplt.plot(  x_test_all, label=&quot;actual&quot;)\r\n\r\nplt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),\r\n          fancybox=True, shadow=True, ncol=2)\r\n\r\nimport matplotlib.ticker as mtick\r\nfmt = '$%.0f'\r\ntick = mtick.FormatStrFormatter(fmt)\r\nax = plt.axes()\r\nax.yaxis.set_major_formatter(tick)\r\n\r\n\r\nplt.show()\r\n\r\n\r\nprint (&quot;prediction data&quot;)\r\nprint ((prediction_data))\r\n\r\nprint (&quot;x_test_all&quot;)\r\nprint ((x_test_all))\r\n\r\nprint (&quot;train_mse&quot;)\r\nprint (train_mse)\r\n\r\nprint (&quot;test_mse&quot;)\r\nprint (test_mse)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here you can find python source code for Time Series Prediction with LSTM and Keras<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"jetpack_post_was_ever_published":false},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"Here you can find python source code for Time Series Prediction with LSTM and Keras\" \/>\n<meta property=\"og:url\" content=\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-17T23:33:08+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/\",\"name\":\"Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2018-01-05T00:22:14+00:00\",\"dateModified\":\"2018-04-17T23:33:08+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Time Series Prediction with LSTM and Keras &#8211; Python Source Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\",\"url\":\"http:\/\/intelligentonlinetools.com\/blog\/\",\"name\":\"Machine Learning Applications\",\"description\":\"Artificial intelligence, data mining and machine learning for building web based tools and services.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/","og_locale":"en_US","og_type":"article","og_title":"Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications","og_description":"Here you can find python source code for Time Series Prediction with LSTM and Keras","og_url":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/","og_site_name":"Machine Learning Applications","article_modified_time":"2018-04-17T23:33:08+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/","url":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/","name":"Time Series Prediction with LSTM and Keras - Python Source Code - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2018-01-05T00:22:14+00:00","dateModified":"2018-04-17T23:33:08+00:00","breadcrumb":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/intelligentonlinetools.com\/blog\/time-series-prediction-lstm-keras-python-source-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Time Series Prediction with LSTM and Keras &#8211; Python Source Code"}]},{"@type":"WebSite","@id":"http:\/\/intelligentonlinetools.com\/blog\/#website","url":"http:\/\/intelligentonlinetools.com\/blog\/","name":"Machine Learning Applications","description":"Artificial intelligence, data mining and machine learning for building web based tools and services.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/intelligentonlinetools.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P7h1IJ-sk","jetpack-related-posts":[{"id":1820,"url":"http:\/\/intelligentonlinetools.com\/blog\/site-map\/","url_meta":{"origin":1756,"position":0},"title":"Sitemap","date":"January 25, 2018","format":false,"excerpt":"Time Series and Stock Data Prediction Python Source Code Machine Learning Stock Prediction with LSTM and Keras Notes - Machine Learning Stock Prediction with LSTM and Keras Time Series Prediction with LSTM and Keras for Multiple Steps Ahead Prediction Data Stock Prices with Prophet Time Series Analysis with Python and\u2026","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2096,"url":"http:\/\/intelligentonlinetools.com\/blog\/source-code-for-machine-learning-correlation-data-analysis-between-food-and-mood\/","url_meta":{"origin":1756,"position":1},"title":"Source Code for Machine Learning Correlation Data Analysis Between Food and Mood","date":"June 14, 2018","format":false,"excerpt":"Here is the code for Machine Learning Correlation Data Analysis Between Food and Mood Python pandas dataframe is used in this script for calculation correlation between two time series.","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2501,"url":"http:\/\/intelligentonlinetools.com\/blog\/reinforcement-learning-dqn-run-planning\/","url_meta":{"origin":1756,"position":2},"title":"Reinforcement Learning DQN Run Planning","date":"January 5, 2019","format":false,"excerpt":"This is the python source code of run_planning_RL_DQN.py for post Reinforcement Learning Python DQN Application for Resource Allocation","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2364,"url":"http:\/\/intelligentonlinetools.com\/blog\/rl-dyna-q-run-planning-rl\/","url_meta":{"origin":1756,"position":3},"title":"Reinforcement Learning Dyna-Q Run Planning","date":"November 3, 2018","format":false,"excerpt":"This is the python source code of run_planning_RL.py for post Reinforcement Learning Example for Planning Tasks Using Q Learning and Dyna-Q","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2366,"url":"http:\/\/intelligentonlinetools.com\/blog\/rl-dyna-q\/","url_meta":{"origin":1756,"position":4},"title":"Reinforcement Learning Dyna-Q","date":"November 3, 2018","format":false,"excerpt":"This is the python source code of RL_brain.py for post Reinforcement Learning Example for Planning Tasks Using Q Learning and Dyna-Q","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2495,"url":"http:\/\/intelligentonlinetools.com\/blog\/reinforcement-learning-dqn-planning-environment\/","url_meta":{"origin":1756,"position":5},"title":"Reinforcement Learning DQN Planning Environment","date":"January 5, 2019","format":false,"excerpt":"This is the python source code of planning_envDQN.py for post Reinforcement Learning Python DQN Application for Resource Allocation","rel":"","context":"Similar post","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/1756"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/comments?post=1756"}],"version-history":[{"count":8,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/1756\/revisions"}],"predecessor-version":[{"id":1994,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/pages\/1756\/revisions\/1994"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}