{"id":1974,"date":"2018-04-03T23:19:45","date_gmt":"2018-04-03T23:19:45","guid":{"rendered":"http:\/\/intelligentonlinetools.com\/blog\/?p=1974"},"modified":"2018-04-05T23:36:16","modified_gmt":"2018-04-05T23:36:16","slug":"machine-learning-stock-market-prediction-lstm-keras","status":"publish","type":"post","link":"http:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/","title":{"rendered":"Machine Learning Stock Market Prediction with LSTM Keras"},"content":{"rendered":"<p>In the previous posts [1,2] I created script for machine learning stock market price on next day prediction. But it was pointed by readers that in stock market prediction, it is more important to know the trend: will the stock go up or down. So I updated the script to predict difference between today and yesterday prices. If it is negative the stock price will go down, if positive it will go up. Below will be described implemented modifications.<\/p>\n<h2>Data inputting<\/h2>\n<p>Data from previous days are entered as features through additional columns. The number of columns can be changed through parameter N in the beginning of script. So for example for day 20 the input will contain data for day 21 as target and data for days 20, 19,18,17&#8230; 20-N<br \/>\nAlso added differencing before scaling. Differencing helped to improve performance of network. It also makes easy to get changes from previous day. In the end the differenced data inverted back.<\/p>\n<p>Below is the stock data prices after applying differencing (subtructing previous day stock data price from current day) <\/p>\n<figure id=\"attachment_1980\" aria-describedby=\"caption-attachment-1980\" style=\"width: 640px\" class=\"wp-caption alignnone\"><img data-attachment-id=\"1980\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/timeseries_diffenced\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-e1522800325781.png\" data-orig-size=\"650,515\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Stock Data Prices after Differencing\" data-image-description=\"&lt;p&gt;Stock Data Prices after Differencing&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Stock Data Prices after Differencing&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-300x237.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-e1522800325781.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-e1522800325781.png\" alt=\"Stock Data Prices after Differencing\" width=\"650\" height=\"515\" class=\"size-full wp-image-1980\" \/><figcaption id=\"caption-attachment-1980\" class=\"wp-caption-text\">Stock Data Prices after Differencing<\/figcaption><\/figure>\n<h2>Predicting future changes<\/h2>\n<p>I used moving_test_window_preds function. Inside of this function the script within loop is adding new prediction to &#8220;moving window&#8221; array and removing first element from it. This is based on example from blog post on forecasting time series with LSTM[4].<br \/>\nSo the script is predicting future day data based on the previous known data in the &#8220;moving window&#8221;, updating  known data and starting again.  The performance evaluated by comparing predicted data with test (not used before) data.<\/p>\n<h2>LSTM Configuration<\/h2>\n<p>The LSTM network is constructed as following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmodel = Sequential ()\r\ninput_shape =(len(cols), 1) ))\r\nmodel.add (LSTM ( 400,  activation = 'relu', inner_activation = 'hard_sigmoid' , bias_regularizer=L1L2(l1=0.01, l2=0.01),  input_shape =(len(cols), 1), return_sequences = False ))\r\nmodel.add(Dropout(0.3))\r\nfrom keras import optimizers\r\nmodel.add (Dense (output_dim =1, activation = 'linear', activity_regularizer=regularizers.l1(0.01)))\r\nadam=optimizers.Adam(lr=0.01, beta_1=0.89, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=True)\r\nmodel.compile (loss =&quot;mean_squared_error&quot; , optimizer = &quot;adam&quot;) \r\nhistory=model.fit (x_train, y_train, batch_size =1, nb_epoch =1400, shuffle = False, validation_split=0.15)\r\n<\/pre>\n<p>Weight regularization together with differencing helped to decrease overfitting.<\/p>\n<h2>Results<\/h2>\n<p>Performance of NN  is 88% :   8 correct data out of 9. Below are the data charts that comparing predicted data (9 first days from 22 total days) with actual test data. Below you can find output chart and full python source code.<\/p>\n<figure id=\"attachment_1982\" aria-describedby=\"caption-attachment-1982\" style=\"width: 674px\" class=\"wp-caption alignnone\"><img data-attachment-id=\"1982\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/timeseries_differenced_prediction_for_future\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future.png\" data-orig-size=\"684,560\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Stock Data Prices Prediction with LSTM\" data-image-description=\"&lt;p&gt;Stock Data Prices Prediction with LSTM&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Stock Data Prices Prediction with LSTM&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future-300x246.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future.png\" alt=\"Stock Data Prices Prediction with LSTM\" width=\"684\" height=\"560\" class=\"size-full wp-image-1982\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future.png 684w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_prediction_for_future-300x246.png 300w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/><figcaption id=\"caption-attachment-1982\" class=\"wp-caption-text\">Stock Data Prices Prediction with LSTM<\/figcaption><\/figure>\n<figure id=\"attachment_1983\" aria-describedby=\"caption-attachment-1983\" style=\"width: 641px\" class=\"wp-caption alignnone\"><img data-attachment-id=\"1983\" data-permalink=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/timeseries_differenced_and_inverted_back\/#main\" data-orig-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back.png\" data-orig-size=\"651,523\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Stock Data Prices Prediction with LSTM\" data-image-description=\"&lt;p&gt;Stock Data Prices Prediction with LSTM, Data Inverted Back from Differencing&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Stock Data Prices Prediction with LSTM, Data Inverted Back from Differencing&lt;\/p&gt;\n\" data-medium-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back-300x241.png\" data-large-file=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back.png\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back.png\" alt=\"Stock Data Prices Prediction with LSTM\" width=\"651\" height=\"523\" class=\"size-full wp-image-1983\" srcset=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back.png 651w, http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_differenced_and_inverted_back-300x241.png 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><figcaption id=\"caption-attachment-1983\" class=\"wp-caption-text\">Stock Data Prices Prediction with LSTM, Data Inverted Back from Differencing<\/figcaption><\/figure>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport pandas as pd\r\nfrom sklearn import preprocessing\r\n\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.ticker as mtick\r\n\r\nfrom keras.regularizers import L1L2\r\n\r\nfname=&quot;C:\\\\Users\\\\Leo\\\\Desktop\\\\A\\\\WS\\\\stock data analysis 2017\\\\GM.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= 150\r\n\r\n# number of training data\r\n# should be less than data_to_use\r\ntrain_end =120\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\nyt = data_csv.iloc [start:total_data ,4]    #Close price\r\nyt_ = yt.shift (-1)   \r\n\r\nprint (yt_)\r\n\r\ndata = pd.concat ([yt, yt_], axis =1)\r\ndata. columns = ['yt', 'yt_']\r\n\r\n\r\nN=18    \r\ncols =['yt']\r\nfor i in range (N):\r\n  \r\n    data['yt'+str(i)] = list(yt.shift(i+1))\r\n    cols.append ('yt'+str(i))\r\n    \r\ndata = data.dropna()\r\ndata_original = data\r\ndata=data.diff()\r\ndata = data.dropna()\r\n    \r\n    \r\n# target variable - closed price\r\n# after shifting\r\ny = data ['yt_']\r\nx = data [cols]\r\n\r\n   \r\nscaler_x = preprocessing.MinMaxScaler ( feature_range =( -1, 1))\r\nx = np. array (x).reshape ((len( x) ,len(cols)))\r\nx = scaler_x.fit_transform (x)\r\n\r\nscaler_y = preprocessing. MinMaxScaler ( feature_range =( -1, 1))\r\ny = np.array (y).reshape ((len( y), 1))\r\ny = scaler_y.fit_transform (y)\r\n\r\n    \r\nx_train = x [0: train_end,]\r\nx_test = x[ train_end +1:len(x),]    \r\ny_train = y [0: train_end] \r\ny_test = y[ train_end +1:len(y)]  \r\n\r\nx_train = x_train.reshape (x_train. shape + (1,)) \r\nx_test = x_test.reshape (x_test. shape + (1,))\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\nfrom keras import optimizers\r\n\r\nfrom numpy.random import seed\r\nseed(1)\r\nfrom tensorflow import set_random_seed\r\nset_random_seed(2)\r\n\r\nfrom keras import regularizers\r\n\r\n\r\nmodel = Sequential ()\r\nmodel.add (LSTM ( 400,  activation = 'relu', inner_activation = 'hard_sigmoid' , bias_regularizer=L1L2(l1=0.01, l2=0.01),  input_shape =(len(cols), 1), return_sequences = False ))\r\nmodel.add(Dropout(0.3))\r\nmodel.add (Dense (output_dim =1, activation = 'linear', activity_regularizer=regularizers.l1(0.01)))\r\nadam=optimizers.Adam(lr=0.01, beta_1=0.89, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=True)\r\nmodel.compile (loss =&quot;mean_squared_error&quot; , optimizer = &quot;adam&quot;) \r\nhistory=model.fit (x_train, y_train, batch_size =1, nb_epoch =1400, shuffle = False, validation_split=0.15)\r\n\r\n\r\ny_train_back=scaler_y.inverse_transform (np. array (y_train). reshape ((len( y_train), 1)))\r\nplt.figure(1)\r\nplt.plot (y_train_back)\r\n\r\n\r\nfmt = '%.1f'\r\ntick = mtick.FormatStrFormatter(fmt)\r\nax = plt.axes()\r\nax.yaxis.set_major_formatter(tick)\r\nprint (model.summary())\r\nprint(history.history.keys())\r\n\r\nplt.figure(2)\r\nplt.plot(history.history['loss'])\r\nplt.plot(history.history['val_loss'])\r\nplt.title('model loss')\r\nplt.ylabel('loss')\r\nplt.xlabel('epoch')\r\nplt.legend(['train', 'test'], loc='upper left')\r\nfmt = '%.1f'\r\ntick = mtick.FormatStrFormatter(fmt)\r\nax = plt.axes()\r\nax.yaxis.set_major_formatter(tick)\r\n\r\nscore_train = model.evaluate (x_train, y_train, batch_size =1)\r\nscore_test = model.evaluate (x_test, y_test, batch_size =1)\r\nprint (&quot; in train MSE = &quot;, round( score_train ,4)) \r\nprint (&quot; in test MSE = &quot;, score_test )\r\n\r\npred1 = model.predict (x_test) \r\npred1 = scaler_y.inverse_transform (np. array (pred1). reshape ((len( pred1), 1)))\r\n \r\nprediction_data = pred1[-1]     \r\nmodel.summary()\r\nprint (&quot;Inputs: {}&quot;.format(model.input_shape))\r\nprint (&quot;Outputs: {}&quot;.format(model.output_shape))\r\nprint (&quot;Actual input: {}&quot;.format(x_test.shape))\r\nprint (&quot;Actual output: {}&quot;.format(y_test.shape))\r\n\r\nprint (&quot;prediction data:&quot;)\r\nprint (prediction_data)\r\n\r\ny_test = scaler_y.inverse_transform (np. array (y_test). reshape ((len( y_test), 1)))\r\nprint (&quot;y_test:&quot;)\r\nprint (y_test)\r\n\r\nact_data = np.array([row[0] for row in y_test])\r\n\r\nfmt = '%.1f'\r\ntick = mtick.FormatStrFormatter(fmt)\r\nax = plt.axes()\r\nax.yaxis.set_major_formatter(tick)\r\n\r\nplt.figure(3)\r\nplt.plot( y_test, label=&quot;actual&quot;)\r\nplt.plot(pred1, label=&quot;predictions&quot;)\r\n\r\nprint (&quot;act_data:&quot;)\r\nprint (act_data)\r\n\r\nprint (&quot;pred1:&quot;)\r\nprint (pred1)\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\n\r\nfmt = '$%.1f'\r\ntick = mtick.FormatStrFormatter(fmt)\r\nax = plt.axes()\r\nax.yaxis.set_major_formatter(tick)\r\n\r\ndef moving_test_window_preds(n_future_preds):\r\n\r\n    ''' n_future_preds - Represents the number of future predictions we want to make\r\n                         This coincides with the number of windows that we will move forward\r\n                         on the test data\r\n    '''\r\n    preds_moving = []                                    # Store the prediction made on each test window\r\n    moving_test_window = [x_test[0,:].tolist()]          # First test window\r\n    moving_test_window = np.array(moving_test_window)    \r\n   \r\n    for i in range(n_future_preds):\r\n      \r\n      \r\n        preds_one_step = model.predict(moving_test_window) \r\n        preds_moving.append(preds_one_step[0,0]) \r\n                       \r\n        preds_one_step = preds_one_step.reshape(1,1,1) \r\n        moving_test_window = np.concatenate((moving_test_window[:,1:,:], preds_one_step), axis=1) # new moving test window, where the first element from the window has been removed and the prediction  has been appended to the end\r\n        \r\n\r\n    print (&quot;pred moving before scaling:&quot;)\r\n    print (preds_moving)\r\n                                         \r\n    preds_moving = scaler_y.inverse_transform((np.array(preds_moving)).reshape(-1, 1))\r\n    \r\n    print (&quot;pred moving after scaling:&quot;)\r\n    print (preds_moving)\r\n    return preds_moving\r\n    \r\nprint (&quot;do moving test predictions for next 22 days:&quot;)    \r\npreds_moving = moving_test_window_preds(22)\r\n\r\n\r\ncount_correct=0\r\nerror =0\r\nfor i in range (len(y_test)):\r\n    error=error + ((y_test[i]-preds_moving[i])**2) \/ y_test[i]\r\n\r\n \r\n    if y_test[i] &gt;=0 and preds_moving[i] &gt;=0 :\r\n        count_correct=count_correct+1\r\n    if y_test[i] &lt; 0 and preds_moving[i] &lt; 0 :\r\n        count_correct=count_correct+1\r\n\r\naccuracy_in_change =  count_correct \/ (len(y_test) )\r\n\r\nplt.figure(4)\r\nplt.title(&quot;Forecast vs Actual, (data is differenced)&quot;)          \r\nplt.plot(preds_moving, label=&quot;predictions&quot;)\r\nplt.plot(y_test, label=&quot;actual&quot;)\r\nplt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),\r\n          fancybox=True, shadow=True, ncol=2)\r\n\r\n\r\nprint (&quot;accuracy_in_change:&quot;)\r\nprint (accuracy_in_change)\r\n\r\nind=data_original.index.values[0] + data_original.shape[0] -len(y_test)-1\r\nprev_starting_price = data_original.loc[ind,&quot;yt_&quot;]\r\npreds_moving_before_diff =  [0 for x in range(len(preds_moving))]\r\n\r\nfor i in range (len(preds_moving)):\r\n    if (i==0):\r\n        preds_moving_before_diff[i]=prev_starting_price + preds_moving[i]\r\n    else:\r\n        preds_moving_before_diff[i]=preds_moving_before_diff[i-1]+preds_moving[i]\r\n\r\n\r\ny_test_before_diff = [0 for x in range(len(y_test))]\r\n\r\nfor i in range (len(y_test)):\r\n    if (i==0):\r\n        y_test_before_diff[i]=prev_starting_price + y_test[i]\r\n    else:\r\n        y_test_before_diff[i]=y_test_before_diff[i-1]+y_test[i]\r\n\r\n\r\nplt.figure(5)\r\nplt.title(&quot;Forecast vs Actual (non differenced data)&quot;)\r\nplt.plot(preds_moving_before_diff, label=&quot;predictions&quot;)\r\nplt.plot(y_test_before_diff, label=&quot;actual&quot;)\r\nplt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05),\r\n          fancybox=True, shadow=True, ncol=2)\r\nplt.show()\r\n<\/pre>\n<p><strong>References<\/strong><br \/>\n1. <a href=http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/27\/time-series-prediction-lstm-keras\/ target=\"_blank\">Time Series Prediction with LSTM and Keras for Multiple Steps Ahead<\/a><br \/>\n2. <a href=http:\/\/intelligentonlinetools.com\/blog\/2018\/01\/19\/machine-learning-stock-prediction-lstm-keras\/ target=\"_blank\">Machine Learning Stock Prediction with LSTM and Keras<\/a><br \/>\n3. <a href=http:\/\/intelligentonlinetools.com\/blog\/stock-data-file\/ target=\"_blank\">Data File<\/a><br \/>\n4. <a href=https:\/\/towardsdatascience.com\/using-lstms-to-forecast-time-series-4ab688386b1f target=\"_blank\">Using LSTMs to forecast time-series<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous posts [1,2] I created script for machine learning stock market price on next day prediction. But it was pointed by readers that in stock market prediction, it is more important to know the trend: will the stock go up or down. So I updated the script to predict difference between today and &#8230; <a title=\"Machine Learning Stock Market Prediction with LSTM Keras\" class=\"read-more\" href=\"http:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[9,10,60],"tags":[42,49,48,27,16],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Machine Learning Stock Market Prediction with LSTM Keras - 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=\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Machine Learning Stock Market Prediction with LSTM Keras - Machine Learning Applications\" \/>\n<meta property=\"og:description\" content=\"In the previous posts [1,2] I created script for machine learning stock market price on next day prediction. But it was pointed by readers that in stock market prediction, it is more important to know the trend: will the stock go up or down. So I updated the script to predict difference between today and ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\" \/>\n<meta property=\"og:site_name\" content=\"Machine Learning Applications\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-03T23:19:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-05T23:36:16+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-e1522800325781.png\" \/>\n<meta name=\"author\" content=\"owygs156\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"owygs156\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\",\"url\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\",\"name\":\"Machine Learning Stock Market Prediction with LSTM Keras - Machine Learning Applications\",\"isPartOf\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#website\"},\"datePublished\":\"2018-04-03T23:19:45+00:00\",\"dateModified\":\"2018-04-05T23:36:16+00:00\",\"author\":{\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\"},\"breadcrumb\":{\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/intelligentonlinetools.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Machine Learning Stock Market Prediction with LSTM Keras\"}]},{\"@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\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478\",\"name\":\"owygs156\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g\",\"caption\":\"owygs156\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Machine Learning Stock Market Prediction with LSTM Keras - 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":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/","og_locale":"en_US","og_type":"article","og_title":"Machine Learning Stock Market Prediction with LSTM Keras - Machine Learning Applications","og_description":"In the previous posts [1,2] I created script for machine learning stock market price on next day prediction. But it was pointed by readers that in stock market prediction, it is more important to know the trend: will the stock go up or down. So I updated the script to predict difference between today and ... Read more","og_url":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/","og_site_name":"Machine Learning Applications","article_published_time":"2018-04-03T23:19:45+00:00","article_modified_time":"2018-04-05T23:36:16+00:00","og_image":[{"url":"http:\/\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/04\/timeseries_diffenced-e1522800325781.png"}],"author":"owygs156","twitter_card":"summary_large_image","twitter_misc":{"Written by":"owygs156","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/","url":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/","name":"Machine Learning Stock Market Prediction with LSTM Keras - Machine Learning Applications","isPartOf":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#website"},"datePublished":"2018-04-03T23:19:45+00:00","dateModified":"2018-04-05T23:36:16+00:00","author":{"@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478"},"breadcrumb":{"@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/intelligentonlinetools.com\/blog\/2018\/04\/03\/machine-learning-stock-market-prediction-lstm-keras\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/intelligentonlinetools.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Machine Learning Stock Market Prediction with LSTM Keras"}]},{"@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"},{"@type":"Person","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/7a886dc5eb9758369af2f6d2cb342478","name":"owygs156","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/intelligentonlinetools.com\/blog\/#\/schema\/person\/image\/","url":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","contentUrl":"http:\/\/2.gravatar.com\/avatar\/b351def598609cb4c0b5bca26497c7e5?s=96&d=mm&r=g","caption":"owygs156"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7h1IJ-vQ","jetpack-related-posts":[{"id":1783,"url":"http:\/\/intelligentonlinetools.com\/blog\/2018\/01\/19\/machine-learning-stock-prediction-lstm-keras\/","url_meta":{"origin":1974,"position":0},"title":"Machine Learning Stock Prediction with LSTM and Keras","date":"January 19, 2018","format":false,"excerpt":"In this post I will share experiments on machine learning stock prediction with LSTM and Keras with one step ahead. I tried to do first multiple steps ahead with few techniques described in the papers on the web. But I discovered that I need fully understand and test the simplest\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"Forecasting one step ahead LSTM 60","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/01\/Forecasting-one-step-ahead-LSTM-60-3_1_2018.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1798,"url":"http:\/\/intelligentonlinetools.com\/blog\/2018\/01\/20\/machine-learning-stock-prediction-lstm-keras-python-source-code\/","url_meta":{"origin":1974,"position":1},"title":"Machine Learning Stock Prediction with LSTM and Keras &#8211; Python Source Code","date":"January 20, 2018","format":false,"excerpt":"Python Source Code for Machine Learning Stock Prediction with LSTM and Keras - Python Source Code with LSTM and Keras Below is the code for machine learning stock prediction with LSTM neural network. References 1. Machine Learning Stock Prediction with LSTM and Keras - Python Source Code with LSTM and\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1754,"url":"http:\/\/intelligentonlinetools.com\/blog\/2018\/02\/27\/time-series-prediction-lstm-keras\/","url_meta":{"origin":1974,"position":2},"title":"Time Series Prediction with LSTM and Keras for Multiple Steps Ahead","date":"February 27, 2018","format":false,"excerpt":"In this post I will share experiment with Time Series Prediction with LSTM and Keras. LSTM neural network is used in this experiment for multiple steps ahead for stock prices data. The experiment is based on the paper [1]. The authors of the paper examine independent value prediction approach. With\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"Multiple step prediction with separate neural networks","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2018\/02\/multiple-step-prediction.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1628,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/12\/17\/time-series-analysis-python-prophet\/","url_meta":{"origin":1974,"position":3},"title":"Time Series Analysis with Python and Prophet","date":"December 17, 2017","format":false,"excerpt":"Recently Facebook released Prophet - open source software tool for forecasting time series data. Facebook team have implemented in Prophet two trend models that can cover many applications: a saturating growth model, and a piecewise linear model. [4] With growth model Prophet can be used for prediction growth\/decay - for\u2026","rel":"","context":"In &quot;Machine Learning&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/12\/time-series-analysis-python-300x180.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1682,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/12\/26\/prediction-data-stock-price-prophet-report\/","url_meta":{"origin":1974,"position":4},"title":"Prediction Data Stock Prices with Prophet","date":"December 26, 2017","format":false,"excerpt":"In the previous post I showed how to use the Prophet for time series analysis with python. I used Prophet for data stock price prediction. But it was used only for one stock and only for next 10 days. In this post we will select more data and will test\u2026","rel":"","context":"In &quot;Data Mining&quot;","img":{"alt_text":"prediction data stock prices with Prophet - accuracy vs used data range","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/12\/prediction-data-stock-prices-with-Prophet-accuracy-vs-used-data-range-1-e1514396944169.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1178,"url":"http:\/\/intelligentonlinetools.com\/blog\/2017\/05\/14\/time-series-prediction-with-convolutional-neural-networks\/","url_meta":{"origin":1974,"position":5},"title":"Forecasting Time Series Data with Convolutional Neural Networks","date":"May 14, 2017","format":false,"excerpt":"Convolutional neural networks(CNN) is increasingly important concept in computer science and finds more and more applications in different fields. Many posts on the web are about applying convolutional neural networks for image classification as CNN is very useful type of neural networks for image classification. But convolutional neural networks can\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/intelligentonlinetools.com\/blog\/wp-content\/uploads\/2017\/05\/time-series-LSTM-300x164.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1974"}],"collection":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"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=1974"}],"version-history":[{"count":11,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1974\/revisions"}],"predecessor-version":[{"id":1991,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/posts\/1974\/revisions\/1991"}],"wp:attachment":[{"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/media?parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/categories?post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/intelligentonlinetools.com\/blog\/wp-json\/wp\/v2\/tags?post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}