You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
44 KiB
190 lines
44 KiB
4 years ago
|
{
|
||
|
"metadata": {
|
||
|
"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.3-final"
|
||
|
},
|
||
|
"orig_nbformat": 2,
|
||
|
"kernelspec": {
|
||
|
"name": "python3",
|
||
|
"display_name": "Python 3",
|
||
|
"language": "python"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2,
|
||
|
"cells": [
|
||
|
{
|
||
|
"source": [
|
||
|
"## Linear Regression Solution"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Import needed libraries"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"import matplotlib.pyplot as plt\n",
|
||
|
"import numpy as np\n",
|
||
|
"from sklearn import datasets, linear_model, model_selection\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Load the diabetes dataset, divided into `X` data and `y` features"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"name": "stdout",
|
||
|
"text": [
|
||
|
"(442, 10)\n[ 0.03807591 0.05068012 0.06169621 0.02187235 -0.0442235 -0.03482076\n -0.04340085 -0.00259226 0.01990842 -0.01764613]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"X, y = datasets.load_diabetes(return_X_y=True)\n",
|
||
|
"print(X.shape)\n",
|
||
|
"print(X[0])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Select just one feature to target for this exercise"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"X = X[:, np.newaxis, 2]\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Split the training and test data for both `X` and `y`"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.33)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Select the model and fit it with the training data"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "execute_result",
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"LinearRegression()"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"execution_count": 6
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"model = linear_model.LinearRegression()\n",
|
||
|
"model.fit(X_train, y_train)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Use test data to predict a line"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y_pred = model.predict(X_test)\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"source": [
|
||
|
"Display the results in a plot"
|
||
|
],
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {}
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "display_data",
|
||
|
"data": {
|
||
|
"text/plain": "<Figure size 432x288 with 1 Axes>",
|
||
|
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n<!-- Created with matplotlib (https://matplotlib.org/) -->\n<svg height=\"248.518125pt\" version=\"1.1\" viewBox=\"0 0 375.2875 248.518125\" width=\"375.2875pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n <defs>\n <style type=\"text/css\">\n*{stroke-linecap:butt;stroke-linejoin:round;}\n </style>\n </defs>\n <g id=\"figure_1\">\n <g id=\"patch_1\">\n <path d=\"M 0 248.518125 \nL 375.2875 248.518125 \nL 375.2875 0 \nL 0 0 \nz\n\" style=\"fill:none;\"/>\n </g>\n <g id=\"axes_1\">\n <g id=\"patch_2\">\n <path d=\"M 33.2875 224.64 \nL 368.0875 224.64 \nL 368.0875 7.2 \nL 33.2875 7.2 \nz\n\" style=\"fill:#ffffff;\"/>\n </g>\n <g id=\"PathCollection_1\">\n <defs>\n <path d=\"M 0 3 \nC 0.795609 3 1.55874 2.683901 2.12132 2.12132 \nC 2.683901 1.55874 3 0.795609 3 0 \nC 3 -0.795609 2.683901 -1.55874 2.12132 -2.12132 \nC 1.55874 -2.683901 0.795609 -3 0 -3 \nC -0.795609 -3 -1.55874 -2.683901 -2.12132 -2.12132 \nC -2.683901 -1.55874 -3 -0.795609 -3 0 \nC -3 0.795609 -2.683901 1.55874 -2.12132 2.12132 \nC -1.55874 2.683901 -0.795609 3 0 3 \nz\n\" id=\"m87062d904c\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#pe67797de08)\">\n <use style=\"stroke:#000000;\" x=\"165.453286\" xlink:href=\"#m87062d904c\" y=\"93.400322\"/>\n <use style=\"stroke:#000000;\" x=\"148.960675\" xlink:href=\"#m87062d904c\" y=\"167.214822\"/>\n <use style=\"stroke:#000000;\" x=\"348.371333\" xlink:href=\"#m87062d904c\" y=\"17.083636\"/>\n <use style=\"stroke:#000000;\" x=\"157.956645\" xlink:href=\"#m87062d904c\" y=\"111.541174\"/>\n <use style=\"stroke:#000000;\" x=\"139.964706\" xlink:href=\"#m87062d904c\" y=\"177.849114\"/>\n <use style=\"stroke:#000000;\" x=\"138.465377\" xlink:href=\"#m87062d904c\" y=\"184.10458\"/>\n <use style=\"stroke:#000000;\" x=\"81.490903\" xlink:href=\"#m87062d904c\" y=\"182.22794\"/>\n <use style=\"stroke:#000000;\" x=\"277.902905\" xlink:href=\"#m87062d904c\" y=\"58.369712\"/>\n <use style=\"stroke:#000000;\" x=\"205.935149\" xlink:href=\"#m87062d904c\" y=\"85.268216\"/>\n <use style=\"stroke:#000000;\" x=\"120.473438\" xlink:href=\"#m87062d904c\" y=\"205.373165\"/>\n <use style=\"stroke:#000000;\" x=\"171.450599\" xlink:href=\"#m87062d904c\" y=\"114.668907\"/>\n <use style=\"stroke:#000000;\" x=\"202.936492\" xlink:href=\"#m87062d904c\" y=\"54.616433\"/>\n <use style=\"stroke:#000000;\" x=\"171.450599\" xlink:href=\"#m87062d904c\" y=\"162.210449\"/>\n <use style=\"stroke:#000000;\" x=\"258.411638\" xlink:href=\"#m87062d904c\" y=\"78.387204\"/>\n <use style=\"stroke:#000000;\" x=\"193.940523\" xlink:href=\"#m87062d904c\" y=\"92.774776\"/>\n <use style=\"stroke:#000000;\" x=\"193.940523\" xlink:href=\"#m87062d904c\" y=\"141.567411\"/>\n <use style=\"stroke:#000000;\" x=\"183.445225\" xlink:href=\"#m87062d904c\" y=\"148.448423\"/>\n <use style=\"stroke:#000000;\" x=\"159.455973\" xlink:href=\"#m87062d904c\" y=\"169.717008\"/>\n <use style=\"stroke:#000000;\" x=\"238.920371\" xlink:href=\"#m87062d904c\" y=\"121.549919\"/>\n <use style=\"stroke:#000000;\" x=\"130.968736\" xlink:href=\"#m87062d904c\" y=\"195.989965\"/>\n <use style=\"stroke:#000000;\" x=\"133.967393\" xlink:href=\"#m87062d904c\" y=\"175.346928\"/>\n <use style=\"stroke:#000000;\" x=\"166.952614\" xlink:href=\"#m87062d904c\" y=\"157.831623\"/>\n <use style=\"stroke:#000000;\" x=\"163.953958\" xlink:href=\"#m87062d904c\" y=\"197.866605\"/>\n <use style=\"stroke:#000000;\" x=\"351.36999\" xlink:href=\"#m87062d904c\" y=\"37.726674\"/>\n <use style=\"stroke:#000000;\" x=\"141.464034\" xlink:href=\"#m87062d904c\" y=\"140.316318\"/>\n <use style=\"stroke:#000000;\" x=\"121.972766\" xlink:href=\"#m87062d904c\" y=\"159.708262\"/>\n <use style=\"stroke:#000000;\" x=\"177.447912\" xlink:href=\"#m870
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO2dfZBc1Xmnn1ejGeIx5kMtoYiP6cG7tjfISWysxVBOiOOEJahSsWNwyutxNgWsx6FCLWbDpiRPOZBkZ20QzrpcdgXGBVXgGXBRwSkLr2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CGKOkz6wVvvje9xbXrm7IzOduZqPAu4FHgl1Xm9keM7vdzE4N9p0BPNd22vPBvuVljZvZLjPbdeDAgTTNEAVShG+wjEROvuHLuEZdeeMN+NSnWqJ9/vnhwn755fCTn7QSffku7LGEmfTLN+BE4FHgw8H79cAArRvEJHB7sP/LwMfbzrsNuDSqbLll/KUIn3u/zwj1aVyjbuze3UrMFed62bmz7JZ2B73OUDWzQeBeYMY59/XgprDfOXfUObcAfAU4Lzj8eVqDsIucCfww3S1H+MLY2BhTU1M0m03MjGazmbm7pN9nhJb55FLHJ4a49LqLXHYZvPJKS94/8IFi21gIYarvjlveBtwJfGHZ/g1tr68Fvha83sjSAdVn0YCqiKDfLdeynlzq1u9J0uuCc9u3l93S7KDHaJlfCv7xe2gLewS+Cjwe7N++TOwnaEXJ7AUuiatD4i76eUZoWVPp65D9Mml63Ysvbs00rRtR4q4ZqkKUTFnRQkVEQuXFc8/BJZfAk09GHzczAx/
|
||
|
},
|
||
|
"metadata": {
|
||
|
"needs_background": "light"
|
||
|
}
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.scatter(X_test, y_test, color='black')\n",
|
||
|
"plt.plot(X_test, y_pred, color='blue', linewidth=3)\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
]
|
||
|
}
|