diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 1970c46..025d139 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Libraries" + "# Libraries\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "from sklearn import datasets as ds" ] }, { @@ -37,11 +41,100 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "\n", + "diabetes = ds.load_diabetes()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'data': array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n", + " 0.01990842, -0.01764613],\n", + " [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n", + " -0.06832974, -0.09220405],\n", + " [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n", + " 0.00286377, -0.02593034],\n", + " ...,\n", + " [ 0.04170844, 0.05068012, -0.01590626, ..., -0.01107952,\n", + " -0.04687948, 0.01549073],\n", + " [-0.04547248, -0.04464164, 0.03906215, ..., 0.02655962,\n", + " 0.04452837, -0.02593034],\n", + " [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,\n", + " -0.00421986, 0.00306441]]),\n", + " 'target': array([151., 75., 141., 206., 135., 97., 138., 63., 110., 310., 101.,\n", + " 69., 179., 185., 118., 171., 166., 144., 97., 168., 68., 49.,\n", + " 68., 245., 184., 202., 137., 85., 131., 283., 129., 59., 341.,\n", + " 87., 65., 102., 265., 276., 252., 90., 100., 55., 61., 92.,\n", + " 259., 53., 190., 142., 75., 142., 155., 225., 59., 104., 182.,\n", + " 128., 52., 37., 170., 170., 61., 144., 52., 128., 71., 163.,\n", + " 150., 97., 160., 178., 48., 270., 202., 111., 85., 42., 170.,\n", + " 200., 252., 113., 143., 51., 52., 210., 65., 141., 55., 134.,\n", + " 42., 111., 98., 164., 48., 96., 90., 162., 150., 279., 92.,\n", + " 83., 128., 102., 302., 198., 95., 53., 134., 144., 232., 81.,\n", + " 104., 59., 246., 297., 258., 229., 275., 281., 179., 200., 200.,\n", + " 173., 180., 84., 121., 161., 99., 109., 115., 268., 274., 158.,\n", + " 107., 83., 103., 272., 85., 280., 336., 281., 118., 317., 235.,\n", + " 60., 174., 259., 178., 128., 96., 126., 288., 88., 292., 71.,\n", + " 197., 186., 25., 84., 96., 195., 53., 217., 172., 131., 214.,\n", + " 59., 70., 220., 268., 152., 47., 74., 295., 101., 151., 127.,\n", + " 237., 225., 81., 151., 107., 64., 138., 185., 265., 101., 137.,\n", + " 143., 141., 79., 292., 178., 91., 116., 86., 122., 72., 129.,\n", + " 142., 90., 158., 39., 196., 222., 277., 99., 196., 202., 155.,\n", + " 77., 191., 70., 73., 49., 65., 263., 248., 296., 214., 185.,\n", + " 78., 93., 252., 150., 77., 208., 77., 108., 160., 53., 220.,\n", + " 154., 259., 90., 246., 124., 67., 72., 257., 262., 275., 177.,\n", + " 71., 47., 187., 125., 78., 51., 258., 215., 303., 243., 91.,\n", + " 150., 310., 153., 346., 63., 89., 50., 39., 103., 308., 116.,\n", + " 145., 74., 45., 115., 264., 87., 202., 127., 182., 241., 66.,\n", + " 94., 283., 64., 102., 200., 265., 94., 230., 181., 156., 233.,\n", + " 60., 219., 80., 68., 332., 248., 84., 200., 55., 85., 89.,\n", + " 31., 129., 83., 275., 65., 198., 236., 253., 124., 44., 172.,\n", + " 114., 142., 109., 180., 144., 163., 147., 97., 220., 190., 109.,\n", + " 191., 122., 230., 242., 248., 249., 192., 131., 237., 78., 135.,\n", + " 244., 199., 270., 164., 72., 96., 306., 91., 214., 95., 216.,\n", + " 263., 178., 113., 200., 139., 139., 88., 148., 88., 243., 71.,\n", + " 77., 109., 272., 60., 54., 221., 90., 311., 281., 182., 321.,\n", + " 58., 262., 206., 233., 242., 123., 167., 63., 197., 71., 168.,\n", + " 140., 217., 121., 235., 245., 40., 52., 104., 132., 88., 69.,\n", + " 219., 72., 201., 110., 51., 277., 63., 118., 69., 273., 258.,\n", + " 43., 198., 242., 232., 175., 93., 168., 275., 293., 281., 72.,\n", + " 140., 189., 181., 209., 136., 261., 113., 131., 174., 257., 55.,\n", + " 84., 42., 146., 212., 233., 91., 111., 152., 120., 67., 310.,\n", + " 94., 183., 66., 173., 72., 49., 64., 48., 178., 104., 132.,\n", + " 220., 57.]),\n", + " 'frame': None,\n", + " 'DESCR': '.. _diabetes_dataset:\\n\\nDiabetes dataset\\n----------------\\n\\nTen baseline variables, age, sex, body mass index, average blood\\npressure, and six blood serum measurements were obtained for each of n =\\n442 diabetes patients, as well as the response of interest, a\\nquantitative measure of disease progression one year after baseline.\\n\\n**Data Set Characteristics:**\\n\\n :Number of Instances: 442\\n\\n :Number of Attributes: First 10 columns are numeric predictive values\\n\\n :Target: Column 11 is a quantitative measure of disease progression one year after baseline\\n\\n :Attribute Information:\\n - age age in years\\n - sex\\n - bmi body mass index\\n - bp average blood pressure\\n - s1 tc, T-Cells (a type of white blood cells)\\n - s2 ldl, low-density lipoproteins\\n - s3 hdl, high-density lipoproteins\\n - s4 tch, thyroid stimulating hormone\\n - s5 ltg, lamotrigine\\n - s6 glu, blood sugar level\\n\\nNote: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\\n\\nSource URL:\\nhttps://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\\n\\nFor more information see:\\nBradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\\n(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)',\n", + " 'feature_names': ['age',\n", + " 'sex',\n", + " 'bmi',\n", + " 'bp',\n", + " 's1',\n", + " 's2',\n", + " 's3',\n", + " 's4',\n", + " 's5',\n", + " 's6'],\n", + " 'data_filename': '/Users/NH/opt/anaconda3/lib/python3.8/site-packages/sklearn/datasets/data/diabetes_data.csv.gz',\n", + " 'target_filename': '/Users/NH/opt/anaconda3/lib/python3.8/site-packages/sklearn/datasets/data/diabetes_target.csv.gz'}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes" ] }, { @@ -53,11 +146,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "diabetes.keys()" ] }, { @@ -73,13 +178,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "scrolled": false }, - "outputs": [], - "source": [ - "# your code here" + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + ".. _diabetes_dataset:\n", + "\n", + "Diabetes dataset\n", + "----------------\n", + "\n", + "Ten baseline variables, age, sex, body mass index, average blood\n", + "pressure, and six blood serum measurements were obtained for each of n =\n", + "442 diabetes patients, as well as the response of interest, a\n", + "quantitative measure of disease progression one year after baseline.\n", + "\n", + "**Data Set Characteristics:**\n", + "\n", + " :Number of Instances: 442\n", + "\n", + " :Number of Attributes: First 10 columns are numeric predictive values\n", + "\n", + " :Target: Column 11 is a quantitative measure of disease progression one year after baseline\n", + "\n", + " :Attribute Information:\n", + " - age age in years\n", + " - sex\n", + " - bmi body mass index\n", + " - bp average blood pressure\n", + " - s1 tc, T-Cells (a type of white blood cells)\n", + " - s2 ldl, low-density lipoproteins\n", + " - s3 hdl, high-density lipoproteins\n", + " - s4 tch, thyroid stimulating hormone\n", + " - s5 ltg, lamotrigine\n", + " - s6 glu, blood sugar level\n", + "\n", + "Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\n", + "\n", + "Source URL:\n", + "https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\n", + "\n", + "For more information see:\n", + "Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\n", + "(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)\n" + ] + } + ], + "source": [ + "# your code here\n", + "print(diabetes.DESCR)" ] }, { @@ -97,11 +248,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "# 1.\n", + "# 2.\n", + "# 3." ] }, { @@ -115,11 +269,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "shape data (442, 10)\n", + "shape target (442,)\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print('shape data', diabetes['data'].shape)\n", + "print('shape target', diabetes['target'].shape)" ] }, { @@ -156,11 +321,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "from sklearn.linear_model import LinearRegression" ] }, { @@ -172,11 +338,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "diabetes_model = LinearRegression()" ] }, { @@ -190,11 +357,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "diabetes_data_train = diabetes['data'][:-20]\n", + "diabetes_data_test = diabetes['data'][:-20]\n", + "diabetes_target_train = diabetes['target'][:-20]\n", + "diabetes_target_test = diabetes['target'][:-20]" ] }, { @@ -206,11 +377,26 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Intercept: 152.76430691633442\n", + "Coefficients: [ 3.03499549e-01 -2.37639315e+02 5.10530605e+02 3.27736980e+02\n", + " -8.14131709e+02 4.92814588e+02 1.02848452e+02 1.84606489e+02\n", + " 7.43519617e+02 7.60951722e+01]\n" + ] + } + ], + "source": [ + "# your code here\n", + "diabetes_model.fit(diabetes_data_train,diabetes_target_train)\n", + "\n", + "print(\"Intercept:\",diabetes_model.intercept_)\n", + "print(\"Coefficients:\",diabetes_model.coef_)" ] }, { @@ -231,11 +417,129 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([206.75928628, 68.44064739, 178.20406005, 166.59039906,\n", + " 129.47338697, 105.48206978, 74.90024045, 120.73067569,\n", + " 159.82011958, 211.92833532, 97.83664191, 97.41005509,\n", + " 115.60592349, 164.92279039, 103.34070745, 177.68217359,\n", + " 210.75217394, 184.40013153, 148.54098532, 123.55941211,\n", + " 120.97403176, 86.41241445, 113.06547293, 252.35802827,\n", + " 165.0256223 , 147.87909701, 97.36674337, 179.12350572,\n", + " 129.58494812, 185.38814558, 158.11930901, 69.5502975 ,\n", + " 263.36897831, 113.46175282, 79.56563326, 87.72481273,\n", + " 207.0421844 , 157.45915639, 240.65662115, 137.1490856 ,\n", + " 155.31655344, 74.42879297, 146.31653437, 78.52705268,\n", + " 222.11926084, 126.59820508, 141.84166437, 109.44753606,\n", + " 75.09372891, 190.42332738, 159.23790309, 171.06440722,\n", + " 134.14185441, 159.31230491, 138.94279895, 73.39607055,\n", + " 207.21538369, 80.45498482, 103.89405947, 135.51866843,\n", + " 113.7604739 , 181.57058967, 61.77249359, 98.88189256,\n", + " 115.70757199, 191.02738602, 150.90888198, 125.92583944,\n", + " 116.54669748, 124.03378216, 75.55191024, 237.48306485,\n", + " 141.50540223, 125.7874299 , 151.92660098, 128.96147947,\n", + " 192.53543155, 77.39022131, 167.09776732, 91.31020128,\n", + " 175.17028723, 124.29323949, 63.18411355, 151.78757246,\n", + " 53.60712124, 165.98566436, 44.4723183 , 152.23324167,\n", + " 81.62386207, 107.2816144 , 80.87409188, 187.27371075,\n", + " 192.57750449, 60.72408869, 106.4031363 , 125.49538842,\n", + " 208.99379988, 214.59988147, 123.54770557, 139.99635036,\n", + " 167.79824717, 108.58475774, 149.32986674, 159.19431647,\n", + " 152.351259 , 117.92793459, 73.04375498, 157.21237354,\n", + " 231.29179423, 146.69328359, 40.80100306, 121.73643613,\n", + " 152.55087233, 209.59441904, 289.90775253, 189.05011996,\n", + " 215.28486372, 237.32410411, 166.37285996, 151.907107 ,\n", + " 157.19405036, 201.84503346, 220.58435724, 176.89381347,\n", + " 169.39959973, 187.68448576, 57.68850542, 107.99405973,\n", + " 92.59936412, 212.23870609, 245.41651206, 69.4816247 ,\n", + " 114.41367356, 69.79914347, 140.31442169, 239.90556041,\n", + " 58.60883226, 235.72019943, 255.73947027, 253.12249817,\n", + " 157.6279999 , 231.98033091, 170.76553345, 117.61035691,\n", + " 179.93772788, 238.81068308, 190.57795278, 228.0108628 ,\n", + " 114.36333784, 178.23235423, 209.81775597, 144.9795296 ,\n", + " 202.60129801, 123.80860815, 151.63805944, 198.97398769,\n", + " 146.59152212, 125.27392182, 86.03270707, 234.82457586,\n", + " 82.84016948, 231.27600488, 144.36323971, 198.48406939,\n", + " 147.06334862, 77.98456984, 60.16531688, 263.50063179,\n", + " 226.2378663 , 219.1627738 , 48.11897346, 88.90700468,\n", + " 222.1094987 , 97.92075863, 165.3163388 , 121.62630945,\n", + " 159.11162765, 223.77647264, 100.21365939, 166.44520444,\n", + " 180.27822407, 90.61060421, 173.45311624, 160.50438597,\n", + " 201.91417101, 186.05703896, 196.96259576, 66.47814072,\n", + " 154.88396506, 117.37775276, 197.02504012, 128.99212268,\n", + " 92.25472599, 141.37351287, 155.92188295, 171.07777829,\n", + " 99.62668276, 192.35843191, 142.33601676, 176.87579872,\n", + " 97.12218811, 70.42117942, 163.92951642, 199.22311971,\n", + " 181.48660328, 227.29930221, 161.41002772, 211.79446617,\n", + " 224.66341593, 175.32963083, 124.96333003, 175.91591988,\n", + " 153.1156677 , 99.72373002, 100.00195178, 263.53536082,\n", + " 225.48647667, 222.38262709, 134.55939169, 145.07798763,\n", + " 54.84517001, 142.09205052, 154.79870054, 124.46470533,\n", + " 78.0745732 , 231.034711 , 78.67174011, 106.97791537,\n", + " 117.81672 , 99.53040319, 165.91262249, 160.30651046,\n", + " 159.59183372, 143.39983051, 231.71849 , 179.59535891,\n", + " 187.56243947, 67.05964986, 192.84191843, 179.08027657,\n", + " 235.94996771, 121.08637751, 85.93905304, 102.65017083,\n", + " 140.31649517, 101.66657695, 119.96430852, 82.16098489,\n", + " 234.31452819, 244.33866794, 263.53283451, 274.30429222,\n", + " 180.91762932, 204.45453023, 254.03323937, 119.28896357,\n", + " 267.19515892, 105.08321149, 119.46191516, 142.50982281,\n", + " 58.807832 , 130.39077413, 263.39520208, 46.17469178,\n", + " 125.0187723 , 131.70093752, 35.64540901, 139.17365621,\n", + " 246.10401878, 89.1724053 , 192.71804663, 166.23315911,\n", + " 147.79589786, 192.59895656, 177.56167389, 159.86178668,\n", + " 188.01701815, 117.08311908, 113.82928222, 118.61959402,\n", + " 166.10750875, 98.10269213, 141.14817148, 85.3149742 ,\n", + " 161.52470754, 203.30162645, 79.99128073, 146.91709814,\n", + " 81.14161392, 190.03643798, 221.95408679, 203.46909044,\n", + " 93.3565789 , 178.7878351 , 83.91466859, 152.06713318,\n", + " 78.34168866, 98.41490946, 108.56162588, 125.62703969,\n", + " 217.90029179, 127.53783653, 207.05767561, 230.19888487,\n", + " 124.47980723, 136.71526057, 127.63090393, 150.98569723,\n", + " 88.5925505 , 139.48869288, 204.52461607, 173.83508424,\n", + " 122.9253427 , 214.36441981, 174.70438665, 110.22556227,\n", + " 198.73936678, 174.83541512, 163.61697039, 193.79942594,\n", + " 191.47045414, 285.50533597, 279.46979662, 216.51365647,\n", + " 210.84831584, 215.64107793, 158.55625638, 224.23468435,\n", + " 188.77485924, 105.07471765, 180.78872664, 114.49716858,\n", + " 291.21345392, 184.36382653, 80.18176355, 86.91136289,\n", + " 248.78148064, 176.34545208, 122.15126229, 146.07273095,\n", + " 171.50388525, 184.66501163, 165.42948001, 157.82604851,\n", + " 143.70436507, 127.03589361, 177.62938237, 105.52013381,\n", + " 132.73458358, 97.5268931 , 250.1859579 , 86.81475817,\n", + " 62.41613529, 154.45925987, 191.6875602 , 134.74421673,\n", + " 94.28531437, 201.15935714, 53.73729167, 176.76832337,\n", + " 197.79656254, 119.63803358, 236.11561006, 166.17655703,\n", + " 162.37080447, 164.02067674, 252.92926842, 256.45853973,\n", + " 197.70698794, 184.82159384, 59.2095776 , 193.66927062,\n", + " 111.23508373, 142.60785153, 127.65299855, 181.28398173,\n", + " 210.83575857, 170.72039696, 165.17584707, 137.74480707,\n", + " 175.68382577, 75.3511821 , 245.44316502, 115.3294497 ,\n", + " 111.81300072, 141.55921954, 111.02445384, 91.9564145 ,\n", + " 164.19818587, 74.89356839, 253.68019464, 54.68468661,\n", + " 100.04367036, 100.83016925, 257.81296474, 169.57401414,\n", + " 62.70775773, 183.11027608, 170.34970265, 190.15105413,\n", + " 187.19845707, 88.17178554, 151.59620981, 250.30795981,\n", + " 199.91994535, 284.01273725, 50.53096422, 173.03012803,\n", + " 205.73902656, 174.78336203, 157.74242737, 150.48016954,\n", + " 234.26382757, 121.60133488, 165.79514728, 173.46162171,\n", + " 227.5018786 , 149.09669413, 99.5856398 , 81.87865626,\n", + " 142.75810005, 193.01616829])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "y_pred = diabetes_model.predict(diabetes_data_test)\n", + "y_pred" ] }, { @@ -247,11 +551,35 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n", + " 0.01990842, -0.01764613],\n", + " [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n", + " -0.06832974, -0.09220405],\n", + " [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n", + " 0.00286377, -0.02593034],\n", + " ...,\n", + " [-0.02004471, -0.04464164, -0.0547075 , ..., -0.03949338,\n", + " -0.07408887, -0.0052198 ],\n", + " [ 0.02354575, -0.04464164, -0.03638469, ..., 0.03430886,\n", + " -0.03324879, 0.06105391],\n", + " [ 0.03807591, 0.05068012, 0.0164281 , ..., 0.07120998,\n", + " 0.04976866, 0.01549073]])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "diabetes_data_test" ] }, { @@ -263,11 +591,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "# no, they are not" ] }, { @@ -302,7 +631,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -326,7 +655,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -351,11 +680,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "auto = pd.read_csv('../data/auto-mpg.csv')" ] }, { @@ -367,11 +697,124 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mpgcylindersdisplacementhorse_powerweightaccelerationmodel_yearcar_name
018.08307.0130.0350412.070\\t\"chevrolet chevelle malibu\"
115.08350.0165.0369311.570\\t\"buick skylark 320\"
218.08318.0150.0343611.070\\t\"plymouth satellite\"
316.08304.0150.0343312.070\\t\"amc rebel sst\"
417.08302.0140.0344910.570\\t\"ford torino\"
\n", + "
" + ], + "text/plain": [ + " mpg cylinders displacement horse_power weight acceleration \\\n", + "0 18.0 8 307.0 130.0 3504 12.0 \n", + "1 15.0 8 350.0 165.0 3693 11.5 \n", + "2 18.0 8 318.0 150.0 3436 11.0 \n", + "3 16.0 8 304.0 150.0 3433 12.0 \n", + "4 17.0 8 302.0 140.0 3449 10.5 \n", + "\n", + " model_year car_name \n", + "0 70 \\t\"chevrolet chevelle malibu\" \n", + "1 70 \\t\"buick skylark 320\" \n", + "2 70 \\t\"plymouth satellite\" \n", + "3 70 \\t\"amc rebel sst\" \n", + "4 70 \\t\"ford torino\" " + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "auto.head()" ] }, { @@ -383,11 +826,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 398 entries, 0 to 397\n", + "Data columns (total 8 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 mpg 398 non-null float64\n", + " 1 cylinders 398 non-null int64 \n", + " 2 displacement 398 non-null float64\n", + " 3 horse_power 392 non-null float64\n", + " 4 weight 398 non-null int64 \n", + " 5 acceleration 398 non-null float64\n", + " 6 model_year 398 non-null int64 \n", + " 7 car_name 398 non-null object \n", + "dtypes: float64(4), int64(3), object(1)\n", + "memory usage: 25.0+ KB\n" + ] + } + ], + "source": [ + "# your code here - all numeric columns have int or float\n", + "auto.info()" ] }, { @@ -399,11 +865,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "count 398.000000\n", + "mean 76.010050\n", + "std 3.697627\n", + "min 70.000000\n", + "25% 73.000000\n", + "50% 76.000000\n", + "75% 79.000000\n", + "max 82.000000\n", + "Name: model_year, dtype: float64" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "auto['model_year'].describe() # min 70 and max 82" ] }, { @@ -415,11 +901,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "auto = auto.dropna().reset_index(drop=True)" ] }, { @@ -431,18 +918,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4 199\n", + "8 103\n", + "6 83\n", + "3 4\n", + "5 3\n", + "Name: cylinders, dtype: int64" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "auto['cylinders'].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "We would like to generate a linear regression model that will predict mpg. To do this, first drop the `car_name` column since it does not contain any quantitative data. Next separate the dataframe to predictor and response variables. Separate those into test and training data with 80% of the data in the training set and the remainder in the test set. \n", + "We would like to generate a linear regression model that will predict mpg. To do this, first drop the `car_name` column since it does not contain any quantitative data. \n", + "\n", + "Next separate the dataframe to predictor and response variables. Separate those into test and training data with 80% of the data in the training set and the remainder in the test set. \n", "\n", "Assign the predictor and response training data to `X_train` and `y_train` respectively. Similarly, assign the predictor and response test data to `X_test` and `y_test`.\n", "\n", @@ -451,11 +957,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "auto.drop(['car_name'], axis=1, inplace=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "X = auto.drop('mpg', axis=1)\n", + "y = auto['mpg']\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, train_size= 0.8, random_state=1123)" ] }, { @@ -469,11 +997,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "auto_model = LinearRegression()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto_model.fit(X_train,y_train)" ] }, { @@ -503,11 +1052,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "from sklearn.metrics import r2_score" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "# your code here\n", + "y_pred = auto_model.predict(X_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8090803904769007" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r2_score(y_train,y_pred) # MUST BE: first real, second prediction" ] }, { @@ -516,18 +1095,43 @@ "source": [ "#### Our next step is to evaluate the model using the test data. \n", "\n", - "We would like to ensure that our model is not overfitting the data. This means that our model was made to fit too closely to the training data by being overly complex. If a model is overfitted, it is not generalizable to data outside the training data. In that case, we need to reduce the complexity of the model by removing certain features (variables).\n", + "We would like to ensure that our model is not overfitting the data. \n", + "\n", + "This means that our model was made to fit too closely to the training data by being overly complex. If a model is overfitted, it is not generalizable to data outside the training data. \n", + "\n", + "In that case, we need to reduce the complexity of the model by removing certain features (variables).\n", "\n", "In the cell below, use the model to generate the predicted values for the test data and assign them to `y_test_pred`. Compute the r squared score of the predicted `y_test_pred` and the oberserved `y_test` data." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "y_test_pred = auto_model.predict(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7984289217568107" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r2_score(y_test,y_test_pred)" ] }, { @@ -541,11 +1145,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ - "# your answer here" + "# your answer here\n", + "# almost no difference -> no averfitting" ] }, { @@ -554,34 +1159,54 @@ "source": [ "# Challenge 5 - Improve the Model Fit\n", "\n", - "While the most common way to improve the fit of a model is by using [regularization](https://datanice.github.io/machine-learning-101-what-is-regularization-interactive.html), there are other simpler ways to improve model fit. The first is to create a simpler model. The second is to increase the train sample size.\n", + "While the most common way to improve the fit of a model is by using [regularization](https://datanice.github.io/machine-learning-101-what-is-regularization-interactive.html), there are other simpler ways to improve model fit. \n", + "\n", + "The first is to create a simpler model. \n", + "The second is to increase the train sample size.\n", "\n", "Let us start with the easier option and increase our train sample size to 90% of the data. Create a new test train split and name the new predictors and response variables `X_train09`, `X_test09`, `y_train09`, `y_test09`." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "X_train09, X_test09, y_train09, y_test09 = train_test_split(X, y, train_size= 0.9, random_state=1123)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Initialize a new linear regression model. Name this model `auto_model09`. Fit the model to the new sample (training) data." + "Initialize a new linear regression model. Name this model `auto_model09`. \n", + "\n", + "Fit the model to the new sample (training) data." ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# your code here" + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# your code here\n", + "auto_model09 = LinearRegression()\n", + "\n", + "auto_model09.fit(X_train09,y_train09)" ] }, { @@ -593,11 +1218,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "y_pred09 = auto_model09.predict(X_train09)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8122008277362573" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r2_score(y_train09,y_pred09)" ] }, { @@ -609,11 +1255,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "y_test_pred09 = auto_model09.predict(X_test09)" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7821162403466526" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r2_score(y_test09,y_test_pred09)" ] }, { @@ -629,7 +1296,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ @@ -646,11 +1313,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ - "# your code here" + "# your code here\n", + "model2 = RFE(auto_model,n_features_to_select=3)" ] }, { @@ -662,18 +1330,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([3, 2, 4, 1, 1, 1])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code here" + "# your code here\n", + "model2.fit(X_train,y_train)\n", + "model2.ranking_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Feature importance is ranked from most important (1) to least important (4). Generate a model with the three most important features. The features correspond to variable names. For example, feature 1 is `cylinders` and feature 2 is `displacement`.\n", + "Feature importance is ranked from most important (1) to least important (4). \n", + "\n", + "Generate a model with the three most important features. \n", + "\n", + "The features correspond to variable names. For example, feature 1 is `cylinders` and feature 2 is `displacement`.\n", "\n", "Perform a test-train split on this reduced column data and call the split data `X_train_reduced`, `X_test_reduced`, `y_test_reduced`, `y_train_reduced`. Use an 80% split." ] @@ -737,7 +1422,49 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.8.5" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false } }, "nbformat": 4,