PolynomialRegression/Polynomial Regression.ipynb

369 lines
53 KiB
Plaintext
Raw Normal View History

2021-06-24 11:07:41 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Algorytm najszybszego spadku dla regresji wielomianowej. \n",
"Zakładamy, że dysponujemy zbiorem składającym się z dwóch cech (x i y). Modelujemy zależność y od x za pomocą funkcji wielomianowej. Celem projektu jest implementacja metody najszybszego spadku dla tego problemu. Zakładamy kwadratową funkcję straty. Implementacja powinna umożliwiać podanie stopnia wielomianu, który ma być użyty do modelowania. Implementacja powinna zwracać wektor oszacowanych parametrów oraz pokazywać wizualnie zmiany wartości funkcji straty wraz z postępem uczenia."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zbiór danych: https://www.kaggle.com/varpit94/apple-stock-data-updated-till-22jun2021?select=AAPL.csv\n",
"This dataset provides historical data of APPLE INC. stock (AAPL). The data is available at a daily level. Currency is USD."
]
},
{
"cell_type": "code",
2021-06-24 13:03:10 +02:00
"execution_count": 1,
2021-06-24 11:07:41 +02:00
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
2021-06-24 13:03:10 +02:00
"import matplotlib.pyplot as plt\n",
"import numpy as np"
2021-06-24 11:07:41 +02:00
]
},
{
"cell_type": "code",
2021-06-24 13:03:10 +02:00
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"degree = 6\n",
"X_plot = np.linspace(0, 150, 1000)\n",
"initial_theta = [0] * (degree + 1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
2021-06-24 11:07:41 +02:00
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<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>Date</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",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1980-12-12</td>\n",
" <td>0.128348</td>\n",
" <td>0.128906</td>\n",
" <td>0.128348</td>\n",
" <td>0.128348</td>\n",
" <td>0.100751</td>\n",
" <td>469033600</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1980-12-15</td>\n",
" <td>0.122210</td>\n",
" <td>0.122210</td>\n",
" <td>0.121652</td>\n",
" <td>0.121652</td>\n",
" <td>0.095495</td>\n",
" <td>175884800</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1980-12-16</td>\n",
" <td>0.113281</td>\n",
" <td>0.113281</td>\n",
" <td>0.112723</td>\n",
" <td>0.112723</td>\n",
" <td>0.088485</td>\n",
" <td>105728000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1980-12-17</td>\n",
" <td>0.115513</td>\n",
" <td>0.116071</td>\n",
" <td>0.115513</td>\n",
" <td>0.115513</td>\n",
" <td>0.090676</td>\n",
" <td>86441600</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1980-12-18</td>\n",
" <td>0.118862</td>\n",
" <td>0.119420</td>\n",
" <td>0.118862</td>\n",
" <td>0.118862</td>\n",
" <td>0.093304</td>\n",
" <td>73449600</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Date Open High Low Close Adj Close Volume\n",
"0 1980-12-12 0.128348 0.128906 0.128348 0.128348 0.100751 469033600\n",
"1 1980-12-15 0.122210 0.122210 0.121652 0.121652 0.095495 175884800\n",
"2 1980-12-16 0.113281 0.113281 0.112723 0.112723 0.088485 105728000\n",
"3 1980-12-17 0.115513 0.116071 0.115513 0.115513 0.090676 86441600\n",
"4 1980-12-18 0.118862 0.119420 0.118862 0.118862 0.093304 73449600"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df = pd.read_csv('AAPL.csv')\n",
2021-06-24 13:03:10 +02:00
"X = df[['Low']]\n",
2021-06-24 11:07:41 +02:00
"Y = df['Volume']\n",
"display(df.head(5))"
]
},
{
"cell_type": "code",
2021-06-24 13:03:10 +02:00
"execution_count": 4,
2021-06-24 11:07:41 +02:00
"metadata": {},
"outputs": [
{
"data": {
2021-06-24 13:03:10 +02:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkkAAAE6CAYAAAAC3sbGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjmElEQVR4nO3df3Dcd33n8dd7pXUiOaBgWXAcQVJaMvQAN/zQcNDMdLgIjpBgAmlh4OScm8CI2IVz7spwBN0055vR9IbrtM5Mm2R2ICGM92AodQCTQElFMtwxbYocEkQIv45YJgQaxylKbDmWLL3vD+3Ku6vPd/e7q9397ld6PjIZa7/73d3PfrT6fl/7+fU1dxcAAADKZZIuAAAAQCciJAEAAAQQkgAAAAIISQAAAAGEJAAAgABCEgAAQEDLQpKZ3WFmT5nZD2LsO2RmU2b2fTN7wMwualW5AAAA4mhlS9JnJV0Rc98/l/Q5d/9dSf9D0p+1qlAAAABxtCwkufu3JT1Tus3MftvMvmFmR8zs/5jZ7xTuepWkqcLP90u6ulXlAgAAiKPdY5Jykj7q7m+Q9DFJtxa2PyLpDwo/v0fSC8ysv81lAwAAWNXdrhcyswsk/Z6kvzGz4ubzCv9+TNJfmdkfSfq2pF9KOtuusgEAAFRqW0jSSqvVb9z9tZV3uPuTkq6RVsPUH7j7XBvLBgAAUKZt3W3u/qykx83svZJkKy4t/LzdzIpluUnSHe0qFwAAQEgrlwD4vKR/kPRK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rLDiGKPZuVm5fHWMUVRQYokAAADQqDiz274v6XVtKEtNUWO
2021-06-24 11:07:41 +02:00
"text/plain": [
2021-06-24 13:03:10 +02:00
"<Figure size 720x360 with 1 Axes>"
2021-06-24 11:07:41 +02:00
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
2021-06-24 13:03:10 +02:00
"fig = plt.figure(figsize=(10,5))\n",
2021-06-24 11:07:41 +02:00
"chart = fig.add_subplot()\n",
"chart.plot(X,Y ,\"go\")\n",
"plt.show()"
]
2021-06-24 13:03:10 +02:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Metody do regresji wielomianowej"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n",
"441.0\n"
]
}
],
"source": [
"def polynomial_regression(theta, x):\n",
" value = 0\n",
" for i in range(len(theta)):\n",
" value += theta[i] * x**i\n",
" return value \n",
"\n",
"def mean_squared_error(Y_predicted, Y):\n",
" result = 0\n",
" for i in range(len(Y)):\n",
" result += (Y_predicted[i] - Y[i]) ** 2\n",
" return result/len(Y)\n",
"\n",
"def gradient(theta, X, Y):\n",
" return 1.0 / len(y) * (X.T * (X * theta - Y)) \n",
"\n",
"def gradient_descent(X, Y, theta, cost_function = mean_squared_error, alpha=0.1, eps=0.001, max_steps = 1000000):\n",
" cost = cost_function([polynomial_regression(theta, x) for x in X], Y)\n",
" logs = [[cost, theta]]\n",
" \n",
" for i in range(max_steps):\n",
" theta = theta - alpha * gradient(theta, X, Y)\n",
" next_cost = cost_function([polynomial_regression(theta, x) for x in X], Y)\n",
" logs.append([next_cost, theta])\n",
" if abs(cost - next_cost) <= eps:\n",
" break\n",
" return theta, logs\n",
" \n",
" \n",
"print(polynomial_regression([1,1,0,1], 2))\n",
"print(mean_squared_error([1,2,1,1],[1,2,43,1]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Metody do wykresów"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkkAAAE6CAYAAAAC3sbGAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAu30lEQVR4nO3de3hcdbn28fuZZFqSlgabFKiUJFU5t7RAOIm+AkGlQIEtygZTLCgGrCBu9cJD3LK7NfrKi1q2CjUKCDKiGyxIaUUwgHiAYgpCoIWKQGoRpaWS0qY0p9/7x5ppJsmazCSZmTUr8/1cV65k1qxMnqwkM3d+R3POCQAAAINFgi4AAACgEBGSAAAAfBCSAAAAfBCSAAAAfBCSAAAAfBCSAAAAfOQsJJnZjWb2qpk9ncG5NWbWamZPmdlDZjYrV3UBAABkIpctST+WdGqG514j6Rbn3OGS/lvSN3JVFAAAQCZyFpKccw9L2pp8zMzebmb3mtlaM/udmR0cv+tQSa3xjx+UdFau6gIAAMhEvscktUi63Dl3lKTPSboufvxJSefEP/43SXuaWWWeawMAANitNF9fyMymSnqnpNvNLHF4cvz95yR9z8wulPSwpJcl9earNgAAgKHyFpLktVq97pybP/QO59zfJX1A2h2mznHOdeaxNgAAgEHy1t3mnNsm6UUz+5AkmWde/OMqM0vU8kVJN+arLgAAAD+5XALgNkmPSDrIzDaZ2cckNUj6mJk9KekZDQzQPlHSc2a2QdI+kppzVRcAAEAmzDkXdA0AAAAFhxW3AQAAfBCSAAAAfORkdltVVZWrra3NxUMDAABk1dq1a7c452YMPZ6TkFRbW6u2trZcPDQAAEBWmVmH33G62wAAAHwQkgAAAHwQkgAAAHwQkgAAAHwQkgAAAHwQkgAAAHwQkgAAAHyEMiTF2mOqXVaryNKIapfVKtYeC7okAAAwweRkMclcirXH1LiyUV09XZKkjs4ONa5slCQ1zG0IsjQAADCBhK4lqam1aXdASujq6VJTa1NAFQEAgIkodCFpY+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Wha2qKaiRiZTTUWNWha2MGgbAABklTnnsv6gdXV1rq2tLeuPCwAAkG1mttY5Vzf0eOhakgAAAPKBkAQAAOCDkAQAAOCDkAQAAOCDkAQAAOCjqEISG+MCAIBMhTYkpQs8Q+9fsmqJGlc2qqOzQ05u98a4BCUAAArU3/8e6JcPZUiKtcdGDDx+9y9vW87GuAAAhMHmzdL550tz5kivvBJYGaEMSU2tTSMGHr/7nfwXzWRjXAAACoRz0s9/Lh16qPSzn0m7dkmPPx5YOaEMSamCTeL4aIIPG+MCAFAAXnlFOucc6bzzpC1bpJNOktrbpdNPD6ykUIakVMEmcTzV/SYbdJuNcQEACJhz0i23SIcdJt15p7TnntIPfiC1tkpve1ugpYUyJDXXN6s8Wj7oWHLgSXX/pXWXsjEuAACF4sUXpVNPlRYvlv71L2nBAumZZ6TGRsks/efnWGnQBYxFItg0tTapo7NDJVYyaExS8v0bOzequqJazfXNBCIAAApBb6907bXSV74idXVJb3mL9J3vSB/5SEGEo4RQhiRpIAg1rmzcPUg7McstcT+hCACAAvPEE9LHPy6tXevdPu88adkyaZ99Ai3LTyi72xLSzXIDAAAFoqtLuvJK6eijvYBUXS2tWiXddltBBiQpxC1JUvpZbgAAoAD85jfSJZdIL7zgdaddcYX0ta9JU6cGXdmIQt2SlG6WGwAACNBrr0kXXii9971eQJo7V3r0Ua97rcADkpRBSDKzPczsMTN70syeMbOl+SgsE+lmuQEAgAA4J/3kJ9Ihh0g33yxNnix9/eteN9sxxwRdXcYy6W7bJelk59x2M4tK+r2Z/co592iOa0uLWWw
"text/plain": [
"<Figure size 720x360 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"def plot_polynomial_regression(theta):\n",
" fig = plt.figure(figsize=(10,5))\n",
" Y_plot = [polynomial_regression(theta, x) for x in X_plot]\n",
" chart = fig.add_subplot()\n",
" chart.plot(X_plot, Y_plot, color=\"red\", lw=2, label=f\"degree {len(theta)}\")\n",
" chart.plot(X,Y ,\"go\")\n",
" plt.show()\n",
" \n",
"plot_polynomial_regression([1,100,0,1000]) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Wyniki za pomocą gotowej biblioteki"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.preprocessing import PolynomialFeatures, StandardScaler\n",
"from sklearn.pipeline import make_pipeline\n",
"from sklearn.linear_model import Ridge, LinearRegression"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Pipeline(steps=[('polynomialfeatures', PolynomialFeatures(degree=6)),\n",
" ('linearregression', LinearRegression())])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = make_pipeline(PolynomialFeatures(degree=degree, include_bias=True), \n",
" LinearRegression())\n",
"model.fit(X,Y)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlIAAAE6CAYAAADdkLzZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAtLUlEQVR4nO3de3xcdZ3/8fdncmluNE2TtkDbpAXLnSIYL1hRoLhCoYIoqNuWirgR2UV0ZVXsKuJvuyqrK6yuQB4IFs1PRSiseGEtVS6iomkFQikC0ia0lDb0EmiTktt3//jOJJNkJpnMTOZMZl7Px+M85syZmXO+800yec/3+z3fY845AQAAYPxCQRcAAABgsiJIAQAAJIkgBQAAkCSCFAAAQJIIUgAAAEkiSAEAACQpsCBlZreZ2S4zeyqB59aZ2Xoze9LMHjSzOZkoIwAAwGiCbJH6vqSzE3zuNyTd4ZxbKOkrkr46UYUCAABIVGBByjn3s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c3p3zcWKb8vynq7k/tosXJqKus09ZPbR31OfNumKfWjtakXgsAAIJhZhucc/WxHkvHWXsm6XuSNicSoiZaECFKSmzMVLznMN4KAIDJKR1de4skrZB0ppk9Hl7ybqIlJzfmmKd4E3kywScAAJNTOs7a+51zzpxzC51zbwwvv0xH4SabscY8rV68WmVFZUO2lRWVJXyhZQAAkF1ybmbzoI02x9SyE5epcWmj6irrZDLVVdapcWmjlp24LMOlBAAA6VAYdAFy0WhjnpaduCytwamppUmr1q9SW0ebaitrtXrxaoIZAAAZQpCaABM
"text/plain": [
"<Figure size 720x360 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"Y_plot = model.predict([[x] for x in X_plot])\n",
"\n",
"fig = plt.figure(figsize=(10,5))\n",
"chart = fig.add_subplot()\n",
"chart.plot(X,Y ,\"go\")\n",
"chart.plot(X_plot, Y_plot, color=\"red\", lw=2, label=f\"degree {degree}\")\n",
"degree"
]
2021-06-24 11:07:41 +02:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}