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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f90cb2c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "collapsed_sections": [
        "Z3N2WMYNV-qX"
      ],
      "authorship_tag": "ABX9TyOuk8MIfThoeWnRbBQlPf+h",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/jsebdev/Stock_Predictor/blob/main/stock_predictor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "project_path = '/content/drive/MyDrive/projects/Stock_Predicter'\n",
        "%cd $project_path"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Xr3Qozgfktoc",
        "outputId": "e80033fb-a41f-438f-fc90-60bc0317d5d3"
      },
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Mounted at /content/drive\n",
            "/content/drive/MyDrive/projects/Stock_Predicter\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "e8SQqogMQYLh"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pandas as pd\n",
        "import pandas_datareader as web\n",
        "import datetime as dt\n",
        "import yfinance as yfin\n",
        "import tensorflow as tf\n",
        "import os\n",
        "import re\n",
        "\n",
        "from sklearn.preprocessing import MinMaxScaler\n",
        "from tensorflow.keras.models import Sequential\n",
        "from tensorflow.keras.layers import Dense, Dropout, LSTM\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Get Data"
      ],
      "metadata": {
        "id": "5vO8pty3VwkG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Select a company for now\n",
        "ticker = 'AAPL'\n",
        "\n",
        "start = dt.datetime(2013,1,1)\n",
        "end = dt.datetime(2023,4,5)"
      ],
      "metadata": {
        "id": "O6dtJpJwS5Eg"
      },
      "execution_count": 93,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "yfin.pdr_override()\n",
        "data = web.data.get_data_yahoo(ticker, start, end)\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "LwPyk8Uh-Zz_",
        "outputId": "63953807-ca2e-4e18-c571-a6bcc4f8db5d"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\r[*********************100%***********************]  1 of 1 completed\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Preprocess_data"
      ],
      "metadata": {
        "id": "SSuS9OONV5-a"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def normalize_data(data, relative_to_previous=True, scaler=None):\n",
        "  def substract_to_values(data, value):\n",
        "    df_copy = pd.DataFrame.copy(data)\n",
        "    df_copy[['Open', 'High', 'Low', 'Close', 'Adj Close']] = df_copy[['Open', 'High', 'Low', 'Close', 'Adj Close']] - value\n",
        "    return df_copy\n",
        "  if relative_to_previous:\n",
        "    the_data = pd.DataFrame(substract_to_values(data.iloc[0], data.iloc[0]['Open'])).T\n",
        "    # the_data = substract_to_values(data.iloc[0], data.iloc[0]['Open']).to_frame().T # This is the same as the previous line\n",
        "    for i in range(1,len(data)):\n",
        "      the_data = pd.concat((the_data, substract_to_values(data.iloc[i], data.iloc[i-1]['Close']).to_frame().T))\n",
        "  else:\n",
        "    the_data = pd.DataFrame.copy(data)\n",
        "  \n",
        "  if scaler is None:\n",
        "    # Create the scaler\n",
        "    values = the_data.values\n",
        "    # print('values')\n",
        "    # print(values)\n",
        "    max_value = np.max(values[:,:-1])\n",
        "    # print(max_value)\n",
        "    min_value = np.min(values[:,:-1])\n",
        "    # print(min_value)\n",
        "    max_volume = np.max(values[:,-1])\n",
        "    min_volume = np.min(values[:,-1])\n",
        "    # print(max_volume, min_volume)\n",
        "    def scaler(data):\n",
        "      values = data.values\n",
        "      # print(values)\n",
        "      values[:,:-1] = (values[:,:-1] - min_value) / (max_value-min_value) * 2 - 1\n",
        "      values[:,-1] = (values[:,-1] - min_volume) / (max_volume-min_volume) * 2 - 1\n",
        "      # print(values)\n",
        "      return data\n",
        "    def anti_scaler(values):\n",
        "      decoded_values = (values + 1) * (max_value-min_value) / 2 + min_value  \n",
        "      return decoded_values\n",
        "  \n",
        "  normalized_data = scaler(the_data)\n",
        "\n",
        "  return normalized_data, scaler, anti_scaler\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "v9RoqzBvtrOb"
      },
      "execution_count": 111,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "norm_data, the_scaler, the_decoder = normalize_data(data, relative_to_previous=True)\n",
        "#todo: save the_scaler somehow to use in new runtimes"
      ],
      "metadata": {
        "id": "-kgo__Q3hw1_"
      },
      "execution_count": 112,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "len(norm_data)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "A1L8giqcsutX",
        "outputId": "0aaf515b-3835-432c-b882-c2111a221ed4"
      },
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2583"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "prediction_days = 100\n",
        "\n",
        "x_train_list = []\n",
        "y_train_list = []\n",
        "\n",
        "for i in range(prediction_days, len(norm_data)):\n",
        "  x_train_list.append(norm_data[i-prediction_days:i])\n",
        "  y_train_list.append(norm_data.iloc[i].values[0:4])\n",
        "\n",
        "x_train = np.array(x_train_list)\n",
        "y_train = np.array(y_train_list)"
      ],
      "metadata": {
        "id": "jMXkRAYFomHM"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(x_train.shape)\n",
        "print(y_train.shape)\n",
        "print(x_train.shape[1:])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "G7oMd1fRyOYt",
        "outputId": "2094c403-096d-4f3a-9b15-bae0fbb7bf11"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2483, 100, 6)\n",
            "(2483, 4)\n",
            "(100, 6)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Model"
      ],
      "metadata": {
        "id": "Z3N2WMYNV-qX"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Create Model"
      ],
      "metadata": {
        "id": "emDyvzVUp5KJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def create_model():\n",
        "  model = Sequential()\n",
        "  # model.add(LSTM(units=112, return_sequences=True, input_shape=(x_train.shape[1:])))\n",
        "  model.add(LSTM(units=112, return_sequences=True, input_shape=(None,x_train.shape[-1],)))\n",
        "  model.add(Dropout(0.2))\n",
        "  model.add(LSTM(units=112, return_sequences=True))\n",
        "  model.add(Dropout(0.2))\n",
        "  model.add(LSTM(units=50))\n",
        "  model.add(Dropout(0.2))\n",
        "  model.add(Dense(units=4))\n",
        "  return model\n",
        "\n",
        "model = create_model()\n",
        "print(model.summary())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "GXhYAKzXVfku",
        "outputId": "c54da788-6e82-4679-df1f-d3e89a20d228"
      },
      "execution_count": 66,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model: \"sequential_1\"\n",
            "_________________________________________________________________\n",
            " Layer (type)                Output Shape              Param #   \n",
            "=================================================================\n",
            " lstm_3 (LSTM)               (None, None, 112)         53312     \n",
            "                                                                 \n",
            " dropout_3 (Dropout)         (None, None, 112)         0         \n",
            "                                                                 \n",
            " lstm_4 (LSTM)               (None, None, 112)         100800    \n",
            "                                                                 \n",
            " dropout_4 (Dropout)         (None, None, 112)         0         \n",
            "                                                                 \n",
            " lstm_5 (LSTM)               (None, 50)                32600     \n",
            "                                                                 \n",
            " dropout_5 (Dropout)         (None, 50)                0         \n",
            "                                                                 \n",
            " dense_1 (Dense)             (None, 4)                 204       \n",
            "                                                                 \n",
            "=================================================================\n",
            "Total params: 186,916\n",
            "Trainable params: 186,916\n",
            "Non-trainable params: 0\n",
            "_________________________________________________________________\n",
            "None\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "model.compile(optimizer='adam', loss='mean_squared_error')"
      ],
      "metadata": {
        "id": "ZhoWj_XeXQws"
      },
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Create checkpoint callback"
      ],
      "metadata": {
        "id": "XU0vc4n8p92L"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Directory where the checkpoints will be saved\n",
        "checkpoint_dir = './training_checkpoints_'+dt.datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
        "# Name of the checkpoint files\n",
        "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt_epoch{epoch}_loss{loss}\")\n",
        "\n",
        "checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(\n",
        "    filepath=checkpoint_prefix,\n",
        "    save_weights_only=True)"
      ],
      "metadata": {
        "id": "M5MBAB1-qCZr"
      },
      "execution_count": 35,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Model Train"
      ],
      "metadata": {
        "id": "65QbfffusPoJ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "print(x_train.shape)\n",
        "print(y_train.shape)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HDT9XPXHvqyN",
        "outputId": "60938333-8afe-4b80-9af3-37bca3d67f83"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(2483, 100, 6)\n",
            "(2483, 4)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y_train[-2]"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "F1wZkJMh3XNH",
        "outputId": "37a023db-0727-434a-85be-141c3c377907"
      },
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([ 0.02002301,  0.0391905 , -0.09898045, -0.05744885])"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "model.fit(x_train, y_train, epochs=25, batch_size=32, callbacks=[checkpoint_callback])\n"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9Ccc_Ej2TmYO",
        "outputId": "235efc3b-616b-4e57-fb87-07efcb377e8e"
      },
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/25\n",
            "78/78 [==============================] - 31s 395ms/step - loss: 0.0117\n",
            "Epoch 2/25\n",
            "78/78 [==============================] - 31s 394ms/step - loss: 0.0111\n",
            "Epoch 3/25\n",
            "78/78 [==============================] - 33s 429ms/step - loss: 0.0109\n",
            "Epoch 4/25\n",
            "78/78 [==============================] - 31s 396ms/step - loss: 0.0109\n",
            "Epoch 5/25\n",
            "78/78 [==============================] - 31s 398ms/step - loss: 0.0108\n",
            "Epoch 6/25\n",
            "78/78 [==============================] - 31s 400ms/step - loss: 0.0108\n",
            "Epoch 7/25\n",
            "78/78 [==============================] - 32s 405ms/step - loss: 0.0108\n",
            "Epoch 8/25\n",
            "78/78 [==============================] - 31s 394ms/step - loss: 0.0108\n",
            "Epoch 9/25\n",
            "78/78 [==============================] - 30s 385ms/step - loss: 0.0108\n",
            "Epoch 10/25\n",
            "78/78 [==============================] - 30s 385ms/step - loss: 0.0108\n",
            "Epoch 11/25\n",
            "78/78 [==============================] - 29s 373ms/step - loss: 0.0108\n",
            "Epoch 12/25\n",
            "78/78 [==============================] - 29s 375ms/step - loss: 0.0107\n",
            "Epoch 13/25\n",
            "78/78 [==============================] - 30s 383ms/step - loss: 0.0107\n",
            "Epoch 14/25\n",
            "78/78 [==============================] - 30s 388ms/step - loss: 0.0107\n",
            "Epoch 15/25\n",
            "78/78 [==============================] - 31s 396ms/step - loss: 0.0108\n",
            "Epoch 16/25\n",
            "78/78 [==============================] - 30s 379ms/step - loss: 0.0107\n",
            "Epoch 17/25\n",
            "78/78 [==============================] - 30s 386ms/step - loss: 0.0107\n",
            "Epoch 18/25\n",
            "78/78 [==============================] - 30s 383ms/step - loss: 0.0108\n",
            "Epoch 19/25\n",
            "78/78 [==============================] - 30s 382ms/step - loss: 0.0107\n",
            "Epoch 20/25\n",
            "78/78 [==============================] - 31s 397ms/step - loss: 0.0107\n",
            "Epoch 21/25\n",
            "78/78 [==============================] - 30s 384ms/step - loss: 0.0107\n",
            "Epoch 22/25\n",
            "78/78 [==============================] - 30s 381ms/step - loss: 0.0106\n",
            "Epoch 23/25\n",
            "78/78 [==============================] - 30s 380ms/step - loss: 0.0106\n",
            "Epoch 24/25\n",
            "78/78 [==============================] - 30s 385ms/step - loss: 0.0107\n",
            "Epoch 25/25\n",
            "78/78 [==============================] - 30s 383ms/step - loss: 0.0106\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<keras.callbacks.History at 0x7f5203d70cd0>"
            ]
          },
          "metadata": {},
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Testing a model"
      ],
      "metadata": {
        "id": "dbSKl47vZvpe"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#print trainings directories to pick one\n",
        "!ls -d training_checkpoints_*/"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "59CDDB0i4yTx",
        "outputId": "497ae253-e3ac-47d0-d066-8e508f55782c"
      },
      "execution_count": 49,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "training_checkpoints_20230406041748/\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_model = create_model()"
      ],
      "metadata": {
        "id": "tpmru7nG9kbW"
      },
      "execution_count": 72,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "checkpoint_dir = 'training_checkpoints_20230406041748'\n",
        "\n",
        "def load_weights(epoch=None):\n",
        "  if epoch is None:\n",
        "    weights_file = tf.train.latest_checkpoint(checkpoint_dir)\n",
        "  else:\n",
        "    with os.scandir(checkpoint_dir) as entries:\n",
        "      for entry in entries:\n",
        "        if re.search(f'^ckpt_epoch{epoch}_.*\\.index', entry.name):\n",
        "          weights_file = checkpoint_dir + '/'+ entry.name[:-6]\n",
        "\n",
        "  print(weights_file)\n",
        "  test_model.load_weights(weights_file)\n",
        "  return test_model\n",
        "\n",
        "test_model = load_weights()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wQ0JTXsp4VKF",
        "outputId": "d4b794c9-7a89-4867-d17c-de1f20b9b607"
      },
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "training_checkpoints_20230406041748/ckpt_epoch25_loss0.01064301934093237\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_start = dt.date.today() - dt.timedelta(days=200)\n",
        "test_end = dt.date.today()\n",
        "\n",
        "yfin.pdr_override()\n",
        "test_data = web.data.get_data_yahoo(ticker, test_start, test_end)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Mf4q97pfaSCA",
        "outputId": "4317ef63-be5e-49ca-fdca-1d5760efbba1"
      },
      "execution_count": 99,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\r[*********************100%***********************]  1 of 1 completed\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_data_norm, _ = normalize_data(test_data, scaler=the_scaler)"
      ],
      "metadata": {
        "id": "xEG2yEdKC8uy"
      },
      "execution_count": 100,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print(type(test_data_norm))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mhbqRZ6cDhd6",
        "outputId": "8b40a738-e143-4920-de03-8e8572f4389a"
      },
      "execution_count": 102,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<class 'pandas.core.frame.DataFrame'>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "input_data = np.expand_dims(test_data_norm.values, axis=0)\n",
        "print(input_data.shape)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "F2bnofchD0xv",
        "outputId": "0b2261fb-056d-4ec2-a98b-82517d7806f1"
      },
      "execution_count": 104,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(1, 138, 6)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "results = test_model.predict(input_data, batch_size=1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "AVYFQZnqEqhx",
        "outputId": "958d1669-c8bc-4eff-bb66-f25eb4dde011"
      },
      "execution_count": 105,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "1/1 [==============================] - 1s 1s/step\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(results)\n",
        "print(the_decoder(results))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FbdX4ulhExsX",
        "outputId": "14a763ca-0983-41ec-e88b-da796fa4b51a"
      },
      "execution_count": 113,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[[-0.01962117  0.09634934 -0.10176479 -0.00849891]]\n",
            "[[-0.06636524  1.3856668  -1.0948591   0.0728941 ]]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "test_data.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 237
        },
        "id": "m0k7toG3E2_9",
        "outputId": "38ab6e43-1321-4028-9482-8e6687802a7d"
      },
      "execution_count": 107,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "                  Open        High         Low       Close   Adj Close  \\\n",
              "Date                                                                     \n",
              "2022-09-19  149.309998  154.559998  149.100006  154.479996  153.989029   \n",
              "2022-09-20  153.399994  158.080002  153.080002  156.899994  156.401352   \n",
              "2022-09-21  157.339996  158.740005  153.600006  153.720001  153.231461   \n",
              "2022-09-22  152.380005  154.470001  150.910004  152.740005  152.254578   \n",
              "2022-09-23  151.190002  151.470001  148.559998  150.429993  149.951904   \n",
              "\n",
              "               Volume  \n",
              "Date                   \n",
              "2022-09-19   81474200  \n",
              "2022-09-20  107689800  \n",
              "2022-09-21  101696800  \n",
              "2022-09-22   86652500  \n",
              "2022-09-23   96029900  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Open</th>\n",
              "      <th>High</th>\n",
              "      <th>Low</th>\n",
              "      <th>Close</th>\n",
              "      <th>Adj Close</th>\n",
              "      <th>Volume</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2022-09-19</th>\n",
              "      <td>149.309998</td>\n",
              "      <td>154.559998</td>\n",
              "      <td>149.100006</td>\n",
              "      <td>154.479996</td>\n",
              "      <td>153.989029</td>\n",
              "      <td>81474200</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2022-09-20</th>\n",
              "      <td>153.399994</td>\n",
              "      <td>158.080002</td>\n",
              "      <td>153.080002</td>\n",
              "      <td>156.899994</td>\n",
              "      <td>156.401352</td>\n",
              "      <td>107689800</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2022-09-21</th>\n",
              "      <td>157.339996</td>\n",
              "      <td>158.740005</td>\n",
              "      <td>153.600006</td>\n",
              "      <td>153.720001</td>\n",
              "      <td>153.231461</td>\n",
              "      <td>101696800</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2022-09-22</th>\n",
              "      <td>152.380005</td>\n",
              "      <td>154.470001</td>\n",
              "      <td>150.910004</td>\n",
              "      <td>152.740005</td>\n",
              "      <td>152.254578</td>\n",
              "      <td>86652500</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2022-09-23</th>\n",
              "      <td>151.190002</td>\n",
              "      <td>151.470001</td>\n",
              "      <td>148.559998</td>\n",
              "      <td>150.429993</td>\n",
              "      <td>149.951904</td>\n",
              "      <td>96029900</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-51b6b5ba-2841-4317-9ce2-b32b40e2e9fc');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 107
        }
      ]
    }
  ]
}