Advertisement
sepi0l

dl_stock

Apr 25th, 2024
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 7.64 KB | None | 0 0
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": null,
  6.    "metadata": {},
  7.    "outputs": [],
  8.    "source": [
  9.     "import numpy as np\n",
  10.     "import matplotlib.pyplot as plt\n",
  11.     "import pandas as pd\n",
  12.     "from sklearn.preprocessing import MinMaxScaler\n",
  13.     "from tensorflow.keras.models import Sequential\n",
  14.     "from tensorflow.keras.layers import LSTM\n",
  15.     "from tensorflow.keras.layers import Dense\n",
  16.     "from tensorflow.keras.layers import Dropout"
  17.    ]
  18.   },
  19.   {
  20.    "cell_type": "code",
  21.    "execution_count": null,
  22.    "metadata": {},
  23.    "outputs": [],
  24.    "source": [
  25.     "dataset_train = pd.read_csv('Google_Stock_Price_Train.csv')"
  26.    ]
  27.   },
  28.   {
  29.    "cell_type": "code",
  30.    "execution_count": null,
  31.    "metadata": {},
  32.    "outputs": [],
  33.    "source": [
  34.     "dataset_train.head(5)"
  35.    ]
  36.   },
  37.   {
  38.    "cell_type": "code",
  39.    "execution_count": null,
  40.    "metadata": {},
  41.    "outputs": [],
  42.    "source": [
  43.     "training_set = dataset_train.iloc[:, 1: 2].values"
  44.    ]
  45.   },
  46.   {
  47.    "cell_type": "code",
  48.    "execution_count": null,
  49.    "metadata": {},
  50.    "outputs": [],
  51.    "source": [
  52.     "training_set.shape"
  53.    ]
  54.   },
  55.   {
  56.    "cell_type": "code",
  57.    "execution_count": null,
  58.    "metadata": {},
  59.    "outputs": [],
  60.    "source": [
  61.     "sc = MinMaxScaler(feature_range = (0, 1))\n",
  62.     "#fit: get min/max of train data\n",
  63.     "training_set_scaled = sc.fit_transform(training_set)"
  64.    ]
  65.   },
  66.   {
  67.    "cell_type": "code",
  68.    "execution_count": null,
  69.    "metadata": {},
  70.    "outputs": [],
  71.    "source": [
  72.     "## 60 timesteps and 1 output\n",
  73.     "X_train = []\n",
  74.     "y_train = []\n",
  75.     "for i in range(60, len(training_set_scaled)):\n",
  76.     " X_train.append(training_set_scaled[i-60: i, 0])\n",
  77.     " y_train.append(training_set_scaled[i, 0])\n",
  78.     "X_train, y_train = np.array(X_train), np.array(y_train)"
  79.    ]
  80.   },
  81.   {
  82.    "cell_type": "code",
  83.    "execution_count": null,
  84.    "metadata": {},
  85.    "outputs": [],
  86.    "source": [
  87.     "X_train.shape"
  88.    ]
  89.   },
  90.   {
  91.    "cell_type": "code",
  92.    "execution_count": null,
  93.    "metadata": {},
  94.    "outputs": [],
  95.    "source": [
  96.     "y_train.shape"
  97.    ]
  98.   },
  99.   {
  100.    "cell_type": "code",
  101.    "execution_count": null,
  102.    "metadata": {},
  103.    "outputs": [],
  104.    "source": [
  105.     "#data reshaping\n",
  106.     "X_train = np.reshape(X_train, newshape =\n",
  107.     " (X_train.shape[0], X_train.shape[1], 1))"
  108.    ]
  109.   },
  110.   {
  111.    "cell_type": "code",
  112.    "execution_count": null,
  113.    "metadata": {},
  114.    "outputs": [],
  115.    "source": [
  116.     "X_train.shape"
  117.    ]
  118.   },
  119.   {
  120.    "cell_type": "code",
  121.    "execution_count": null,
  122.    "metadata": {},
  123.    "outputs": [],
  124.    "source": [
  125.     "plt.figure(figsize=(18, 8))\n",
  126.     "plt.plot(dataset_train['Open'])\n",
  127.     "plt.title(\"Google Stock Open Prices\")\n",
  128.     "plt.xlabel(\"Time (oldest -> latest)\")\n",
  129.     "plt.ylabel(\"Stock Open Price\")\n",
  130.     "plt.show()"
  131.    ]
  132.   },
  133.   {
  134.    "cell_type": "code",
  135.    "execution_count": null,
  136.    "metadata": {},
  137.    "outputs": [],
  138.    "source": [
  139.     "plt.figure(figsize=(18, 8))\n",
  140.     "plt.plot(dataset_train['Low'])\n",
  141.     "plt.title(\"Google Stock Low Prices\")\n",
  142.     "plt.xlabel(\"Time (oldest -> latest)\")\n",
  143.     "plt.ylabel(\"Stock Lowest Price\")\n",
  144.     "plt.show()"
  145.    ]
  146.   },
  147.   {
  148.    "cell_type": "code",
  149.    "execution_count": null,
  150.    "metadata": {},
  151.    "outputs": [],
  152.    "source": [
  153.     "regressor = Sequential()\n",
  154.     "#add 1st lstm layer\n",
  155.     "regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))\n",
  156.     "regressor.add(Dropout(rate = 0.2))\n",
  157.     "##add 2nd lstm layer: 50 neurons\n",
  158.     "regressor.add(LSTM(units = 50, return_sequences = True))\n",
  159.     "regressor.add(Dropout(rate = 0.2))\n",
  160.     "##add 3rd lstm layer\n",
  161.     "regressor.add(LSTM(units = 50, return_sequences = True))\n",
  162.     "regressor.add(Dropout(rate = 0.2))\n",
  163.     "##add 4th lstm layer\n",
  164.     "regressor.add(LSTM(units = 50, return_sequences = False))\n",
  165.     "regressor.add(Dropout(rate = 0.2))\n",
  166.     "##add output layer\n",
  167.     "regressor.add(Dense(units = 1))"
  168.    ]
  169.   },
  170.   {
  171.    "cell_type": "code",
  172.    "execution_count": null,
  173.    "metadata": {},
  174.    "outputs": [],
  175.    "source": [
  176.     "regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')"
  177.    ]
  178.   },
  179.   {
  180.    "cell_type": "code",
  181.    "execution_count": null,
  182.    "metadata": {},
  183.    "outputs": [],
  184.    "source": [
  185.     "regressor.fit(x = X_train, y = y_train, batch_size = 32, epochs = 100)"
  186.    ]
  187.   },
  188.   {
  189.    "cell_type": "code",
  190.    "execution_count": null,
  191.    "metadata": {},
  192.    "outputs": [],
  193.    "source": [
  194.     "dataset_test = pd.read_csv('Google_Stock_Price_Test.csv')"
  195.    ]
  196.   },
  197.   {
  198.    "cell_type": "code",
  199.    "execution_count": null,
  200.    "metadata": {},
  201.    "outputs": [],
  202.    "source": [
  203.     "dataset_test.head()"
  204.    ]
  205.   },
  206.   {
  207.    "cell_type": "code",
  208.    "execution_count": null,
  209.    "metadata": {},
  210.    "outputs": [],
  211.    "source": [
  212.     "\n",
  213.     "#keras only takes numpy array\n",
  214.     "real_stock_price = dataset_test.iloc[:, 1: 2].values\n",
  215.     "real_stock_price.shape\n"
  216.    ]
  217.   },
  218.   {
  219.    "cell_type": "code",
  220.    "execution_count": null,
  221.    "metadata": {},
  222.    "outputs": [],
  223.    "source": [
  224.     "dataset_total = pd.concat((dataset_train['Open'], dataset_test['Open']),\n",
  225.     " axis = 0)\n",
  226.     "##use .values to make numpy array\n",
  227.     "inputs = dataset_total[len(dataset_total) - len(dataset_test) - 60:].values"
  228.    ]
  229.   },
  230.   {
  231.    "cell_type": "code",
  232.    "execution_count": null,
  233.    "metadata": {},
  234.    "outputs": [],
  235.    "source": [
  236.     "#reshape data to only have 1 col\n",
  237.     "inputs = inputs.reshape(-1, 1)\n",
  238.     "#scale input\n",
  239.     "inputs = sc.transform(inputs)"
  240.    ]
  241.   },
  242.   {
  243.    "cell_type": "code",
  244.    "execution_count": null,
  245.    "metadata": {},
  246.    "outputs": [],
  247.    "source": [
  248.     "len(inputs)"
  249.    ]
  250.   },
  251.   {
  252.    "cell_type": "code",
  253.    "execution_count": null,
  254.    "metadata": {},
  255.    "outputs": [],
  256.    "source": [
  257.     "X_test = []\n",
  258.     "for i in range(60, len(inputs)):\n",
  259.     " X_test.append(inputs[i-60:i, 0])\n",
  260.     "X_test = np.array(X_test)\n",
  261.     "#add dimension of indicator\n",
  262.     "X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))"
  263.    ]
  264.   },
  265.   {
  266.    "cell_type": "code",
  267.    "execution_count": null,
  268.    "metadata": {},
  269.    "outputs": [],
  270.    "source": [
  271.     "X_test.shape"
  272.    ]
  273.   },
  274.   {
  275.    "cell_type": "code",
  276.    "execution_count": null,
  277.    "metadata": {},
  278.    "outputs": [],
  279.    "source": [
  280.     "predicted_stock_price = regressor.predict(X_test)"
  281.    ]
  282.   },
  283.   {
  284.    "cell_type": "code",
  285.    "execution_count": null,
  286.    "metadata": {},
  287.    "outputs": [],
  288.    "source": [
  289.     "#inverse the scaled value\n",
  290.     "predicted_stock_price = sc.inverse_transform(predicted_stock_price)"
  291.    ]
  292.   },
  293.   {
  294.    "cell_type": "code",
  295.    "execution_count": null,
  296.    "metadata": {},
  297.    "outputs": [],
  298.    "source": [
  299.     "plt.plot(real_stock_price, color = 'red', label = 'Real price')\n",
  300.     "plt.plot(predicted_stock_price, color = 'blue', label = 'Predicted price')\n",
  301.     "plt.title('Google price prediction')\n",
  302.     "plt.xlabel('Time')\n",
  303.     "plt.ylabel('Price')\n",
  304.     "plt.legend()\n",
  305.     "plt.show()"
  306.    ]
  307.   }
  308.  ],
  309.  "metadata": {
  310.   "kernelspec": {
  311.    "display_name": "Python 3 (ipykernel)",
  312.    "language": "python",
  313.    "name": "python3"
  314.   },
  315.   "language_info": {
  316.    "codemirror_mode": {
  317.     "name": "ipython",
  318.     "version": 3
  319.    },
  320.    "file_extension": ".py",
  321.    "mimetype": "text/x-python",
  322.    "name": "python",
  323.    "nbconvert_exporter": "python",
  324.    "pygments_lexer": "ipython3",
  325.    "version": "3.11.8"
  326.   }
  327.  },
  328.  "nbformat": 4,
  329.  "nbformat_minor": 4
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement