{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-04T17:40:53.930429Z","iopub.execute_input":"2022-07-04T17:40:53.931412Z","iopub.status.idle":"2022-07-04T17:40:53.953027Z","shell.execute_reply.started":"2022-07-04T17:40:53.931330Z","shell.execute_reply":"2022-07-04T17:40:53.952161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Long Short Term Memory [LSTM]:**\n**Recurrent Neural Network is a machine learning algorithm, perfectly suited for machine learning\nproblem that involves sequential data. Recurrent Neural Network model remembers the previous data\npoint, which causes it to optimize its weights and also tune other hyper parameters according to the\nprevious data point, with the aim to determine the next data point. Because of this feature we use\nRecurrent Neural Network model in time series problems, such as stock market prediction, market price\nprediction, Natural Language Processing, as in these predictions, previous past input data has high\ninfluence on future outputs.**\n\n**The whole idea behind the Recurrent Neural Network is to modify the weight values, which could be\nachieve by weight optimization techniques. But, as we calculate the loss gradient for weight\noptimization technique, we have to use the chain rule, as it might sometimes lead to a problem like\nvanishing gradient problem, which is due to the long term dependency. In order to, solve vanishing\ngradient problem, we introduce LSTM, which optimizes its weight and uses the sigmoid function, to\nforget the inputs which are less prioritized.**\n\n","metadata":{}},{"cell_type":"markdown","source":"**I will get our dataset from train stock prices, which has almost 12 columns, and among those columns, we only need datas from close column as an input, as these values might have some significance in predicting the future values..**","metadata":{}},{"cell_type":"code","source":"dataset = \"/kaggle/input/jpx-tokyo-stock-exchange-prediction\"\ntrain_file = \"train_files/stock_prices.csv\"\ntrain_all_file = os.path.join(dataset, train_file)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:40:55.368783Z","iopub.execute_input":"2022-07-04T17:40:55.369578Z","iopub.status.idle":"2022-07-04T17:40:55.374226Z","shell.execute_reply.started":"2022-07-04T17:40:55.369542Z","shell.execute_reply":"2022-07-04T17:40:55.373132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I will read the csv file from the given path.**","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(train_all_file)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:40:55.951933Z","iopub.execute_input":"2022-07-04T17:40:55.952410Z","iopub.status.idle":"2022-07-04T17:41:01.511113Z","shell.execute_reply.started":"2022-07-04T17:40:55.952344Z","shell.execute_reply":"2022-07-04T17:41:01.510147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:01.513080Z","iopub.execute_input":"2022-07-04T17:41:01.513484Z","iopub.status.idle":"2022-07-04T17:41:01.556415Z","shell.execute_reply.started":"2022-07-04T17:41:01.513444Z","shell.execute_reply":"2022-07-04T17:41:01.555233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:01.557936Z","iopub.execute_input":"2022-07-04T17:41:01.558327Z","iopub.status.idle":"2022-07-04T17:41:01.591334Z","shell.execute_reply.started":"2022-07-04T17:41:01.558280Z","shell.execute_reply":"2022-07-04T17:41:01.590172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:01.594028Z","iopub.execute_input":"2022-07-04T17:41:01.594663Z","iopub.status.idle":"2022-07-04T17:41:01.601081Z","shell.execute_reply.started":"2022-07-04T17:41:01.594628Z","shell.execute_reply":"2022-07-04T17:41:01.600165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As it could be clearly seen that there are almost 2400000 stock price values**","metadata":{}},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:01.602707Z","iopub.execute_input":"2022-07-04T17:41:01.603326Z","iopub.status.idle":"2022-07-04T17:41:02.256245Z","shell.execute_reply.started":"2022-07-04T17:41:01.603288Z","shell.execute_reply":"2022-07-04T17:41:02.255368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(df['Close'])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:02.257640Z","iopub.execute_input":"2022-07-04T17:41:02.258175Z","iopub.status.idle":"2022-07-04T17:41:02.931210Z","shell.execute_reply.started":"2022-07-04T17:41:02.258141Z","shell.execute_reply":"2022-07-04T17:41:02.930291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The graph seen above is quite incomprehensible.**","metadata":{}},{"cell_type":"markdown","source":"**We need to calculate the adjusted close price by multiplying the ajustment factor with \"Close\" value.**","metadata":{}},{"cell_type":"code","source":"\nfrom decimal import ROUND_HALF_UP, Decimal\ndef adjusted_price(price):\n    price.loc[:, \"Date\"] = pd.to_datetime(price.loc[:, \"Date\"], format = \"%Y-%m-%d\")\n    \n    def generated_close(df):\n        df = df.sort_values(\"Date\", ascending=False)\n        df.loc[:, \"CumulativeAdjustmentFactor\"] = df[\"AdjustmentFactor\"].cumprod()\n        df.loc[:, \"AdjustedClose\"] = (\n            df[\"CumulativeAdjustmentFactor\"] * df[\"Close\"]\n        ).map(lambda x: float(\n            Decimal(str(x)).quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)\n        ))\n        df = df.sort_values(\"Date\")\n        df.loc[df[\"AdjustedClose\"] == 0, \"AdjustedClose\"] = np.nan\n        df.loc[:, \"AdjustedClose\"] = df.loc[:, \"AdjustedClose\"].ffill()\n        return df\n    price = price.sort_values([\"SecuritiesCode\", \"Date\"])\n    price = price.groupby(\"SecuritiesCode\").apply(generated_close).reset_index(drop=True)\n\n    price.set_index(\"Date\", inplace=True)\n    return price","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:02.933199Z","iopub.execute_input":"2022-07-04T17:41:02.933800Z","iopub.status.idle":"2022-07-04T17:41:02.942977Z","shell.execute_reply.started":"2022-07-04T17:41:02.933751Z","shell.execute_reply":"2022-07-04T17:41:02.941983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As, I have added this new column, which is adjusted close value, and it must be taken as an input to train my neural network.**","metadata":{}},{"cell_type":"code","source":"df = adjusted_price(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:02.944727Z","iopub.execute_input":"2022-07-04T17:41:02.945487Z","iopub.status.idle":"2022-07-04T17:41:19.749464Z","shell.execute_reply.started":"2022-07-04T17:41:02.945437Z","shell.execute_reply":"2022-07-04T17:41:19.748471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:19.750776Z","iopub.execute_input":"2022-07-04T17:41:19.751760Z","iopub.status.idle":"2022-07-04T17:41:19.771555Z","shell.execute_reply.started":"2022-07-04T17:41:19.751720Z","shell.execute_reply":"2022-07-04T17:41:19.770599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:19.776628Z","iopub.execute_input":"2022-07-04T17:41:19.777426Z","iopub.status.idle":"2022-07-04T17:41:19.784059Z","shell.execute_reply.started":"2022-07-04T17:41:19.777391Z","shell.execute_reply":"2022-07-04T17:41:19.783061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:19.785892Z","iopub.execute_input":"2022-07-04T17:41:19.786430Z","iopub.status.idle":"2022-07-04T17:41:19.833028Z","shell.execute_reply.started":"2022-07-04T17:41:19.786394Z","shell.execute_reply":"2022-07-04T17:41:19.832045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df[\"AdjustedClose\"])\nplt.ylabel(\"Adjusted_Close_Value\")\nplt.xlabel(\"Year\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:19.834448Z","iopub.execute_input":"2022-07-04T17:41:19.835141Z","iopub.status.idle":"2022-07-04T17:41:20.743278Z","shell.execute_reply.started":"2022-07-04T17:41:19.835106Z","shell.execute_reply":"2022-07-04T17:41:20.742245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df[\"AdjustedClose\"])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.744983Z","iopub.execute_input":"2022-07-04T17:41:20.745334Z","iopub.status.idle":"2022-07-04T17:41:20.753310Z","shell.execute_reply.started":"2022-07-04T17:41:20.745291Z","shell.execute_reply":"2022-07-04T17:41:20.752339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**To make the graph more clear, and to determine the rise and fall of stock prices, clearly on the graph. I have taken only 100 adjusted Close prices. The graph can be seen below.**","metadata":{}},{"cell_type":"code","source":"dataset = df[\"AdjustedClose\"]\nlen(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.755133Z","iopub.execute_input":"2022-07-04T17:41:20.755908Z","iopub.status.idle":"2022-07-04T17:41:20.764442Z","shell.execute_reply.started":"2022-07-04T17:41:20.755872Z","shell.execute_reply":"2022-07-04T17:41:20.763403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = dataset[:100]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.766056Z","iopub.execute_input":"2022-07-04T17:41:20.766825Z","iopub.status.idle":"2022-07-04T17:41:20.773829Z","shell.execute_reply.started":"2022-07-04T17:41:20.766784Z","shell.execute_reply":"2022-07-04T17:41:20.772901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.775415Z","iopub.execute_input":"2022-07-04T17:41:20.776101Z","iopub.status.idle":"2022-07-04T17:41:20.785316Z","shell.execute_reply.started":"2022-07-04T17:41:20.776065Z","shell.execute_reply":"2022-07-04T17:41:20.784133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.787044Z","iopub.execute_input":"2022-07-04T17:41:20.787813Z","iopub.status.idle":"2022-07-04T17:41:20.800182Z","shell.execute_reply.started":"2022-07-04T17:41:20.787778Z","shell.execute_reply":"2022-07-04T17:41:20.799313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I have defined the number of 100 datas to influence the future stock prices.** **It is clearly seen that the length of the values is more than permssible, as training of the data might strain the computer. So we need to divide the values into more permissible amount and also further divide them into train set and test set**.","metadata":{}},{"cell_type":"code","source":"df1.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.801238Z","iopub.execute_input":"2022-07-04T17:41:20.802233Z","iopub.status.idle":"2022-07-04T17:41:20.811998Z","shell.execute_reply.started":"2022-07-04T17:41:20.802199Z","shell.execute_reply":"2022-07-04T17:41:20.810992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing the important Packages, that are needed.**","metadata":{}},{"cell_type":"code","source":"import math\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.metrics import mean_squared_error\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Dense, LSTM, Flatten, Activation, Dropout\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:20.813979Z","iopub.execute_input":"2022-07-04T17:41:20.814311Z","iopub.status.idle":"2022-07-04T17:41:26.290700Z","shell.execute_reply.started":"2022-07-04T17:41:20.814279Z","shell.execute_reply":"2022-07-04T17:41:26.289705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df1)\nplt.ylabel(\"AdjustedClose_Values\")\nplt.xlabel(\"Year\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:26.292021Z","iopub.execute_input":"2022-07-04T17:41:26.292789Z","iopub.status.idle":"2022-07-04T17:41:26.482221Z","shell.execute_reply.started":"2022-07-04T17:41:26.292746Z","shell.execute_reply":"2022-07-04T17:41:26.481277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Since, there are large number of datasets, it will be quite straining and time consuming task to fit and run the whole model by taking these large number of close values. So, I have taken only 200000 values, which could be used for my model training process.**","metadata":{}},{"cell_type":"code","source":"df2 = dataset[:200000]\nlen(df2)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:26.483606Z","iopub.execute_input":"2022-07-04T17:41:26.484427Z","iopub.status.idle":"2022-07-04T17:41:26.491630Z","shell.execute_reply.started":"2022-07-04T17:41:26.484389Z","shell.execute_reply":"2022-07-04T17:41:26.490410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:26.493062Z","iopub.execute_input":"2022-07-04T17:41:26.493536Z","iopub.status.idle":"2022-07-04T17:41:26.507363Z","shell.execute_reply.started":"2022-07-04T17:41:26.493485Z","shell.execute_reply":"2022-07-04T17:41:26.506241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.plot(figsize=(12, 4), legend=True, linestyle=\"--\", marker=\"o\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:26.509553Z","iopub.execute_input":"2022-07-04T17:41:26.509993Z","iopub.status.idle":"2022-07-04T17:41:28.311995Z","shell.execute_reply.started":"2022-07-04T17:41:26.509958Z","shell.execute_reply":"2022-07-04T17:41:28.310088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(df2, bins=100, color='magenta')","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.314162Z","iopub.execute_input":"2022-07-04T17:41:28.314954Z","iopub.status.idle":"2022-07-04T17:41:28.840277Z","shell.execute_reply.started":"2022-07-04T17:41:28.314913Z","shell.execute_reply":"2022-07-04T17:41:28.839302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We keep the whole values between 0 and 1, by using the MinMaxScaler**\n","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler(feature_range=(0,1))\ndf2 = scaler.fit_transform(np.array(df2).reshape(-1, 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.841669Z","iopub.execute_input":"2022-07-04T17:41:28.842105Z","iopub.status.idle":"2022-07-04T17:41:28.849848Z","shell.execute_reply.started":"2022-07-04T17:41:28.842067Z","shell.execute_reply":"2022-07-04T17:41:28.848747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df2)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.851219Z","iopub.execute_input":"2022-07-04T17:41:28.851671Z","iopub.status.idle":"2022-07-04T17:41:28.864310Z","shell.execute_reply.started":"2022-07-04T17:41:28.851635Z","shell.execute_reply":"2022-07-04T17:41:28.863125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(df2)*0.75)\ntest_size = len(df2)-train_size\ntrain_data, test_data = df2[0:train_size, :], df2[train_size:len(df2), :1]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.865842Z","iopub.execute_input":"2022-07-04T17:41:28.866324Z","iopub.status.idle":"2022-07-04T17:41:28.875088Z","shell.execute_reply.started":"2022-07-04T17:41:28.866288Z","shell.execute_reply":"2022-07-04T17:41:28.873887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here, I have taken time step as 100, and we have to train the model by taking the 100 inputs to the 101 as output, and the process continues for 200000 datas. The whole model is developed in this way to predict the another time step stock close value.**","metadata":{}},{"cell_type":"code","source":"def create_dataset(dataset, time_step=1):\n    dataX, dataY = [], []\n    for i in range(len(dataset)-time_step-1):\n        a = dataset[i:(i+time_step), 0]\n        dataX.append(a)\n        dataY.append(dataset[i+time_step, 0])\n    return np.array(dataX), np.array(dataY)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.885117Z","iopub.execute_input":"2022-07-04T17:41:28.885660Z","iopub.status.idle":"2022-07-04T17:41:28.892386Z","shell.execute_reply.started":"2022-07-04T17:41:28.885632Z","shell.execute_reply":"2022-07-04T17:41:28.891360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_step = 100\nX_train, y_train = create_dataset(train_data, time_step)\nX_test, y_test = create_dataset(test_data, time_step)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:28.893591Z","iopub.execute_input":"2022-07-04T17:41:28.894625Z","iopub.status.idle":"2022-07-04T17:41:29.216286Z","shell.execute_reply.started":"2022-07-04T17:41:28.894590Z","shell.execute_reply":"2022-07-04T17:41:29.215243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.217535Z","iopub.execute_input":"2022-07-04T17:41:29.217873Z","iopub.status.idle":"2022-07-04T17:41:29.222709Z","shell.execute_reply.started":"2022-07-04T17:41:29.217840Z","shell.execute_reply":"2022-07-04T17:41:29.221747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.224080Z","iopub.execute_input":"2022-07-04T17:41:29.224676Z","iopub.status.idle":"2022-07-04T17:41:29.234139Z","shell.execute_reply.started":"2022-07-04T17:41:29.224622Z","shell.execute_reply":"2022-07-04T17:41:29.232895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.235691Z","iopub.execute_input":"2022-07-04T17:41:29.236282Z","iopub.status.idle":"2022-07-04T17:41:29.245251Z","shell.execute_reply.started":"2022-07-04T17:41:29.236245Z","shell.execute_reply":"2022-07-04T17:41:29.244277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.247509Z","iopub.execute_input":"2022-07-04T17:41:29.248081Z","iopub.status.idle":"2022-07-04T17:41:29.256621Z","shell.execute_reply.started":"2022-07-04T17:41:29.248048Z","shell.execute_reply":"2022-07-04T17:41:29.255290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)\nX_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.257974Z","iopub.execute_input":"2022-07-04T17:41:29.258755Z","iopub.status.idle":"2022-07-04T17:41:29.267372Z","shell.execute_reply.started":"2022-07-04T17:41:29.258721Z","shell.execute_reply":"2022-07-04T17:41:29.266478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.269735Z","iopub.execute_input":"2022-07-04T17:41:29.271241Z","iopub.status.idle":"2022-07-04T17:41:29.279810Z","shell.execute_reply.started":"2022-07-04T17:41:29.271212Z","shell.execute_reply":"2022-07-04T17:41:29.278736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Multi Step LSTM is used in the following order as shown below.**","metadata":{}},{"cell_type":"code","source":"model_1 = Sequential([\n    LSTM(units=100, return_sequences=True, input_shape=(100, 1)),\n    Dropout(0.2),\n    LSTM(100, return_sequences=True),\n    Dropout(0.1),\n    LSTM(50, return_sequences=True),\n    Dropout(0.2),\n    Flatten(),\n    Dense(100, activation='relu'),\n    Dropout(0.2),\n    Dense(25, activation=\"relu\"),\n    Dropout(0.1),\n    Dense(1)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:29.281307Z","iopub.execute_input":"2022-07-04T17:41:29.282438Z","iopub.status.idle":"2022-07-04T17:41:32.915526Z","shell.execute_reply.started":"2022-07-04T17:41:29.282313Z","shell.execute_reply":"2022-07-04T17:41:32.914521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:32.917294Z","iopub.execute_input":"2022-07-04T17:41:32.917736Z","iopub.status.idle":"2022-07-04T17:41:32.925869Z","shell.execute_reply.started":"2022-07-04T17:41:32.917699Z","shell.execute_reply":"2022-07-04T17:41:32.924672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model_1, 'model_1.jpg', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:32.927617Z","iopub.execute_input":"2022-07-04T17:41:32.927967Z","iopub.status.idle":"2022-07-04T17:41:34.416523Z","shell.execute_reply.started":"2022-07-04T17:41:32.927931Z","shell.execute_reply":"2022-07-04T17:41:34.415406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.compile(optimizer=\"adam\", loss=\"mean_squared_error\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:34.418716Z","iopub.execute_input":"2022-07-04T17:41:34.419401Z","iopub.status.idle":"2022-07-04T17:41:34.453110Z","shell.execute_reply.started":"2022-07-04T17:41:34.419359Z","shell.execute_reply":"2022-07-04T17:41:34.452290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_1 = model_1.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:41:34.457249Z","iopub.execute_input":"2022-07-04T17:41:34.459510Z","iopub.status.idle":"2022-07-04T17:54:56.088338Z","shell.execute_reply.started":"2022-07-04T17:41:34.459474Z","shell.execute_reply":"2022-07-04T17:54:56.087416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = model_1.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:54:56.090124Z","iopub.execute_input":"2022-07-04T17:54:56.090488Z","iopub.status.idle":"2022-07-04T17:55:06.007524Z","shell.execute_reply.started":"2022-07-04T17:54:56.090453Z","shell.execute_reply":"2022-07-04T17:55:06.006620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.DataFrame(history_1.history).plot(figsize=(10, 7))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:06.010578Z","iopub.execute_input":"2022-07-04T17:55:06.010850Z","iopub.status.idle":"2022-07-04T17:55:06.280136Z","shell.execute_reply.started":"2022-07-04T17:55:06.010825Z","shell.execute_reply":"2022-07-04T17:55:06.279158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The loss obtained from the given model is as shown below.**","metadata":{}},{"cell_type":"code","source":"print(f\"Loss of the model is: {loss*100}%.\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:06.284519Z","iopub.execute_input":"2022-07-04T17:55:06.287206Z","iopub.status.idle":"2022-07-04T17:55:06.297936Z","shell.execute_reply.started":"2022-07-04T17:55:06.287163Z","shell.execute_reply":"2022-07-04T17:55:06.296602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The loss calculated above is 0.006 %, which is quite low.**","metadata":{}},{"cell_type":"code","source":"train_predict = model_1.predict(X_train)\ntest_predict = model_1.predict(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:06.303251Z","iopub.execute_input":"2022-07-04T17:55:06.305551Z","iopub.status.idle":"2022-07-04T17:55:41.510127Z","shell.execute_reply.started":"2022-07-04T17:55:06.305514Z","shell.execute_reply":"2022-07-04T17:55:41.509114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I predicted the values for stock market using the model as shown above, and converted them back to the original values, by inverse transfrom of MinMaxScaler.**","metadata":{}},{"cell_type":"code","source":"train_p = scaler.inverse_transform(train_predict) \ntest_p = scaler.inverse_transform(test_predict)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.511733Z","iopub.execute_input":"2022-07-04T17:55:41.512159Z","iopub.status.idle":"2022-07-04T17:55:41.520039Z","shell.execute_reply.started":"2022-07-04T17:55:41.512115Z","shell.execute_reply":"2022-07-04T17:55:41.519034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I calculated the square root of mean squared error of predicted values and the real values, and found the mean squared error for train set is 2705.614 and test set is 3368.504.**","metadata":{}},{"cell_type":"code","source":"import math\nfrom sklearn.metrics import mean_squared_error\nmath.sqrt(mean_squared_error(y_train, train_p))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.521597Z","iopub.execute_input":"2022-07-04T17:55:41.521944Z","iopub.status.idle":"2022-07-04T17:55:41.536060Z","shell.execute_reply.started":"2022-07-04T17:55:41.521911Z","shell.execute_reply":"2022-07-04T17:55:41.535101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nfrom sklearn.metrics import mean_squared_error\nmath.sqrt(mean_squared_error(y_test, test_p))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.538162Z","iopub.execute_input":"2022-07-04T17:55:41.539263Z","iopub.status.idle":"2022-07-04T17:55:41.548613Z","shell.execute_reply.started":"2022-07-04T17:55:41.539227Z","shell.execute_reply":"2022-07-04T17:55:41.547284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now, I have to test our model on test stock market. I have also calculated the adjusted close price of the given test dataset.**","metadata":{}},{"cell_type":"code","source":"dataset_test = \"/kaggle/input/jpx-tokyo-stock-exchange-prediction\"\ntest_file = \"example_test_files/stock_prices.csv\"\ntest_all_file = os.path.join(dataset_test, test_file)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.550077Z","iopub.execute_input":"2022-07-04T17:55:41.550761Z","iopub.status.idle":"2022-07-04T17:55:41.556554Z","shell.execute_reply.started":"2022-07-04T17:55:41.550726Z","shell.execute_reply":"2022-07-04T17:55:41.555432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t = pd.read_csv(test_all_file)\ndf_t.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.558243Z","iopub.execute_input":"2022-07-04T17:55:41.559260Z","iopub.status.idle":"2022-07-04T17:55:41.595243Z","shell.execute_reply.started":"2022-07-04T17:55:41.559224Z","shell.execute_reply":"2022-07-04T17:55:41.594389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.tail()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.597147Z","iopub.execute_input":"2022-07-04T17:55:41.597493Z","iopub.status.idle":"2022-07-04T17:55:41.617911Z","shell.execute_reply.started":"2022-07-04T17:55:41.597457Z","shell.execute_reply":"2022-07-04T17:55:41.616918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_t)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.619532Z","iopub.execute_input":"2022-07-04T17:55:41.620296Z","iopub.status.idle":"2022-07-04T17:55:41.626629Z","shell.execute_reply.started":"2022-07-04T17:55:41.620221Z","shell.execute_reply":"2022-07-04T17:55:41.625597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df_t['Close'])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.630907Z","iopub.execute_input":"2022-07-04T17:55:41.631179Z","iopub.status.idle":"2022-07-04T17:55:41.818704Z","shell.execute_reply.started":"2022-07-04T17:55:41.631152Z","shell.execute_reply":"2022-07-04T17:55:41.817818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t = adjusted_price(df_t)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:41.820210Z","iopub.execute_input":"2022-07-04T17:55:41.820563Z","iopub.status.idle":"2022-07-04T17:55:51.402911Z","shell.execute_reply.started":"2022-07-04T17:55:41.820528Z","shell.execute_reply":"2022-07-04T17:55:51.401858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.407760Z","iopub.execute_input":"2022-07-04T17:55:51.410061Z","iopub.status.idle":"2022-07-04T17:55:51.433806Z","shell.execute_reply.started":"2022-07-04T17:55:51.410021Z","shell.execute_reply":"2022-07-04T17:55:51.432810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df_t[\"AdjustedClose\"])\nplt.xlabel(\"Year\")\nplt.ylabel(\"Adjusted_Close_Value\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.438644Z","iopub.execute_input":"2022-07-04T17:55:51.441088Z","iopub.status.idle":"2022-07-04T17:55:51.763492Z","shell.execute_reply.started":"2022-07-04T17:55:51.441049Z","shell.execute_reply":"2022-07-04T17:55:51.762522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The graph is quite incomprehensible and I have taken only 1000 values, for prediction.**","metadata":{}},{"cell_type":"code","source":"df_t_1 = df_t[:1000]","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.767927Z","iopub.execute_input":"2022-07-04T17:55:51.770208Z","iopub.status.idle":"2022-07-04T17:55:51.777061Z","shell.execute_reply.started":"2022-07-04T17:55:51.770170Z","shell.execute_reply":"2022-07-04T17:55:51.776008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_t_1 = df_t_1.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.778557Z","iopub.execute_input":"2022-07-04T17:55:51.779267Z","iopub.status.idle":"2022-07-04T17:55:51.792254Z","shell.execute_reply.started":"2022-07-04T17:55:51.779225Z","shell.execute_reply":"2022-07-04T17:55:51.791150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = df_t[\"AdjustedClose\"][:1000]\nlen(df3)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.793586Z","iopub.execute_input":"2022-07-04T17:55:51.794280Z","iopub.status.idle":"2022-07-04T17:55:51.803556Z","shell.execute_reply.started":"2022-07-04T17:55:51.794245Z","shell.execute_reply":"2022-07-04T17:55:51.802313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.805090Z","iopub.execute_input":"2022-07-04T17:55:51.806302Z","iopub.status.idle":"2022-07-04T17:55:51.821754Z","shell.execute_reply.started":"2022-07-04T17:55:51.806262Z","shell.execute_reply":"2022-07-04T17:55:51.820266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df3)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.826656Z","iopub.execute_input":"2022-07-04T17:55:51.828892Z","iopub.status.idle":"2022-07-04T17:55:51.838382Z","shell.execute_reply.started":"2022-07-04T17:55:51.828856Z","shell.execute_reply":"2022-07-04T17:55:51.837385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.842848Z","iopub.execute_input":"2022-07-04T17:55:51.845756Z","iopub.status.idle":"2022-07-04T17:55:51.855992Z","shell.execute_reply.started":"2022-07-04T17:55:51.845718Z","shell.execute_reply":"2022-07-04T17:55:51.854814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3.plot(figsize=(12, 4), legend=True, linestyle=\"--\", marker=\"o\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:51.860892Z","iopub.execute_input":"2022-07-04T17:55:51.861674Z","iopub.status.idle":"2022-07-04T17:55:52.171022Z","shell.execute_reply.started":"2022-07-04T17:55:51.861638Z","shell.execute_reply":"2022-07-04T17:55:52.168881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(df3, bins=100, color=\"magenta\")","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.177395Z","iopub.execute_input":"2022-07-04T17:55:52.179273Z","iopub.status.idle":"2022-07-04T17:55:52.755045Z","shell.execute_reply.started":"2022-07-04T17:55:52.179230Z","shell.execute_reply":"2022-07-04T17:55:52.754150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.759428Z","iopub.execute_input":"2022-07-04T17:55:52.761663Z","iopub.status.idle":"2022-07-04T17:55:52.775257Z","shell.execute_reply.started":"2022-07-04T17:55:52.761625Z","shell.execute_reply":"2022-07-04T17:55:52.774147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df3 = df3.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.780441Z","iopub.execute_input":"2022-07-04T17:55:52.782949Z","iopub.status.idle":"2022-07-04T17:55:52.790233Z","shell.execute_reply.started":"2022-07-04T17:55:52.782911Z","shell.execute_reply":"2022-07-04T17:55:52.789087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df3)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.794982Z","iopub.execute_input":"2022-07-04T17:55:52.797556Z","iopub.status.idle":"2022-07-04T17:55:52.807515Z","shell.execute_reply.started":"2022-07-04T17:55:52.797520Z","shell.execute_reply":"2022-07-04T17:55:52.806472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler(feature_range=(0,1))\ndf3 = scaler.fit_transform(np.array(df3).reshape(-1, 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.810030Z","iopub.execute_input":"2022-07-04T17:55:52.811994Z","iopub.status.idle":"2022-07-04T17:55:52.823431Z","shell.execute_reply.started":"2022-07-04T17:55:52.811947Z","shell.execute_reply":"2022-07-04T17:55:52.822324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_step = 100\nX_test_1, y_test_1 = create_dataset(df3, time_step)\nX_test_1","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.826196Z","iopub.execute_input":"2022-07-04T17:55:52.827179Z","iopub.status.idle":"2022-07-04T17:55:52.845289Z","shell.execute_reply.started":"2022-07-04T17:55:52.827142Z","shell.execute_reply":"2022-07-04T17:55:52.844412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.850279Z","iopub.execute_input":"2022-07-04T17:55:52.850927Z","iopub.status.idle":"2022-07-04T17:55:52.859235Z","shell.execute_reply.started":"2022-07-04T17:55:52.850892Z","shell.execute_reply":"2022-07-04T17:55:52.858294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_1 = X_test_1.reshape(X_test_1.shape[0], X_test_1.shape[1], 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.860905Z","iopub.execute_input":"2022-07-04T17:55:52.861655Z","iopub.status.idle":"2022-07-04T17:55:52.869373Z","shell.execute_reply.started":"2022-07-04T17:55:52.861618Z","shell.execute_reply":"2022-07-04T17:55:52.868399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.871022Z","iopub.execute_input":"2022-07-04T17:55:52.872321Z","iopub.status.idle":"2022-07-04T17:55:52.888092Z","shell.execute_reply.started":"2022-07-04T17:55:52.872236Z","shell.execute_reply":"2022-07-04T17:55:52.887065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict_1 = model_1.predict(X_test_1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:52.889226Z","iopub.execute_input":"2022-07-04T17:55:52.889592Z","iopub.status.idle":"2022-07-04T17:55:53.165436Z","shell.execute_reply.started":"2022-07-04T17:55:52.889558Z","shell.execute_reply":"2022-07-04T17:55:53.164376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_1 = scaler.inverse_transform(test_predict_1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.171469Z","iopub.execute_input":"2022-07-04T17:55:53.173809Z","iopub.status.idle":"2022-07-04T17:55:53.180862Z","shell.execute_reply.started":"2022-07-04T17:55:53.173772Z","shell.execute_reply":"2022-07-04T17:55:53.179843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test_1 = y_test_1.reshape(897,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.183146Z","iopub.execute_input":"2022-07-04T17:55:53.185412Z","iopub.status.idle":"2022-07-04T17:55:53.193729Z","shell.execute_reply.started":"2022-07-04T17:55:53.185374Z","shell.execute_reply":"2022-07-04T17:55:53.192720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.195326Z","iopub.execute_input":"2022-07-04T17:55:53.196034Z","iopub.status.idle":"2022-07-04T17:55:53.216786Z","shell.execute_reply.started":"2022-07-04T17:55:53.195998Z","shell.execute_reply":"2022-07-04T17:55:53.215873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Square root mean squared error is found to be 3094.5207 on the test values for stock price prediction.**","metadata":{}},{"cell_type":"code","source":"import math\nfrom sklearn.metrics import mean_squared_error\nmath.sqrt(mean_squared_error(y_test_1, test_p_1))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.233501Z","iopub.execute_input":"2022-07-04T17:55:53.235958Z","iopub.status.idle":"2022-07-04T17:55:53.247066Z","shell.execute_reply.started":"2022-07-04T17:55:53.235922Z","shell.execute_reply":"2022-07-04T17:55:53.246168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nback = 100\n\ntestPredictPlot = np.empty_like(df3)\ntestPredictPlot[:, :] = np.nan\ntestPredictPlot[back:len(test_p_1)+back, :] = test_p_1\n\n\nplt.plot(scaler.inverse_transform(df3))\nplt.plot(testPredictPlot)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.251593Z","iopub.execute_input":"2022-07-04T17:55:53.254148Z","iopub.status.idle":"2022-07-04T17:55:53.459286Z","shell.execute_reply.started":"2022-07-04T17:55:53.254114Z","shell.execute_reply":"2022-07-04T17:55:53.458352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_1 = np.squeeze(test_p_1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.463793Z","iopub.execute_input":"2022-07-04T17:55:53.466085Z","iopub.status.idle":"2022-07-04T17:55:53.472841Z","shell.execute_reply.started":"2022-07-04T17:55:53.466046Z","shell.execute_reply":"2022-07-04T17:55:53.471476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_1 = pd.DataFrame(test_p_1, columns = ['Stock_Price'])","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.477613Z","iopub.execute_input":"2022-07-04T17:55:53.480287Z","iopub.status.idle":"2022-07-04T17:55:53.487574Z","shell.execute_reply.started":"2022-07-04T17:55:53.480248Z","shell.execute_reply":"2022-07-04T17:55:53.486325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_1 = np.squeeze(test_p_1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.492657Z","iopub.execute_input":"2022-07-04T17:55:53.494946Z","iopub.status.idle":"2022-07-04T17:55:53.501789Z","shell.execute_reply.started":"2022-07-04T17:55:53.494910Z","shell.execute_reply":"2022-07-04T17:55:53.500844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"te_p_1 = pd.DataFrame({'id':list(range(0, len(test_p_1-1))), 'Stock_Price': test_p_1})\nte_p_1","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.504742Z","iopub.execute_input":"2022-07-04T17:55:53.505039Z","iopub.status.idle":"2022-07-04T17:55:53.529280Z","shell.execute_reply.started":"2022-07-04T17:55:53.505016Z","shell.execute_reply":"2022-07-04T17:55:53.528288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Lets predict for 2000 datasets**","metadata":{}},{"cell_type":"code","source":"df4 = df_t[\"AdjustedClose\"]\nlen(df4)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.533583Z","iopub.execute_input":"2022-07-04T17:55:53.535901Z","iopub.status.idle":"2022-07-04T17:55:53.546879Z","shell.execute_reply.started":"2022-07-04T17:55:53.535863Z","shell.execute_reply":"2022-07-04T17:55:53.545838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df4.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.551178Z","iopub.execute_input":"2022-07-04T17:55:53.553904Z","iopub.status.idle":"2022-07-04T17:55:53.569217Z","shell.execute_reply.started":"2022-07-04T17:55:53.553870Z","shell.execute_reply":"2022-07-04T17:55:53.568291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df4 = scaler.fit_transform(np.array(df4).reshape(-1, 1))","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.571086Z","iopub.execute_input":"2022-07-04T17:55:53.571906Z","iopub.status.idle":"2022-07-04T17:55:53.577593Z","shell.execute_reply.started":"2022-07-04T17:55:53.571868Z","shell.execute_reply":"2022-07-04T17:55:53.576407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"time_step = 100\nX_test_2, y_test_2 = create_dataset(df4, time_step)\nX_test_2","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.579164Z","iopub.execute_input":"2022-07-04T17:55:53.579807Z","iopub.status.idle":"2022-07-04T17:55:53.602827Z","shell.execute_reply.started":"2022-07-04T17:55:53.579772Z","shell.execute_reply":"2022-07-04T17:55:53.601920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_2 = X_test_2.reshape(X_test_2.shape[0], X_test_2.shape[1], 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:55:53.604384Z","iopub.execute_input":"2022-07-04T17:55:53.604986Z","iopub.status.idle":"2022-07-04T17:55:53.610646Z","shell.execute_reply.started":"2022-07-04T17:55:53.604952Z","shell.execute_reply":"2022-07-04T17:55:53.609615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict_2 = model_1.predict(X_test_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:56:23.334012Z","iopub.execute_input":"2022-07-04T17:56:23.334489Z","iopub.status.idle":"2022-07-04T17:56:24.078287Z","shell.execute_reply.started":"2022-07-04T17:56:23.334450Z","shell.execute_reply":"2022-07-04T17:56:24.077269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_2 = scaler.inverse_transform(test_predict_2)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:56:24.594130Z","iopub.execute_input":"2022-07-04T17:56:24.595107Z","iopub.status.idle":"2022-07-04T17:56:24.601034Z","shell.execute_reply.started":"2022-07-04T17:56:24.595073Z","shell.execute_reply":"2022-07-04T17:56:24.599941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_2 = test_p_2.reshape(3899,1)","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:56:59.092376Z","iopub.execute_input":"2022-07-04T17:56:59.093300Z","iopub.status.idle":"2022-07-04T17:56:59.098551Z","shell.execute_reply.started":"2022-07-04T17:56:59.093263Z","shell.execute_reply":"2022-07-04T17:56:59.097529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_p_2 = np.squeeze(test_p_2)\ntest_p_2 = pd.DataFrame(test_p_2, columns = ['Stock_Price'])\ntest_p_2 = np.squeeze(test_p_2)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:57:53.950746Z","iopub.execute_input":"2022-07-04T17:57:53.951561Z","iopub.status.idle":"2022-07-04T17:57:53.957596Z","shell.execute_reply.started":"2022-07-04T17:57:53.951524Z","shell.execute_reply":"2022-07-04T17:57:53.956551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'id':list(range(0, len(test_p_2-1))), 'Stock_Price': test_p_2})\nmy_submission","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:57:55.273551Z","iopub.execute_input":"2022-07-04T17:57:55.273900Z","iopub.status.idle":"2022-07-04T17:57:55.290508Z","shell.execute_reply.started":"2022-07-04T17:57:55.273871Z","shell.execute_reply":"2022-07-04T17:57:55.289443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission.dropna()","metadata":{"execution":{"iopub.status.busy":"2022-07-04T17:58:28.569358Z","iopub.execute_input":"2022-07-04T17:58:28.569718Z","iopub.status.idle":"2022-07-04T17:58:28.586520Z","shell.execute_reply.started":"2022-07-04T17:58:28.569690Z","shell.execute_reply":"2022-07-04T17:58:28.585527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **\"model_1\" is the trained model with very low losses and high accuracy, you could use that to predict your stock prices.**","metadata":{}}]}