{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "n_samples = 100\n",
    "xs = np.linspace(0, 10, n_samples)\n",
    "ys = xs**3 + np.random.randn(n_samples)*100 + 100\n",
    "\n",
    "X = xs.reshape(-1, 1)\n",
    "Y = ys.reshape(-1, 1)\n",
    "\n",
    "print(f'X[:5] =')\n",
    "print(X[:5])\n",
    "print()\n",
    "print(f'Y[:5] =')\n",
    "print(Y[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "plt.figure(figsize=(15, 10))\n",
    "plt.scatter(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_regressions(models, X, Y):\n",
    "    \"\"\"\n",
    "    Display multiple polynomial regression lines of various degrees\n",
    "    among a scatter plot of x, y values.\n",
    "    @param models the dictionary of regression models keyed by degree\n",
    "    @param X the x values as a numpy column vector.\n",
    "    @param Y the y values as a numpy column vector.\n",
    "    \"\"\"\n",
    "    plt.figure(figsize=(15, 10))\n",
    "    plt.scatter(X, Y);\n",
    "\n",
    "    linear_reg = LinearRegression()\n",
    "\n",
    "    for deg, X_poly in models.items():\n",
    "        linear_reg.fit(X_poly, Y)\n",
    "        Y_preds = linear_reg.predict(X_poly)\n",
    "        plt.plot(X, Y_preds, label=f'{deg}');\n",
    "        \n",
    "        print(f'Degree {deg} r2_score = {r2_score(Y, Y_preds):.4f}')\n",
    "        \n",
    "    plt.legend(loc='upper left')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "models = {}\n",
    "\n",
    "for deg in [1, 2, 3, 5, 9]:\n",
    "    poly_features = PolynomialFeatures(degree=deg)\n",
    "    X_poly = poly_features.fit_transform(X)\n",
    "    models[deg] = X_poly\n",
    "    \n",
    "show_regressions(models, X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
