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.
ML-For-Beginners/2-Regression/1-Tools/solution/notebook.ipynb

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 for North American Pumpkins - Lesson 1"
4 years ago
],
"cell_type": "markdown",
"metadata": {}
},
{
"source": [
"Import needed libraries"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 1,
4 years ago
"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": 2,
4 years ago
"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": 3,
4 years ago
"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": 4,
4 years ago
"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": 5,
4 years ago
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"LinearRegression()"
]
},
"metadata": {},
"execution_count": 5
4 years ago
}
],
"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": 6,
4 years ago
"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": 7,
4 years ago
"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=\"m24aebb984e\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#p966a7f7ee7)\">\n <use style=\"stroke:#000000;\" x=\"137.939547\" xlink:href=\"#m24aebb984e\" y=\"196.520872\"/>\n <use style=\"stroke:#000000;\" x=\"163.904217\" xlink:href=\"#m24aebb984e\" y=\"95.860959\"/>\n <use style=\"stroke:#000000;\" x=\"217.276039\" xlink:href=\"#m24aebb984e\" y=\"108.990513\"/>\n <use style=\"stroke:#000000;\" x=\"197.081296\" xlink:href=\"#m24aebb984e\" y=\"38.236806\"/>\n <use style=\"stroke:#000000;\" x=\"100.435023\" xlink:href=\"#m24aebb984e\" y=\"188.497256\"/>\n <use style=\"stroke:#000000;\" x=\"81.682761\" xlink:href=\"#m24aebb984e\" y=\"173.179443\"/>\n <use style=\"stroke:#000000;\" x=\"135.054583\" xlink:href=\"#m24aebb984e\" y=\"171.720604\"/>\n <use style=\"stroke:#000000;\" x=\"107.647431\" xlink:href=\"#m24aebb984e\" y=\"190.685515\"/>\n <use style=\"stroke:#000000;\" x=\"244.683192\" xlink:href=\"#m24aebb984e\" y=\"44.801583\"/>\n <use style=\"stroke:#000000;\" x=\"207.178668\" xlink:href=\"#m24aebb984e\" y=\"137.43788\"/>\n <use style=\"stroke:#000000;\" x=\"166.78918\" xlink:href=\"#m24aebb984e\" y=\"108.261094\"/>\n <use style=\"stroke:#000000;\" x=\"250.453118\" xlink:href=\"#m24aebb984e\" y=\"65.954753\"/>\n <use style=\"stroke:#000000;\" x=\"113.417358\" xlink:href=\"#m24aebb984e\" y=\"168.073506\"/>\n <use style=\"stroke:#000000;\" x=\"250.453118\" xlink:href=\"#m24aebb984e\" y=\"64.495914\"/>\n <use style=\"stroke:#000000;\" x=\"241.798228\" xlink:href=\"#m24aebb984e\" y=\"106.802254\"/>\n <use style=\"stroke:#000000;\" x=\"161.019254\" xlink:href=\"#m24aebb984e\" y=\"145.461496\"/>\n <use style=\"stroke:#000000;\" x=\"117.744803\" xlink:href=\"#m24aebb984e\" y=\"92.943281\"/>\n <use style=\"stroke:#000000;\" x=\"166.78918\" xlink:href=\"#m24aebb984e\" y=\"163.696988\"/>\n <use style=\"stroke:#000000;\" x=\"126.399693\" xlink:href=\"#m24aebb984e\" y=\"179.74422\"/>\n <use style=\"stroke:#000000;\" x=\"70.142907\" xlink:href=\"#m24aebb984e\" y=\"208.191587\"/>\n <use style=\"stroke:#000000;\" x=\"287.957642\" xlink:href=\"#m24aebb984e\" y=\"39.695646\"/>\n <use style=\"stroke:#000000;\" x=\"220.161003\" xlink:href=\"#m24aebb984e\" y=\"121.390647\"/>\n <use style=\"stroke:#000000;\" x=\"181.213997\" xlink:href=\"#m24aebb984e\" y=\"150.567434\"/>\n <use style=\"stroke:#000000;\" x=\"71.585389\" xlink:href=\"#m24aebb984e\" y=\"139.626139\"/>\n <use style=\"stroke:#000000;\" x=\"175.44407\" xlink:href=\"#m24aebb984e\" y=\"208.191587\"/>\n <use style=\"stroke:#000000;\" x=\"78.797797\" xlink:href=\"#m24aebb984e\" y=\"203.085649\"/>\n <use style=\"stroke:#000000;\" x=\"122.072248\" xlink:href=\"#m24aebb9
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9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6zfrFU4/EYYz4DvElEzjTGHAP8gGqc/XBgDfByEZl0O4fmuSutQNK5/gsWLGDXrl117Vi9enVuxz/27oW3vx3uust9n//9X3jzm5OzKS688tx9B1SBk4CzgQ3GmHXOuv8DfNgY83qqHkEZ+CSAiGw0xlwL3A9MAJ/yEnZFaRW6u7vrTpgV13hGPWEHqFQq9PX1AeRK4HfvhhNPhAcecN9n/Xp47WuTsylN9AlVRUmIpJ8C9XojE2RrFkYvHn20KtgubRkAmzfD0UcnZ1NS6BOqimIBST8F2tXV5bndllfuNcvWrdXMlyVL6gt7Vxc88kg1up5HYfdDxV1RYsAtSyXJl0x/4xvfYM6cOa7bs5reeu+9VVF3E+yXv7w63/rOnXD44cnaZhMq7kruSDv9L42Ux3oUi0W+973v1fXgs5je+pvfVEX9da+rv/2EE+Dpp6sxd59OS2vglkaT5KKpkEpU2DCHjY3T58aV3ppE2uzPf+6dzvjud1fTHrNGFNcOnc9daRWiEtYwP7w8vSPUi7gb0v/6L29RP/tskf37IzlV4kR17VTclZYhCmEN+8Oz0XOPg7jq+Z3veIv68uV2T+YVhKiunYq7Yi1Rd+uj+NGELcOG0FASRN1D+dKXvEX9i1+MuAIpEtW1U3FXrCQOEYy
4 years ago
},
"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": []
}
]
}