Last active
September 12, 2023 02:49
-
-
Save neon-ninja/7c222def5c4161d7bb92a0734bc6a12c to your computer and use it in GitHub Desktop.
NZX50 close price prediction using RandomForestRegressor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/neon-ninja/7c222def5c4161d7bb92a0734bc6a12c/stonks.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "AyLtX1H0MyAT" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import yfinance\n", | |
| "from sklearn.metrics import mean_absolute_error, r2_score\n", | |
| "from sklearn.ensemble import RandomForestRegressor\n", | |
| "import pandas as pd\n", | |
| "pd.options.plotting.backend = \"plotly\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 455 | |
| }, | |
| "id": "3vzw8alQMyAU", | |
| "outputId": "f0dc866e-eb9c-4b98-f29d-9e4e72998bcb" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| " Open High Low \\\n", | |
| "Date \n", | |
| "2003-01-03 00:00:00+13:00 1954.959961 1954.959961 1954.959961 \n", | |
| "2003-01-06 00:00:00+13:00 1981.099976 1981.099976 1981.099976 \n", | |
| "2003-01-07 00:00:00+13:00 1992.420044 1992.420044 1992.420044 \n", | |
| "2003-01-08 00:00:00+13:00 1987.989990 1987.989990 1987.989990 \n", | |
| "2003-01-09 00:00:00+13:00 1984.449951 1984.449951 1984.449951 \n", | |
| "... ... ... ... \n", | |
| "2023-09-06 00:00:00+12:00 11437.250000 11469.169922 11393.169922 \n", | |
| "2023-09-07 00:00:00+12:00 11427.660156 11461.480469 11397.719727 \n", | |
| "2023-09-08 00:00:00+12:00 11426.839844 11448.120117 11323.070312 \n", | |
| "2023-09-11 00:00:00+12:00 11344.110352 11365.129883 11284.320312 \n", | |
| "2023-09-12 00:00:00+12:00 11303.820312 11317.040039 11266.830078 \n", | |
| "\n", | |
| " Close Volume Dividends Stock Splits \n", | |
| "Date \n", | |
| "2003-01-03 00:00:00+13:00 1954.959961 0 0.0 0.0 \n", | |
| "2003-01-06 00:00:00+13:00 1981.099976 0 0.0 0.0 \n", | |
| "2003-01-07 00:00:00+13:00 1992.420044 0 0.0 0.0 \n", | |
| "2003-01-08 00:00:00+13:00 1987.989990 0 0.0 0.0 \n", | |
| "2003-01-09 00:00:00+13:00 1984.449951 0 0.0 0.0 \n", | |
| "... ... ... ... ... \n", | |
| "2023-09-06 00:00:00+12:00 11427.660156 24625000 0.0 0.0 \n", | |
| "2023-09-07 00:00:00+12:00 11426.839844 25017200 0.0 0.0 \n", | |
| "2023-09-08 00:00:00+12:00 11344.110352 23152200 0.0 0.0 \n", | |
| "2023-09-11 00:00:00+12:00 11302.410156 19210500 0.0 0.0 \n", | |
| "2023-09-12 00:00:00+12:00 11273.919922 0 0.0 0.0 \n", | |
| "\n", | |
| "[5098 rows x 7 columns]" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| " <div id=\"df-1216dee8-2323-40c6-a87d-0e11890e8acd\" 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>Volume</th>\n", | |
| " <th>Dividends</th>\n", | |
| " <th>Stock Splits</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", | |
| " <th></th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>2003-01-03 00:00:00+13:00</th>\n", | |
| " <td>1954.959961</td>\n", | |
| " <td>1954.959961</td>\n", | |
| " <td>1954.959961</td>\n", | |
| " <td>1954.959961</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2003-01-06 00:00:00+13:00</th>\n", | |
| " <td>1981.099976</td>\n", | |
| " <td>1981.099976</td>\n", | |
| " <td>1981.099976</td>\n", | |
| " <td>1981.099976</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2003-01-07 00:00:00+13:00</th>\n", | |
| " <td>1992.420044</td>\n", | |
| " <td>1992.420044</td>\n", | |
| " <td>1992.420044</td>\n", | |
| " <td>1992.420044</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2003-01-08 00:00:00+13:00</th>\n", | |
| " <td>1987.989990</td>\n", | |
| " <td>1987.989990</td>\n", | |
| " <td>1987.989990</td>\n", | |
| " <td>1987.989990</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2003-01-09 00:00:00+13:00</th>\n", | |
| " <td>1984.449951</td>\n", | |
| " <td>1984.449951</td>\n", | |
| " <td>1984.449951</td>\n", | |
| " <td>1984.449951</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>...</th>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-09-06 00:00:00+12:00</th>\n", | |
| " <td>11437.250000</td>\n", | |
| " <td>11469.169922</td>\n", | |
| " <td>11393.169922</td>\n", | |
| " <td>11427.660156</td>\n", | |
| " <td>24625000</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-09-07 00:00:00+12:00</th>\n", | |
| " <td>11427.660156</td>\n", | |
| " <td>11461.480469</td>\n", | |
| " <td>11397.719727</td>\n", | |
| " <td>11426.839844</td>\n", | |
| " <td>25017200</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-09-08 00:00:00+12:00</th>\n", | |
| " <td>11426.839844</td>\n", | |
| " <td>11448.120117</td>\n", | |
| " <td>11323.070312</td>\n", | |
| " <td>11344.110352</td>\n", | |
| " <td>23152200</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-09-11 00:00:00+12:00</th>\n", | |
| " <td>11344.110352</td>\n", | |
| " <td>11365.129883</td>\n", | |
| " <td>11284.320312</td>\n", | |
| " <td>11302.410156</td>\n", | |
| " <td>19210500</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-09-12 00:00:00+12:00</th>\n", | |
| " <td>11303.820312</td>\n", | |
| " <td>11317.040039</td>\n", | |
| " <td>11266.830078</td>\n", | |
| " <td>11273.919922</td>\n", | |
| " <td>0</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>0.0</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "<p>5098 rows × 7 columns</p>\n", | |
| "</div>\n", | |
| " <div class=\"colab-df-buttons\">\n", | |
| "\n", | |
| " <div class=\"colab-df-container\">\n", | |
| " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-1216dee8-2323-40c6-a87d-0e11890e8acd')\"\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 -960 960 960\">\n", | |
| " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n", | |
| " </svg>\n", | |
| " </button>\n", | |
| "\n", | |
| " <style>\n", | |
| " .colab-df-container {\n", | |
| " display:flex;\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", | |
| " .colab-df-buttons div {\n", | |
| " margin-bottom: 4px;\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-1216dee8-2323-40c6-a87d-0e11890e8acd 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-1216dee8-2323-40c6-a87d-0e11890e8acd');\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", | |
| "\n", | |
| "\n", | |
| "<div id=\"df-d7f06902-5fa0-423d-8189-daa4f650ea8c\">\n", | |
| " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-d7f06902-5fa0-423d-8189-daa4f650ea8c')\"\n", | |
| " title=\"Suggest charts.\"\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", | |
| " <g>\n", | |
| " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n", | |
| " </g>\n", | |
| "</svg>\n", | |
| " </button>\n", | |
| "\n", | |
| "<style>\n", | |
| " .colab-df-quickchart {\n", | |
| " --bg-color: #E8F0FE;\n", | |
| " --fill-color: #1967D2;\n", | |
| " --hover-bg-color: #E2EBFA;\n", | |
| " --hover-fill-color: #174EA6;\n", | |
| " --disabled-fill-color: #AAA;\n", | |
| " --disabled-bg-color: #DDD;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-quickchart {\n", | |
| " --bg-color: #3B4455;\n", | |
| " --fill-color: #D2E3FC;\n", | |
| " --hover-bg-color: #434B5C;\n", | |
| " --hover-fill-color: #FFFFFF;\n", | |
| " --disabled-bg-color: #3B4455;\n", | |
| " --disabled-fill-color: #666;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart {\n", | |
| " background-color: var(--bg-color);\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: var(--fill-color);\n", | |
| " height: 32px;\n", | |
| " padding: 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart:hover {\n", | |
| " background-color: var(--hover-bg-color);\n", | |
| " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: var(--button-hover-fill-color);\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart-complete:disabled,\n", | |
| " .colab-df-quickchart-complete:disabled:hover {\n", | |
| " background-color: var(--disabled-bg-color);\n", | |
| " fill: var(--disabled-fill-color);\n", | |
| " box-shadow: none;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-spinner {\n", | |
| " border: 2px solid var(--fill-color);\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " animation:\n", | |
| " spin 1s steps(1) infinite;\n", | |
| " }\n", | |
| "\n", | |
| " @keyframes spin {\n", | |
| " 0% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " }\n", | |
| " 20% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 30% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 40% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 60% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 80% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " 90% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " }\n", | |
| "</style>\n", | |
| "\n", | |
| " <script>\n", | |
| " async function quickchart(key) {\n", | |
| " const quickchartButtonEl =\n", | |
| " document.querySelector('#' + key + ' button');\n", | |
| " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n", | |
| " quickchartButtonEl.classList.add('colab-df-spinner');\n", | |
| " try {\n", | |
| " const charts = await google.colab.kernel.invokeFunction(\n", | |
| " 'suggestCharts', [key], {});\n", | |
| " } catch (error) {\n", | |
| " console.error('Error during call to suggestCharts:', error);\n", | |
| " }\n", | |
| " quickchartButtonEl.classList.remove('colab-df-spinner');\n", | |
| " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n", | |
| " }\n", | |
| " (() => {\n", | |
| " let quickchartButtonEl =\n", | |
| " document.querySelector('#df-d7f06902-5fa0-423d-8189-daa4f650ea8c button');\n", | |
| " quickchartButtonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| " })();\n", | |
| " </script>\n", | |
| "</div>\n", | |
| " </div>\n", | |
| " </div>\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 2 | |
| } | |
| ], | |
| "source": [ | |
| "# Load data from Yahoo Finance\n", | |
| "df = yfinance.Ticker(\"^NZ50\").history(period=\"max\")\n", | |
| "df" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 542 | |
| }, | |
| "id": "QnuniVkPMyAV", | |
| "outputId": "053e0aca-3604-48d1-8238-7d0ce55b758b" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "display_data", | |
| "data": { | |
| "text/html": [ | |
| "<html>\n", | |
| "<head><meta charset=\"utf-8\" /></head>\n", | |
| "<body>\n", | |
| " <div> <script src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_SVG\"></script><script type=\"text/javascript\">if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}</script> <script type=\"text/javascript\">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>\n", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment