mirea-projects/Third term/Artificial intelligence systems and big data/4.ipynb

1358 lines
317 KiB
Plaintext
Raw Normal View History

2024-09-23 23:22:33 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Рабочая тетрадь No 4"
]
},
{
"cell_type": "code",
"execution_count": 478,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"from sklearn import metrics\n",
"import matplotlib.pyplot as plt\n",
"from scipy.optimize import curve_fit\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1.3 Задание\n",
"\n",
"Представьте собственные данные и постройте эктраполяцию полиномами \n",
"первой, второй и третьей степени. "
]
},
{
"cell_type": "code",
"execution_count": 479,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-5.1585996 -4.46639064 -2.55830037 -2.0590825 -0.53638287 0.3553611\n",
" 0.60294719 2.45189108 3.28141706 4.08769096 4.92161744]\n",
"[24.70826208 16.36113899 9.04877285 4.01427977 1.02983411 0.38854345\n",
" 0.74865966 4.11727242 9.13843478 15.83010792 24.60875347]\n"
]
}
],
"source": [
"delta = 1.0\n",
"x = np.linspace(-5, 5, 11)\n",
"y = x ** 2 + delta * (np.random.rand(11) - 0.5)\n",
"x += delta * (np.random.rand(11) - 0.5)\n",
"\n",
"print(x)\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Эктраполяция полиномом первой степени"
]
},
{
"cell_type": "code",
"execution_count": 480,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAh8AAAGdCAYAAACyzRGfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAk9ElEQVR4nO3df3BU5d338c9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sOquLhP2ttw8FklwIAQFLlJruAbLC24aBWew5IUvDfJVMrk1kSAABJw8iHzUKDR8BqzwFGQAAAWYvwYaNowSOAAAIAyFaED5sMFDwCCCAAgGxE+LBBLMEjgAACAMg2hA8brIkxeAx2fQAA0hnhwwbLqsfYuj4AIDVxWoXYcKitDQKH0cay66WuegyH3QJABuC0CrEjfNgklgBC8ACAzPDzvxzSM+8eCltGAOkf4cNGAwUQggcAZIa3P3doy9FDUR8jgETHnA+bLZlaqbo+czoIHgCQGX7+l0PacjRnwHU4qjESIx8GBILGGs8BLSN4AEBGWNtwMGJXS38YAQlH+DBkydRK/ugAIIMM5rQKfA50Y7cLAACDwGkVBo/wAQDAICyZWqkHbh8d07rM9QtH+AAAYJAWTRmtWeWdA65D8IhE+AAA4BJMH+nvdwSE4BEd4QMAgEu0aMpoTqsQB452AQAgATitQuziHvnYuXOn7rjjDpWVlcnhcOj1118Pe3zevHlyOBxhtxkzZiSqXgAAUtaSqZU6/NOvEzwuIu7w0db
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"m = np.vstack((x, np.ones(11))).T\n",
"s = np.linalg.lstsq(m,y,rcond = None)[0]\n",
"\n",
"x_prec = np.linspace(-5, 5, 101)\n",
"\n",
"plt.plot(x, y,'D')\n",
"plt.plot(x_prec, s[0] * x_prec + s[1], '-', lw=1)\n",
"plt.grid()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Эктраполяция полиномом второй степени"
]
},
{
"cell_type": "code",
"execution_count": 481,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAh8AAAGdCAYAAACyzRGfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAABIYElEQVR4nO3deXhU1f3H8fdkshFIQNY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NaTwURMOR8UJPbrZnIiI2LBmpq8uf1Q+TCh81FTXq8zytQBrZ5plbUVERIKluMh3rzFnAnQabbefWlD4qKmU5tBmkKkP7jKnX0RERIJl6wIo+NHUHS+COg2stlMbCh+1oYmnIiJiS4VTLr+018dJUPiojY4XmWVswVxyW5RvtR0REYkSh/bD1o9Mndwc2g6y208tKXzURlwd6DTK1EWHzKJjIiIigbZ+NniKTd31Kohx2u2nlhQ+aqtb+VMvr9nrQ0REokeYX+VSqkbhY/LkyfTu3Zvk5GSaNm3KqFGj2Lx5c4XHHDlyhHHjxtGoUSPq1avHmDFjyM7O9mvTIaHl2dCwjal3LDWTT0VERAJl7zrIXm/qU3tBk9Pt9nMSahQ+lixZwrhx41ixYgUZGRm4XC6GDRtGfr5vzsNdd93F3LlzmT17NkuWLGHPnj2MHh1+lwGdkMNRLnV6NfFUREQCq8JRj/C5iVxVYmvy4Pnz51fYnj59Ok2bNmXVqlX079+fnJwcXnrpJWbOnMkFF1wAwMsvv8wZZ5zBihUrOPvss/3XeSjodg0s+gsmfLwG/e8Jq7X1RUQkTBQXwfo3Te2Mh85j7PZzkmoUPo6
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"m = np.vstack((x**2, x, np.ones(11))).T\n",
"s = np.linalg.lstsq(m, y, rcond = None)[0]\n",
"\n",
"x_prec = np.linspace(-5, 5, 101)\n",
"\n",
"plt.plot(x, y,'D')\n",
"plt.plot(x_prec, s[0] * x_prec**2 + s[1] * x_prec + s[2], '-', lw=2)\n",
"plt.grid()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Эктраполяция полиномом третьей степени"
]
},
{
"cell_type": "code",
"execution_count": 482,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAh8AAAGdCAYAAACyzRGfAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0c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JS1ZTEUHC5TsEH78JrvAQof3ux27ytfti2HX5eb6UdERKTYus+t22m9ICnFTC8nQeHDlzMuh+RTrLWlk830IiIiAuByeV9iG4anXEDhw7fYODj7Lmtt41zI/tH3/iIiIoG2cyUc3WutdTzfTC8nSeGjImddDwkNrLWlzxlpRURExOuUS+PToGFbM72cJIWPisTVhj53WGtrP4KDv5jpR0REopfbDT+XCx9hesoFFD4q1+tWiKtTuu12eVY9FRERCab9G+DgZmtN4SNCJdSHHjdZa1nvwuFsM/2IiEh0Kn/KJakZNOtmphc/UPg4kT7jPEvXFisqgOVTzfUjIiLRp/yqph3P8yyMGaYUPk4kuSl0vdpaW/kGHPvNTD8iIhJdcnbCzlXWWhifcgGFj6rpdw/YygxVwRH45l/m+hERkehR/pRLrbqee5GFMYWPqmjQBjpdbK2t+CfkHzHTj4iIRI+fPrVudxgNMQ4zvfiJwkdVnZNu3T72G6yaZqQVERGJEkf2wbZl1tppF5rpxY8UPqoqpZMnbZa17AXPHQZFREQCYf0szzIPxRy1oe0gc/34icJHdfQvd/TjSDZkTTfTi4iIRL6fy51yaTcMHAlmevEjhY/qSOsJrQdYa189B0WFRtoREZEIduwQ/LLIWjs9/E+5QDXDx6RJk+jZsydJSUk
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"m = np.vstack((x**3, x**2, x, np.ones(11))).T\n",
"s = np.linalg.lstsq(m, y, rcond = None)[0]\n",
"\n",
"x_prec = np.linspace(-5, 5, 101)\n",
"\n",
"plt.plot(x, y,'D')\n",
"plt.plot(x_prec, s[0] * x_prec**3 + s[1] * x_prec**2 + s[2] * x_prec + s[3], '-', lw=3)\n",
"plt.grid()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.1.5 Задача\n",
"\n",
"Необходимо проверить гипотезу, что наши точечно заданная функция ложится \n",
"на кривые вида: \n",
"1. $f(x,b) = b_0+b_1x$\n",
"2. $f(x,b) = b_0+b_1x+b_2x^2$\n",
"3. $f(x,b) = b_0+b_1\\ln(x)$\n",
"4. $f(x,b) = b_0x^{b_1}$ "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Функция №1"
]
},
{
"cell_type": "code",
"execution_count": 483,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.2474115 1.73859228]\n",
"0.0001438268762724966\n",
"0.12646845774981774\n"
]
}
],
"source": [
"beta = (1.24, 1.74)\n",
"def f(x, b0, b1):\n",
" return b0 + b1 * x\n",
"\n",
"xdata = np.linspace(0,5,50)\n",
"y = f(xdata, *beta)\n",
"ydata = y + 0.05 * np.random.randn(len(xdata))\n",
"\n",
"from scipy.optimize import curve_fit\n",
"beta_opt, beta_cov = curve_fit(f, xdata, ydata)\n",
"print(beta_opt)\n",
"\n",
"lin_dev = sum(beta_cov[0])\n",
"print(lin_dev)\n",
"\n",
"residuals = ydata - f(xdata, *beta_opt)\n",
"fres = sum(residuals**2)\n",
"print(fres)"
]
},
{
"cell_type": "code",
"execution_count": 484,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkUAAAG6CAYAAAAGUjKQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAABS1klEQVR4nO3dd3hUVf7H8ffMpEMyEEhIgAChE5DeQRSkFwEVEEXRVXfXxbbsWnBXkZ+62NayoljWpagIiFKVJr0EQgu9F+kECKSRQmbu74+Qm0IogSSTZD6v58nzbM69M3M0C/l47vl+j8UwDAMRERERN2d19QREREREigOFIhEREREUikREREQAhSIRERERQKFIREREBFAoEhEREQEUikREREQA8HD1BIorp9PJyZMn8ff3x2KxuHo6IiIichMMwyAhIYHKlStjteZv7Ueh6BpOnjxJWFiYq6chIiIit+DYsWNUrVo1X69RKLoGf39/IONfakBAgItn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UAHkd4Go4DHwnxPOn/R1JwxsAG+n0CvmJ/UM8OeijUvv8sLp6AiIiInJ9uavKAModTcH/0zCW7R9oBqIGnvuJHL+VEYs7Ub6SPcd7hNh9GD+suVuW2t8srRSJiIgUc9kPcDWcBrV+SWPdrl4k4weABSe9gn7m5XmtaNW6BQDdIkJUap9PCkUiIiLFXGa1WMDJNKw/hrEspaV5rYblEHU7r2Bnq2AueGX9Wlepff4pFImIiBRzQWW8CZ+fwsZt3Un
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, y, 'r', lw=2)\n",
"ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)\n",
"ax.set_xlim(0,5)\n",
"ax.set_xlabel(r'$x$', fontsize=18)\n",
"ax.set_ylabel(r'$f(x, \\beta)$', fontsize=18)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Функция №2"
]
},
{
"cell_type": "code",
"execution_count": 485,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.53903101 2.70563694 1.17692555]\n",
"0.00014584672644415915\n",
"0.12162090612880988\n"
]
}
],
"source": [
"beta = (0.51, 2.74, 1.17)\n",
"def f(x, b0, b1, b2):\n",
" return b0 + b1 * x + b2 * x**2\n",
"\n",
"xdata = np.linspace(0,5,50)\n",
"y = f(xdata, *beta)\n",
"ydata = y + 0.05 * np.random.randn(len(xdata))\n",
"\n",
"from scipy.optimize import curve_fit\n",
"beta_opt, beta_cov = curve_fit(f, xdata, ydata)\n",
"print(beta_opt)\n",
"\n",
"lin_dev = sum(beta_cov[0])\n",
"print(lin_dev)\n",
"\n",
"residuals = ydata - f(xdata, *beta_opt)\n",
"fres = sum(residuals**2)\n",
"print(fres)"
]
},
{
"cell_type": "code",
"execution_count": 486,
"metadata": {},
"outputs": [
{
"data": {
"image/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iJSpYpVqqEsEIX8143t6dcC4EYRt0b9h8FPXOewOdYlhSIRERGpUnmV6nJtlxjYkjQaACNmpvb+gPXD2rAtIctRU6xTCkUiIiJSpYrVp0N+tLL50IWO97d3fp+YCe0vGteQKRSJiIhIlcqrT4esMROzZ6Lt+O1tPmDztLYXjWvoFIpERESkSkM6+tN1O2zZcSEQ3RL4CVtntAKDoUFUqa4JhSIRERGp0vLXD7N+7Ris5+PC5BaLiJ3VHKvB2GCqVNeEtuSLiIjIRdb++xi3/G87SinrcD/Jbwl77/HAYjIBZVWq508Kc/oq1TWhUCQiIiJ2tnyZyE2/DaKIsrVC01utYeHhMcRmFDS4KtU1oVAkIiLShFVu3+F2oIhxd/iRb20GwKTmP/Px/iG4+noR6evl4NleXQpFIiIiTVTl9h0+SaWk/HcYZ61lHe5v8N7Cl/t64+rv48hp1huFIhERkSaocvsOrzQzpxcNJtPaEoCh7rEs2dUJj9YtHDfJeqbdZyIiIk1M5fYd7pkWcj/tT6olBIBexj20+F0Cnh0CHTdJB1AoEhERaWIqtu9wPWuhZGFPTprbAdDVcAjvGbvY5+nZaNp3VJdCkYiISBNT3pbDJdeC4cOuJJR2AaC94Titbt9CakgLu3FNhUKRiIhIE9PKxwPTOSuuH3TkcEkPAEI4Rceb13GyfYDduKZEoUhERKSJ6dXCh2b/DuVAUW8AWpFGz0krSejaCqDRte+oLoUiERGRJqQ
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, y, 'r', lw=2)\n",
"ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)\n",
"ax.set_xlim(0,5)\n",
"ax.set_xlabel(r'$x$', fontsize=18)\n",
"ax.set_ylabel(r'$f(x, \\beta)$', fontsize=18)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Функция №3"
]
},
{
"cell_type": "code",
"execution_count": 487,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.36588575 0.48465322]\n",
"3.7836263532723186e-05\n",
"0.11229032607799314\n"
]
}
],
"source": [
"beta = (1.37, 0.48)\n",
"\n",
"def f(x, b0, b1):\n",
" return b0 + b1 * np.log(x)\n",
"\n",
"xdata = np.linspace(0.01,5,50)\n",
"y = f(xdata, *beta)\n",
"ydata = y + 0.05 * np.random.randn(len(xdata))\n",
"\n",
"from scipy.optimize import curve_fit\n",
"beta_opt, beta_cov = curve_fit(f, xdata, ydata)\n",
"print(beta_opt)\n",
"\n",
"lin_dev = sum(beta_cov[0])\n",
"print(lin_dev)\n",
"\n",
"residuals = ydata - f(xdata, *beta_opt)\n",
"fres = sum(residuals**2)\n",
"print(fres)"
]
},
{
"cell_type": "code",
"execution_count": 488,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlUAAAG6CAYAAAAh/LN4AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAABWtElEQVR4nO3deXhU5d3G8e9MQjaSTEgmZIEAYd9kl13cQFDgldpaXFGrtK6VolXxteJa3Nr6Vq27olKq1oqIC4qgUjSyR0D2JBDIvk/2ZWbePyIDgeyZLcn9ua65OjlzzpzfJNq5fc5zfo/BbrfbEREREZE2MXq6ABEREZGOQKFKRERExAkUqkREREScQKFKRERExAkUqkREREScQKFKRERExAkUqkREREScwNfTBXRUNpuN9PR0QkJCMBgMni5HREREmsFut1NcXExsbCxGY8vGnhSqXCQ9PZ24uDhPlyEiIiKtcOzYMXr27NmiYxSqXCQkJASo/aOEhoZ6uBoRERFpDovFQlxcnON7vCUUqlzkxCW/0NBQhSoREZF2pjVTdzRRXURERMQJFKpEREREnEChSkRERMQJFKpEREREnEChSkRERMQJFKpEREREnE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uKucA0m+HMwycbTUjA0fwPzzo2mxpNGXJAaG5TAwtoT+/Q30Gx5ATR9/8ntEE9J/EOP7T3VZCHHFJU9P9YvqTDpVqLrtttvYs2cPmzZtcvp7L1mypM7olsViIS4uzunnERHxBm0NQad+gTe3rcGs4TFM6hXNqi9yOLgpm6J
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, y, 'r', lw=2)\n",
"ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)\n",
"ax.set_xlim(0,5)\n",
"ax.set_xlabel(r'$x$', fontsize=18)\n",
"ax.set_ylabel(r'$f(x, \\beta)$', fontsize=18)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Функция №4"
]
},
{
"cell_type": "code",
"execution_count": 489,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.8817479 1.92063655]\n",
"0.00010562358913983954\n",
"0.14631327681648387\n"
]
}
],
"source": [
"beta = (0.89, 1.92)\n",
"def f(x, b0, b1):\n",
" return b0 + np.float_power(x, b1)\n",
"\n",
"xdata = np.linspace(0,5,50)\n",
"y = f(xdata, *beta)\n",
"ydata = y + 0.05 * np.random.randn(len(xdata))\n",
"\n",
"from scipy.optimize import curve_fit\n",
"beta_opt, beta_cov = curve_fit(f, xdata, ydata)\n",
"print(beta_opt)\n",
"\n",
"lin_dev = sum(beta_cov[0])\n",
"print(lin_dev)\n",
"\n",
"residuals = ydata - f(xdata, *beta_opt)\n",
"fres = sum(residuals**2)\n",
"print(fres)"
]
},
{
"cell_type": "code",
"execution_count": 490,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkUAAAG6CAYAAAAGUjKQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0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L+J+YaZS6e4eHZ1o1HeKZo5cyYffvghX3/9NYGBgY51QsHBwfj7+xMcHMydd97JAw88QEhICEFBQfz2t78lPj6+WjvPRERE5LQlu1KYvXAPKelWfF7tSLotDIDhfhv499bhmLwsLp5h3WiUoWju3LkAjBo1yun4O++8w+233w7ASy+9hNls5pprrqGkpIRx48bx6quvNvBMRUREGrclu1K494Ot2A1o9V5LtpZ2B6CdKRH79ftZkdqW8a2CXTzLutEk6hTVB9UpEhERT2ezGwx/diUpuVaiP7ew7tB4AJqTz4gJH7G3d2sig/1Y+/BlblOpujbf341yTZGIiIjUv42J2aTkWolabXMEIhN2ruz3AXt6t8YAUnKtbEzMdu1E64hCkYiIiJxTer6VkD3lbF0/znHs+jbvsOGKtmeNawoUikREROScjGNlHF00jFIqStZMDPySDTeGnTUuPNCvoadWLxrlQmsRERGpX7kZpfx5OmQbrQAY4v0jibfbMSynd5qZqGj2OrhDI+zpcQ66UyQiIiJOyssMbuy3nz1F7QHoZDqAccsRCgP8HWMql1XPmtzDbRZZ15ZCkYiIiDh5YPQ2liRV9DQLIYu/P38CUxfnjhCRwX7MvaU/43tFuWKK9UKPz0RERDyYzW6wMTGb9Hwr4YF
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, y, 'r', lw=2)\n",
"ax.plot(xdata, f(xdata, *beta_opt), 'b', lw=2)\n",
"ax.set_xlim(0,5)\n",
"ax.set_xlabel(r'$x$', fontsize=18)\n",
"ax.set_ylabel(r'$f(x, \\beta)$', fontsize=18)\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2.1 Задание\n",
"\n",
"Постройте модель линейной регрессии для произвольных данных из двух \n",
"столбцов. Для примера можно взять точечную зависимость заработной платы от \n",
"опыта работы: \n",
"(https://raw.githubusercontent.com/AnnaShestova/salary-years-simple-linear-regression/master/Salary_Data.csv). \n",
"Найдите коэффициенты линии регрессии. Постройте прогноз. "
]
},
{
"cell_type": "code",
"execution_count": 491,
"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>YearsExperience</th>\n",
" <th>Salary</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>30.000000</td>\n",
" <td>30.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>5.313333</td>\n",
" <td>76003.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>2.837888</td>\n",
" <td>27414.429785</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>1.100000</td>\n",
" <td>37731.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>3.200000</td>\n",
" <td>56720.750000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>4.700000</td>\n",
" <td>65237.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>7.700000</td>\n",
" <td>100544.750000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>10.500000</td>\n",
" <td>122391.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" YearsExperience Salary\n",
"count 30.000000 30.000000\n",
"mean 5.313333 76003.000000\n",
"std 2.837888 27414.429785\n",
"min 1.100000 37731.000000\n",
"25% 3.200000 56720.750000\n",
"50% 4.700000 65237.000000\n",
"75% 7.700000 100544.750000\n",
"max 10.500000 122391.000000"
]
},
"execution_count": 491,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = 'https://raw.githubusercontent.com/AnnaShestova/salary-years-simple-linear-regression/master/Salary_Data.csv'\n",
"\n",
"ds = pd.read_csv(url)\n",
"\n",
"ds.describe()"
]
},
{
"cell_type": "code",
"execution_count": 492,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAlUAAAGwCAYAAACAZ5AeAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA+jElEQVR4nO3df3hT5f3/8Vda6A+hDRRt02rBDhEoRQEZtYI6tVgmdnZzU1hxDJnuy2BSUQfOQWETEdRNUCni9hnbwB/oNdGidp8OnAysLbRULCCiVkFsQCkk5UcLNuf7Rz+NhLbQhtMkbZ6P68q15Zw7J+9EJS/Ouc/7thiGYQgAAADnJMTfBQAAAHQGhCoAAAATEKoAAABMQKgCAAAwAaEKAADABIQqAAAAExCqAAAATNDF3wUEE5fLpS+//FJRUVGyWCz+LgcAALSCYRiqqalRQkKCQkJaPh9FqPKhL7/8UomJif4uAwAAeGHv3r266KKLWtxPqPKhqKgoSQ3/UKKjo/1cDQAAaA2n06nExET373hLCFU+1HjJLzo6mlAFAEAHc7apO0xUBwAAMAGhCgAAwASEKgAAABMQqgAAAExAqAIAADABoQoAAMAEhCoAAAATEKoAAABMQKgCAAAwAR3VAQBAwKp3GSqprNaBmlrFRkVoRFKMQkPO3NncXwhVAAAgIBVUVGle/g5VOWrd2+KtEcrNTNaYlHg/VtY8Lv8BAICAU1BRpSkryzwClSTZHbWasrJMBRVV7m31LkNFnxzUa+X7VPTJQdW7DF+XK4kzVQAAIMDUuwzNy9+h5qKRIckiaV7+Do1Otqlwhz1gzmZxpgoAAASUksrqJmeoTmVIqnLU6un1H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EuNMSitL69/F1Gh8flPwAAABNwpgoAgP/DwsI4F4QqAADEwsI4d1z+AwAEPRYWhhkIVQCAoMbCwjALoQoAENRYWBhmYU4VACCosbBw85i033aEKgBAUGNh4aaYtO8dLv8BAIIaCwt7YtK+9whVAICgxsLC32LS/rkhVAEAgh4LCzdg0v65YU4VAABiYWGJSfvnilAFAMD/CfaFhZm0f264/AcAACQxaf9cEaoAAIAkJu2fK0IVAABwY9K+95hTBQAAPDBp3zuEKgAA0ESwT9r3BqEKAAAfYT29zo1QBQCAD7CeXufHRHUAANoZ6+kFB0IVAADtiPX0ggehCgCAdsR6esGDUAUAQDtiPb3gQagCAKAdsZ5e8CBUAQDQjlhPL3gQqgAAaEespxc8CFUAALQz1tMLDjT/BADAB1hPr/MjVAEA4COsp9e5cfkPAADABIQqAAAAExCqAAAATECoAgAAMAET1QEACHD1LoO7BjsAQhUAAAGsoKJK8/J3eCzKHG+NUG5mMv2tAgyX/wAACFAFFVWasrLMI1BJkt1Rqykry1RQUeWnytAcQhUAAAGo3mVoXv4OGc3sa9w2L3+H6l3NjYA/EKoAAAhAJZXVTc5QncqQVOWoVUllte+KwhkRqgAACEAHaloOVN6MQ/sjVAEAEIBioyLOPqgN49D+CFUAAASgEUkxirdGqKXGCRY13AU4IinGl2XhDAhVAAAEoNAQi3IzkyWpSbBqfJ6bmUy/qgBCqAIAIECNSYlX3oRhslk9L/HZrBHKmzCMPlUBhuafAAAEsDEp8RqdbKOjegdAqAIAIMCFhliU1reXv8vAWXD5DwAAwASEKgAAABMQqgAAAExAqAIAADABoQoAAMAE3P0HAOh06l0GLQjgc4QqAECnUlBRpXn5O1Tl+Hah4XhrhHIzk2mWiXbF5T8AQKdRUFGlKSvLPAKVJNkdtZqyskwFFVV+qgzBgFAFAOgU6l2G5uX
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(ds['YearsExperience'], ds['Salary'])\n",
"plt.xlabel('Опыт (годы)')\n",
"plt.ylabel('Зарплата')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 493,
"metadata": {},
"outputs": [],
"source": [
"X = ds.iloc[:, :-1].values\n",
"y = ds.iloc[:, 1].values\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)"
]
},
{
"cell_type": "code",
"execution_count": 494,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"26780.099150628157\n",
"[9312.57512673]\n"
]
}
],
"source": [
"regressor = LinearRegression()\n",
"regressor.fit(X_train, y_train)\n",
"\n",
"print(regressor.intercept_)\n",
"print(regressor.coef_)"
]
},
{
"cell_type": "code",
"execution_count": 495,
"metadata": {},
"outputs": [],
"source": [
"y_pred = regressor.predict(X_test)\n",
"df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})"
]
},
{
"cell_type": "code",
"execution_count": 496,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkIAAAGYCAYAAACu6o3UAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA8hklEQVR4nO3dfVhUdf7/8dcMNwOogGhyU6hsd2qapqTRmmuFYLmtmrlZltSyuhlUyKZJX1PSyqTVvE3Wb5r1XS3tt+WausgsrlJJqChlpmatm+62g+56Q0jAyMzvDy9mHfEOPYjjeT6ui+tqznnP53zOmzPyas45Mxa32+0WAACACVmbegIAAABNhSAEAABMiyAEAABMiyAEAABMiyAEAABMiyAEAABMiyAEAABMiyAEAABMy7+pJ3A5c7lc+v7779WiRQtZLJamng4AADgPbrdbP/zwg2JiYmS1nv09H4LQWXz//feKjY1t6mkAAIALsH//fl1zzTVnrSEInUWLFi0knWhkaGhoE8/mzJxOp/Lz85WUlKSAgICmno7Poo/GoZfGoZfGoI/G8YVelpeXKzY21vN3/GwIQmdRdzosNDT0sg9CISEhCg0NvWwPSl9AH41DL41DL41BH43jS708n8tauFgaAACYFkEIAACYFkEIAACYFtcIATgjl8ulmpqaBj/P6XTK399fVVVVqq2tbYSZmcfF9DIgIEB+fn6NNDPgykAQAnBaNTU12rt3r1wuV4Of63a7FRUVpf379/MZXBfpYnsZHh6uqKgofg/AGRCEANTjdrv1r3/9S35+foqNjT3nB5KdyuVyqaKiQs2bN2/wc+HtQnvpdrtVWVmpAwcOSJKio6Mba4qATyMIAajn+PHjqqysVExMjEJCQhr8/LpTakFBQQShi3QxvQwODpYkHThwQG3atOE0GXAa/AsFoJ66a1ECAwObeCa4WHVB1ul0NvFMgMsTQQjAGXFdie/jdwicHUEIAACYFkEIAC4Bi8WiFStWNPU0AJyCi6UBnLf241df0u39/dUBF/S8oqIi9e7dW/3799fq1ec/5/bt2ysjI0MZGRkXtF0Avod3hABccRYuXKinnnpKhYWF+v7775t6OgAuYwQhAFeUiooKLVu2TKNHj9aAAQO0ePFir/UfffSRbr31VgUFBal169YaPHiwJKlv37767rvvNGbMGFksFs9FxtnZ2erWrZvXGDNnzlT79u09jzdv3qx+/fqpdevWCgsL089+9jNt3bq1MXcTgEE4NQacauo1kqvKuPGyjxo3Fs5p+fLl6tChg2688UY98sgjysjIUFZWliwWi1avXq3Bgwfrf/7nf/TOO++opqZGa9askSR98MEH6tq1q0aNGqWRI0c2aJs//PCDUlJSNGfOHLndbk2fPl333nuv9uzZoxYtWjTGbgIwCEEIPs3Ia1Zsfm7l9DRsODSRhQsX6pFHHpEk9e/fX0ePHtWGDRvUt29fvfzyyxo2bJhefPFFT33Xrl0lSREREfLz81OLFi0UFRXVoG3eddddXo8XLFig8PBwbdiwQT//+c8vco8ANCZOjQG4YuzevVubNm3SQw89JEny9/fXgw8+qIULF0qSSktLdffddxu+3bKyMo0cOVLXX3+9wsLCFBoaqoqKCu3bt8/wbQEwFu8IAbhiLFy4UMePH1dMTIxnmdvtls1m09y5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Pv27ZPT6dQDDzygLVu2aMmSJaqtrZXD4ZDD4fBcZ9CxY0f1799fI0eO1KZNm/Tpp58qPT1dw4YN83wI2sMPP6zAwEClpqZqx44dWrZsmWbNmuV12uqZZ55RXl6epk+frl27dik7O1tbtmxRenq6pBP/95SRkaGXXnpJK1e
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"df.plot(kind='bar')\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 497,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkIAAAGdCAYAAAD+JxxnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAAA9uUlEQVR4nO3de3RU1d3/8U8uMAmQC2BzQS6mPgjlUoVSY7z+WlJCF1JZ2lpjUIq0+lCsoFbxBog3MFC1qIBoVSoIhVZFELEpoAikAUO4RDBQG4EiSfoQMglIQsjs3x/HDDlMgASSnMnM+7XWrOXZZ5P5TlOdD9+9zzkhxhgjAACAIBTqdAEAAABOIQgBAICgRRACAABBiyAEAACCFkEIAAAELYIQAAAIWgQhAAAQtAhCAAAgaIU7XYA/83g8+vrrrxUVFaWQkBCnywEAAA1gjFFFRYW6dOmi0NAz93wIQmfw9ddfq1u3bk6XAQAAzsH+/fvVtWvXM84hCJ1BVFSUJOt/yOjoaIerAQAADVFeXq5u3bp5v8fPhCB0BrXLYdHR0QQhAABamYZsa2GzNAAACFoEIQAAELQIQgAAIGgRhAAAQNAiCAEAgKBFEAIAAEGLIAQAAIIWQQgAAAQtghAAAAhaBCEAABC0eMQGAABocTUeo02FpSqpqFRcVIQuT+qksNCzPxKjqRGEAABAi1qVf1BTl+/UQXeldywxJkJThvfR0H6JLVoLS2MAAKDFrMo/qLELtuhYUYke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CQpOztbsbGxGjRokHdOamqqQkNDlZOT451z7bXXqm3btt45aWlpKigo0OHDh71zUlNP3oSpdk52drYkqbCwUEVFRbY5MTExSk5O9s45VVVVlcrLy20vAABwGqtWSe3b28dKSqTRo52p5xw0axCqrKzUxIkTlZ6erujoaElSUVGR4uLibPPCw8PVqVMnFRUVeefEx8fb5tQen21O3fN1/1x9c041bdo0xcTEeF/dutV/fwMAAIKaMdKPfyz99Kcnx2691Rr/znecq+scNFsQqq6u1s033yxjjObMmdNcb9OkHn74Ybndbu9r//79TpcEAIB/2bvX2hC9du3JsY0bpYULnavpPDRLEKoNQXv37lVWVpa3GyRJCQkJKikpsc0/ceKESktLlZCQ4J1TXFxsm1N7fLY5dc/X/XP1zTmVy+VSdHS07QUAAL6VmSlddNHJ4w4dpOPHpZQUx0o6X00ehGpD0J49e/SPf/xDnTvbH5qWkpKisrIy5ebmesfWrFkjj8ej5ORk75x169apurraOycrK0u9evVSx44dvXNWr15t+9lZWVlK+faXkZSUpISEBNuc8vJy5eTkeOcAAIAGqKqyukATJ54cmzVLqqiQ2rRxrq6m0Nid2BUVFSYvL8/k5eUZSea5554zeXl5Zu/eveb48ePmZz/7menatavZunWrOXjwoPdV9wqwoUOHmgEDBpicnByzfv1607NnT5Oenu49X1ZWZuLj481tt91m8vPzzeLFi027du3MK6+84p2zYcMGEx4ebmbOnGl27dplpkyZYtq0aWN27NjhnTN9+nQTGxtrli1bZrZv325uuOEGk5SUZI4dO9agz8pVYwCAoPfxx75Xhf3nP05XdUaN+f5udBBau3atkeTzGjVqlCksLKz3nCSzdu1a7884dOiQSU9PNx06dDDR0dFm9OjRpqKiwvY+27ZtM1dffbVxuVzmwgsvNNOnT/epZcmSJeaSSy4xbdu2NX379jUffPCB7bzH4zGTJk0y8fHxxuVymcGDB5uCgoIGf1aCEAAgqN1wgz0AXX+90xU1SGO+v0OMMfU99wyyltJiYmLkdrvZLwQACB4HD0pdutjHVq+2rhRrBRrz/c3T5wEACDA1HqNNhaUqq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pDD6AgBANBAq/I
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(X_test, y_test)\n",
"plt.plot(X_test, y_pred, c='r')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3.2 Задание\n",
"\n",
"Постройте модель множественной линейной регрессии для произвольных \n",
"данных из нескольких столбцов. Для примера можно взять потребления \n",
"газа (в миллионах галлонов) в 48 штатах США или набор данных о \n",
"качестве красного вина (1) и (2) соответственно. Найдите коэффициенты \n",
"множественной регрессии. Постройте прогноз. "
]
},
{
"cell_type": "code",
"execution_count": 498,
"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>fixed acidity</th>\n",
" <th>volatile acidity</th>\n",
" <th>citric acid</th>\n",
" <th>residual sugar</th>\n",
" <th>chlorides</th>\n",
" <th>free sulfur dioxide</th>\n",
" <th>total sulfur dioxide</th>\n",
" <th>density</th>\n",
" <th>pH</th>\n",
" <th>sulphates</th>\n",
" <th>alcohol</th>\n",
" <th>quality</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>count</th>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" <td>1599.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>mean</th>\n",
" <td>8.319637</td>\n",
" <td>0.527821</td>\n",
" <td>0.270976</td>\n",
" <td>2.538806</td>\n",
" <td>0.087467</td>\n",
" <td>15.874922</td>\n",
" <td>46.467792</td>\n",
" <td>0.996747</td>\n",
" <td>3.311113</td>\n",
" <td>0.658149</td>\n",
" <td>10.422983</td>\n",
" <td>5.636023</td>\n",
" </tr>\n",
" <tr>\n",
" <th>std</th>\n",
" <td>1.741096</td>\n",
" <td>0.179060</td>\n",
" <td>0.194801</td>\n",
" <td>1.409928</td>\n",
" <td>0.047065</td>\n",
" <td>10.460157</td>\n",
" <td>32.895324</td>\n",
" <td>0.001887</td>\n",
" <td>0.154386</td>\n",
" <td>0.169507</td>\n",
" <td>1.065668</td>\n",
" <td>0.807569</td>\n",
" </tr>\n",
" <tr>\n",
" <th>min</th>\n",
" <td>4.600000</td>\n",
" <td>0.120000</td>\n",
" <td>0.000000</td>\n",
" <td>0.900000</td>\n",
" <td>0.012000</td>\n",
" <td>1.000000</td>\n",
" <td>6.000000</td>\n",
" <td>0.990070</td>\n",
" <td>2.740000</td>\n",
" <td>0.330000</td>\n",
" <td>8.400000</td>\n",
" <td>3.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25%</th>\n",
" <td>7.100000</td>\n",
" <td>0.390000</td>\n",
" <td>0.090000</td>\n",
" <td>1.900000</td>\n",
" <td>0.070000</td>\n",
" <td>7.000000</td>\n",
" <td>22.000000</td>\n",
" <td>0.995600</td>\n",
" <td>3.210000</td>\n",
" <td>0.550000</td>\n",
" <td>9.500000</td>\n",
" <td>5.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>50%</th>\n",
" <td>7.900000</td>\n",
" <td>0.520000</td>\n",
" <td>0.260000</td>\n",
" <td>2.200000</td>\n",
" <td>0.079000</td>\n",
" <td>14.000000</td>\n",
" <td>38.000000</td>\n",
" <td>0.996750</td>\n",
" <td>3.310000</td>\n",
" <td>0.620000</td>\n",
" <td>10.200000</td>\n",
" <td>6.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>75%</th>\n",
" <td>9.200000</td>\n",
" <td>0.640000</td>\n",
" <td>0.420000</td>\n",
" <td>2.600000</td>\n",
" <td>0.090000</td>\n",
" <td>21.000000</td>\n",
" <td>62.000000</td>\n",
" <td>0.997835</td>\n",
" <td>3.400000</td>\n",
" <td>0.730000</td>\n",
" <td>11.100000</td>\n",
" <td>6.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>max</th>\n",
" <td>15.900000</td>\n",
" <td>1.580000</td>\n",
" <td>1.000000</td>\n",
" <td>15.500000</td>\n",
" <td>0.611000</td>\n",
" <td>72.000000</td>\n",
" <td>289.000000</td>\n",
" <td>1.003690</td>\n",
" <td>4.010000</td>\n",
" <td>2.000000</td>\n",
" <td>14.900000</td>\n",
" <td>8.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" fixed acidity volatile acidity citric acid residual sugar \n",
"count 1599.000000 1599.000000 1599.000000 1599.000000 \\\n",
"mean 8.319637 0.527821 0.270976 2.538806 \n",
"std 1.741096 0.179060 0.194801 1.409928 \n",
"min 4.600000 0.120000 0.000000 0.900000 \n",
"25% 7.100000 0.390000 0.090000 1.900000 \n",
"50% 7.900000 0.520000 0.260000 2.200000 \n",
"75% 9.200000 0.640000 0.420000 2.600000 \n",
"max 15.900000 1.580000 1.000000 15.500000 \n",
"\n",
" chlorides free sulfur dioxide total sulfur dioxide density \n",
"count 1599.000000 1599.000000 1599.000000 1599.000000 \\\n",
"mean 0.087467 15.874922 46.467792 0.996747 \n",
"std 0.047065 10.460157 32.895324 0.001887 \n",
"min 0.012000 1.000000 6.000000 0.990070 \n",
"25% 0.070000 7.000000 22.000000 0.995600 \n",
"50% 0.079000 14.000000 38.000000 0.996750 \n",
"75% 0.090000 21.000000 62.000000 0.997835 \n",
"max 0.611000 72.000000 289.000000 1.003690 \n",
"\n",
" pH sulphates alcohol quality \n",
"count 1599.000000 1599.000000 1599.000000 1599.000000 \n",
"mean 3.311113 0.658149 10.422983 5.636023 \n",
"std 0.154386 0.169507 1.065668 0.807569 \n",
"min 2.740000 0.330000 8.400000 3.000000 \n",
"25% 3.210000 0.550000 9.500000 5.000000 \n",
"50% 3.310000 0.620000 10.200000 6.000000 \n",
"75% 3.400000 0.730000 11.100000 6.000000 \n",
"max 4.010000 2.000000 14.900000 8.000000 "
]
},
"execution_count": 498,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"url = 'https://raw.githubusercontent.com/aniruddhachoudhury/Red-Wine-Quality/master/winequality-red.csv'\n",
"\n",
"ds = pd.read_csv(url)\n",
"\n",
"ds.describe()"
]
},
{
"cell_type": "code",
"execution_count": 499,
"metadata": {},
"outputs": [],
"source": [
"X = ds[['fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar', 'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density', 'pH', 'sulphates', 'alcohol']]\n",
"y = ds['quality']\n",
"\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)"
]
},
{
"cell_type": "code",
"execution_count": 500,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"34.99871062872134\n",
"[ 4.12835075e-02 -1.14952802e+00 -1.77927063e-01 2.78700036e-02\n",
" -1.87340739e+00 2.68362616e-03 -2.77748370e-03 -3.15166657e+01\n",
" -2.54486051e-01 9.24040106e-01 2.67797417e-01]\n"
]
}
],
"source": [
"regressor = LinearRegression()\n",
"regressor.fit(X_train, y_train)\n",
"\n",
"print(regressor.intercept_)\n",
"print(regressor.coef_)"
]
},
{
"cell_type": "code",
"execution_count": 501,
"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>Coefficient</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>fixed acidity</th>\n",
" <td>0.041284</td>\n",
" </tr>\n",
" <tr>\n",
" <th>volatile acidity</th>\n",
" <td>-1.149528</td>\n",
" </tr>\n",
" <tr>\n",
" <th>citric acid</th>\n",
" <td>-0.177927</td>\n",
" </tr>\n",
" <tr>\n",
" <th>residual sugar</th>\n",
" <td>0.027870</td>\n",
" </tr>\n",
" <tr>\n",
" <th>chlorides</th>\n",
" <td>-1.873407</td>\n",
" </tr>\n",
" <tr>\n",
" <th>free sulfur dioxide</th>\n",
" <td>0.002684</td>\n",
" </tr>\n",
" <tr>\n",
" <th>total sulfur dioxide</th>\n",
" <td>-0.002777</td>\n",
" </tr>\n",
" <tr>\n",
" <th>density</th>\n",
" <td>-31.516666</td>\n",
" </tr>\n",
" <tr>\n",
" <th>pH</th>\n",
" <td>-0.254486</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sulphates</th>\n",
" <td>0.924040</td>\n",
" </tr>\n",
" <tr>\n",
" <th>alcohol</th>\n",
" <td>0.267797</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Coefficient\n",
"fixed acidity 0.041284\n",
"volatile acidity -1.149528\n",
"citric acid -0.177927\n",
"residual sugar 0.027870\n",
"chlorides -1.873407\n",
"free sulfur dioxide 0.002684\n",
"total sulfur dioxide -0.002777\n",
"density -31.516666\n",
"pH -0.254486\n",
"sulphates 0.924040\n",
"alcohol 0.267797"
]
},
"execution_count": 501,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(regressor.coef_, X.columns, columns=['Coefficient'])"
]
},
{
"cell_type": "code",
"execution_count": 502,
"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>Actual</th>\n",
" <th>Predicted</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1109</th>\n",
" <td>6</td>\n",
" <td>5.782930</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1032</th>\n",
" <td>5</td>\n",
" <td>5.036193</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1002</th>\n",
" <td>7</td>\n",
" <td>6.596989</td>\n",
" </tr>\n",
" <tr>\n",
" <th>487</th>\n",
" <td>6</td>\n",
" <td>5.339126</td>\n",
" </tr>\n",
" <tr>\n",
" <th>979</th>\n",
" <td>5</td>\n",
" <td>5.939529</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>794</th>\n",
" <td>6</td>\n",
" <td>6.559757</td>\n",
" </tr>\n",
" <tr>\n",
" <th>813</th>\n",
" <td>4</td>\n",
" <td>6.017576</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1322</th>\n",
" <td>5</td>\n",
" <td>6.251291</td>\n",
" </tr>\n",
" <tr>\n",
" <th>704</th>\n",
" <td>4</td>\n",
" <td>5.163926</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1023</th>\n",
" <td>6</td>\n",
" <td>6.367205</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>320 rows × 2 columns</p>\n",
"</div>"
],
"text/plain": [
" Actual Predicted\n",
"1109 6 5.782930\n",
"1032 5 5.036193\n",
"1002 7 6.596989\n",
"487 6 5.339126\n",
"979 5 5.939529\n",
"... ... ...\n",
"794 6 6.559757\n",
"813 4 6.017576\n",
"1322 5 6.251291\n",
"704 4 5.163926\n",
"1023 6 6.367205\n",
"\n",
"[320 rows x 2 columns]"
]
},
"execution_count": 502,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y_pred = regressor.predict(X_test)\n",
"df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Средняя квадратичная ошибка"
]
},
{
"cell_type": "code",
"execution_count": 503,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.38447119782012573"
]
},
"execution_count": 503,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"metrics.mean_squared_error(y_test,y_pred)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.3.2 Задание*\n",
"\n",
"Экспериментально получены N значений величины Y при \n",
"различных значениях величины X. Построить полиномы первой и второй \n",
"степени, аппроксимирующие результаты эксперимента, с применением \n",
"метода наименьших квадратов. Результаты выводятся в виде таблиц \n",
"значений и графиков, полученных полиномов. \n",
"\n",
"*Вариант:* 2"
]
},
{
"cell_type": "code",
"execution_count": 504,
"metadata": {},
"outputs": [],
"source": [
"# Линейная функция\n",
"def linear_func(x, b0, b1):\n",
" return b0 + b1 * x\n",
"\n",
"# Квадратичная функция\n",
"def quadratic_func(x, b0, b1, b2):\n",
" return b0 + b1 * x + b2 * x**2"
]
},
{
"cell_type": "code",
"execution_count": 505,
"metadata": {},
"outputs": [],
"source": [
"xdata = np.linspace(0, 1, 6)\n",
"ydata = np.asarray([5.0, 5.0, 4.0, 4.0, 6.0, 6.0])"
]
},
{
"cell_type": "code",
"execution_count": 506,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4.42857143 1.14285715]\n",
" x y\n",
"0 0.0 4.428571\n",
"1 0.2 4.657143\n",
"2 0.4 4.885714\n",
"3 0.6 5.114286\n",
"4 0.8 5.342857\n",
"5 1.0 5.571429\n"
]
}
],
"source": [
"beta_opt, beta_cov = curve_fit(linear_func, xdata, ydata, method='lm')\n",
"print(beta_opt)\n",
"df = pd.DataFrame({\"x\": xdata, \"y\": linear_func(xdata, *beta_opt)});\n",
"#df.columns=['xdata', 'ydata']\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 507,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAkAAAAGwCAYAAABB4NqyAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAABFG0lEQVR4nO3deVxV1f7/8fcBFEwBcWJQMpwrNVOvpNltkBuWX9PqVppJmqkpNnnLhlupTZq328OvQ06ZQ+mlbPDXYJZaNIlDDtepzIHCCbxRcMQSFPbvj/WVyxZQQGAD+/V8PM7jdvZa55zP2Tc7b9daey+PZVmWAAAAXMTH6QIAAAAqGwEIAAC4DgEIAAC4DgEIAAC4DgEIAAC4DgEIAAC4DgEIAAC4jp/TBVRFeXl5Onz4sAIDA+XxeJwuBwAAlIBlWTp27JgiIiLk43P2MR4CUBEOHz6syMhIp8sAAABlcODAATVr1uysfQhARQgMDJRkTmBQUJDD1QAAgJLwer2KjIzM/x0/GwJQEU5PewUFBRGAAACoZkqyfIVF0AAAwHUIQAAAwHUIQAAAwHUIQAAAwHUIQAAAwHUIQAAAwHUIQAAAwHUIQAAAwHUIQAAAwHW4EzQAwPVy8yxtSP5VR4+dUJPAAHWLaiBfHzbDLm9V6Tw7PgJ06NAh3XXXXWrYsKHq1KmjDh066LvvvjvraxITE9W5c2f5+/urVatWWrhwYaE+M2fO1EUXXaSAgABFR0drw4YNFfQNAADV2codR9Tzpc81cN46PZiwVQPnrVPPlz7Xyh1HnC6tRqlq59nRAPTbb7/pyiuvVK1atfTJJ59o165d+uc//6mQkJBiX5OcnKw+ffro2muv1datW/XQQw/p3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U1Vp06dNG3aNEVHR5eoJi6DBwB3qUp3KK7JKvo8l+b32/EAVBURgAAAqH6qzX2AAAAAnEAAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAArkMAAgAAruNoAJowYYI8Ho/t0a5du2L7X3PNNYX6ezwe9enTJ7/PkCFDCrX37t27Mr4OAACoJvycLuDSSy/V6tWr85/7+RVf0nvvvaecnJz85+np6brssst022232fr17t1bCxYsyH/u7+9fjhUDAIDqzvEA5Ofnp7CwsBL1bdCgge15QkKCLrjggkIByN/fv8TvCQAA3MfxNUB79uxRRESEWrRooUGDBiklJaXEr50/f74GDBigunXr2o4nJiaqSZMmatu2rUaNGqX09PSzvk92dra8Xq/tAQAAai6PZVmWUx/+ySefKCsrS23bttWRI0c0ceJEHTp0SDt27FBgYOBZX7thwwZFR0dr/fr16tatW/7x06NCUVFR2rdvn5588knVq1dPSUlJ8vX1LfK9JkyYoIkTJxY6npmZqaCgoPP7kgAAoFJ4vV4FBweX6Pfb0QB0poyMDDVv3lyvvPKKhg0bdta+I0eOVFJSkrZt23bWfvv371fLli21evVq9erVq8g+2dnZys7Ozn/u9XoVGRlJAAIAoBopTQByfAqsoPr166tNmzbau3fvWfsdP35cCQkJ5wxJktSiRQs1atTorO/p7++voKAg2wMAANRcVSoAZWVlad++fQoPDz9rv2XLlik7O1t33XXXOd/z4MGDSk9PP+d7AgAA93A0AD3yyCP68ssv9dNPP2nt2rW6+eab5evrq4EDB0qS4uLi9MQTTxR63fz589W/f381bNjQdjwrK0uPPvqo1q1bp59++klr1qxRv3791KpVK8XGxlbKdwIAAFWfo5fBHzx4UAMHDlR6eroaN26snj17at26dWrcuLEkKSUlRT4+9oy2e/duffPNN/rss88KvZ+vr6+2bdumRYsWKSMjQxEREbr++uv13HPPcS8gAACQr0otgq4qSrOICgAAVA3VdhE0AABAZSAAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQAA1yEAAQCASrN9uzRypLRkibN1+Dn78QAAoKY7dUr64ANp+nQpMdEcW79euvNOyeNxpiYCEAAAqBC//CK99pr06qvSgQP2tn37pJ9+kqKiHCmNAAQAAMrXli1mtGfpUik7297Wpo00Zox0991SUJAz9UkEIAAAUA5OnpTee88En2+/tbd5PFKfPib4/OUvkk8VWIFMAAIAAGWWlibNnSvNni0dPmxvCw6Whg2TRo+WWrZ0pr7iEIAAAECpbdhgRnveflvKybG3XXqpdP/90l13SXXrOlPfuRCAAABAiWRnS8uWmeCzYYO9zcdHuukmE3yuvda5q7tKigAEAADO6vBhM8U1Z4509Ki9rUEDafhwadQoqXlzZ+orCwIQAAAoxLKktWvNaM+775p7+RTUqZMZ7Rk4UKpTx5ESz4uj67AnTJggj8dje7Rr167Y/gsXLizUPyAgwNbHsiw988wzCg8PV506dRQTE6M9e/ZU9FcBAKBG+OMPacECqUsXqWdP6a23/ht+fH2l226Tvv5a2rxZuuee6hl+pCowAnTppZdq9erV+c/9/M5eUlBQkHbv3p3/3HPGJOOUKVM0bdo0LVq0SFFRUXr66acVGxurXbt2FQpLAADASEmRZs2S5s2T0tPtbY0bSyNGSPfdJzVr5kx95c3xAOTn56ewsLAS9/d4PMX
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, linear_func(xdata, *beta_opt), 'b', lw=2)\n",
"plt.xlabel('x')\n",
"plt.ylabel('linear')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 508,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 5.14285714 -4.2142857 5.35714285]\n",
" x y\n",
"0 0.0 5.142857\n",
"1 0.2 4.514286\n",
"2 0.4 4.314286\n",
"3 0.6 4.542857\n",
"4 0.8 5.200000\n",
"5 1.0 6.285714\n"
]
}
],
"source": [
"beta_opt, beta_cov = curve_fit(quadratic_func, xdata, ydata, method='lm')\n",
"print(beta_opt)\n",
"df = pd.DataFrame({\"x\": xdata, \"y\": quadratic_func(xdata, *beta_opt)});\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 509,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjcAAAGwCAYAAABVdURTAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy80BEi2AAAACXBIWXMAAA9hAAAPYQGoP6dpAABBxElEQVR4nO3de5yN5f7/8feaGTODPTPkLJNNOcsWNgnpoIiEKMmxLUr6VpRQbKdCQu2d0jk6qii7EJVCRdu57VDKKcJQYcZxxszcvz+u38xqmJlmxlrrWuter+fjsR5d15p7Zj5zh/s9133d1+VxHMcRAACAS0TYLgAAAMCXCDcAAMBVCDcAAMBVCDcAAMBVCDcAAMBVCDcAAMBVCDcAAMBVomwXEGiZmZnav3+/4uLi5PF4bJcDAAAKwHEcHTt2TJUrV1ZERP5jM2EXbvbv36/ExETbZQAAgCLYu3evqlSpku8xYRdu4uLiJJmTEx8fb7kaAABQECkpKUpMTMy+jucn7MJN1q2o+Ph4wg0AACGmIFNKmFAMAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAABchXADAAB8Yt8+6corpRUr7NZBuAEAAD7x6KPSV19JrVtLr7xirw7CDQAAOG/r1kmzZ5t26d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QbgAACDIZGWbBviyTJpnbUigYwg0AAEHm1VelTZtMu0k
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig, ax = plt.subplots()\n",
"ax.scatter(xdata, ydata)\n",
"ax.plot(xdata, quadratic_func(xdata, *beta_opt), 'b', lw=2)\n",
"plt.xlabel('x')\n",
"plt.ylabel('linear')\n",
"plt.show()"
]
}
],
"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.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}