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

194 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.7.0"
4 years ago
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python37364bit8d3b438fb5fc4430a93ac2cb74d693a7",
"display_name": "Python 3.7.0 64-bit ('3.7')"
},
"metadata": {
"interpreter": {
"hash": "70b38d7a306a849643e446cd70466270a13445e5987dfa1344ef2b127438fa4d"
}
4 years ago
}
},
"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(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"
4 years ago
]
},
"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;white-space:pre;}\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=\"m789fb0d015\" style=\"stroke:#000000;\"/>\n </defs>\n <g clip-path=\"url(#pba36295171)\">\n <use style=\"stroke:#000000;\" x=\"127.932027\" xlink:href=\"#m789fb0d015\" y=\"148.433275\"/>\n <use style=\"stroke:#000000;\" x=\"139.018575\" xlink:href=\"#m789fb0d015\" y=\"174.186365\"/>\n <use style=\"stroke:#000000;\" x=\"262.356425\" xlink:href=\"#m789fb0d015\" y=\"180.624637\"/>\n <use style=\"stroke:#000000;\" x=\"152.876761\" xlink:href=\"#m789fb0d015\" y=\"181.268465\"/>\n <use style=\"stroke:#000000;\" x=\"295.616069\" xlink:href=\"#m789fb0d015\" y=\"140.707349\"/>\n <use style=\"stroke:#000000;\" x=\"240.183328\" xlink:href=\"#m789fb0d015\" y=\"88.557342\"/>\n <use style=\"stroke:#000000;\" x=\"233.254235\" xlink:href=\"#m789fb0d015\" y=\"147.789448\"/>\n <use style=\"stroke:#000000;\" x=\"139.018575\" xlink:href=\"#m789fb0d015\" y=\"115.598086\"/>\n <use style=\"stroke:#000000;\" x=\"111.302205\" xlink:href=\"#m789fb0d015\" y=\"174.830192\"/>\n <use style=\"stroke:#000000;\" x=\"96.058201\" xlink:href=\"#m789fb0d015\" y=\"196.720318\"/>\n <use style=\"stroke:#000000;\" x=\"137.632757\" xlink:href=\"#m789fb0d015\" y=\"174.186365\"/>\n <use style=\"stroke:#000000;\" x=\"104.373112\" xlink:href=\"#m789fb0d015\" y=\"143.926485\"/>\n <use style=\"stroke:#000000;\" x=\"161.191672\" xlink:href=\"#m789fb0d015\" y=\"108.515987\"/>\n <use style=\"stroke:#000000;\" x=\"101.601475\" xlink:href=\"#m789fb0d015\" y=\"190.282046\"/>\n <use style=\"stroke:#000000;\" x=\"144.56185\" xlink:href=\"#m789fb0d015\" y=\"188.994391\"/>\n <use style=\"stroke:#000000;\" x=\"147.333487\" xlink:href=\"#m789fb0d015\" y=\"130.406113\"/>\n <use style=\"stroke:#000000;\" x=\"158.420035\" xlink:href=\"#m789fb0d015\" y=\"91.776479\"/>\n <use style=\"stroke:#000000;\" x=\"165.349127\" xlink:href=\"#m789fb0d015\" y=\"210.884517\"/>\n <use style=\"stroke:#000000;\" x=\"137.632757\" xlink:href=\"#m789fb0d015\" y=\"186.419082\"/>\n <use style=\"stroke:#000000;\" x=\"145.947668\" xlink:href=\"#m789fb0d015\" y=\"134.269076\"/>\n <use style=\"stroke:#000000;\" x=\"115.45966\" xlink:href=\"#m789fb0d015\" y=\"141.995003\"/>\n <use style=\"stroke:#000000;\" x=\"145.947668\" xlink:href=\"#m789fb0d015\" y=\"185.131428\"/>\n <use style=\"stroke:#000000;\" x=\"199.994591\" xlink:href=\"#m789fb0d015\" y=\"158.090684\"/>\n <use style=\"stroke:#000000;\" x=\"274.828791\" xlink:href=\"#m789fb0d015\" y=\"17.092519\"/>\n <use style=\"stroke:#000000;\" x=\"105.758931\" xlink:href=\"#m789fb0d015\" y=\"192.213528\"/>\n <use style=\"stroke:#000000;\" x=\"169.506583\" xlink:href=\"#m789fb0d015\" y=\"156.159202\"/>\n <use style=\"stroke:#000000;\" x=\"80.814197
"image/png": "iVBORw0KGgoAAAANSUhEUgAA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Sl/UsX4JgNvM9UMwu0H1SbBBlWSYtD3TpMhi7NyKrVtVzzrLWdSB4wr7nPzgJO5ibLdHRI4EcD+AxwC0zeTPAjgLwGHmD2QSwCdV9RnzmCaA8wBsA/AZVf2x0zUGBwd1fHzc0Q5C4qJer2NqampOeq1Ww+TkZPIGxUTW8/n668DppwN33eW015EAHpiVkhX700BElqvqoOVGO9VPcqHnTtLEaaKOIoVqslpDeeUV1aOOcvbUx8fjq3nkOSQHDj9AiD12sehuMcmCEIYlS0LmZSq7xx9/Y/84+ttntcDzCsWdEAesXnA7LzELH+7knY0bVQ880FnUraayi0OIs/yBlhecxJ1jy5DS02g0MDo6ilqtBhFBrVYzPB8L3Ma7IfY89xyw555AtQpMTMzdvssuwJo1hrwfeODc7VbPaXR0NNQ3EUHHNcoDrg2qScAGVZI17BofK5UK2u02BgYGMDIywo+tPPDUU8Db3w5MT1tvX7QI+M1vgD32SNYuIPuNzG44NajScyfEAqtR/gBg+/btUFVMTU1haGgos6MjZoGJCeNr0oEBa2F/61uBjRuBp59OR9iBjI7mGBEUd0Is6A4BVCqVOftMT0+j2WymYF22efxxQ9QPOsh6++AgsHkzsGoVMH9+srZ1E0eoJyswLEOIB3p6eizj8CKCdrttcUT5WL7cEG473vc+4Ec/AnbaKTGTCg/DMoR4wGlQLafBs8o4wUon999veOp2wv6hDwFbtgDLllHYE8WuG02SC7tCkrRx62Znt314eDjX/aTDcPfdzt0ZGw1jKAESH2A/d0Kc8dLf2eo
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": []
}
]
}