From 786a9bc070058132e39f3717623d811c4e2b566c Mon Sep 17 00:00:00 2001 From: Sofia Bonilla Date: Mon, 11 Oct 2021 16:28:33 -0400 Subject: [PATCH] lab_supervised_sklearn [Sofia Bonilla] --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 1393 +++++++++++++++++ your-code/main.ipynb | 833 +++++++++- 2 files changed, 2139 insertions(+), 87 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/main-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..2dd5525 --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,1393 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Before your start:\n", + "- Read the README.md file\n", + "- Comment as much as you can and use the resources in the README.md file\n", + "- Happy learning!" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Import your libraries:\n", + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "from sklearn.datasets import load_boston\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet\n", + "from sklearn.ensemble import RandomForestRegressor\n", + "from sklearn.preprocessing import StandardScaler\n", + "from sklearn import datasets" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 1 - Explore the Scikit-Learn Datasets\n", + "\n", + "Before starting to work on our own datasets, let's first explore the datasets that are included in this Python library. These datasets have been cleaned and formatted for use in ML algorithms." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First, we will load the diabetes dataset. Do this in the cell below by importing the datasets and then loading the dataset to the `diabetes` variable using the `load_diabetes()` function ([documentation](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html))." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "diabetes = datasets.load_diabetes()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's explore this variable by looking at the different attributes (keys) of `diabetes`. Note that the `load_diabetes` function does not return dataframes. It returns you a Python dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "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, total serum cholesterol\\n - s2 ldl, low-density lipoproteins\\n - s3 hdl, high-density lipoproteins\\n - s4 tch, total cholesterol / HDL\\n - s5 ltg, possibly log of serum triglycerides level\\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': 'diabetes_data.csv.gz',\n", + " 'target_filename': 'diabetes_target.csv.gz',\n", + " 'data_module': 'sklearn.datasets.data'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### The next step is to read the description of the dataset. \n", + "\n", + "Print the description in the cell below using the `DESCR` attribute of the `diabetes` variable. Read the data description carefully to fully understand what each column represents.\n", + "\n", + "*Hint: If your output is ill-formatted by displaying linebreaks as `\\n`, it means you are not using the `print` function.*" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": false + }, + "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, total serum cholesterol\n", + " - s2 ldl, low-density lipoproteins\n", + " - s3 hdl, high-density lipoproteins\n", + " - s4 tch, total cholesterol / HDL\n", + " - s5 ltg, possibly log of serum triglycerides level\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)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Based on the data description, answer the following questions:\n", + "\n", + "1. How many attributes are there in the data? What do they mean?\n", + "\n", + "1. What is the relation between `diabetes['data']` and `diabetes['target']`?\n", + "\n", + "1. How many records are there in the data?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Enter your answer here:\n", + "# HAY 10 ATRIBUTOS EN EN LA DATA\n", + "# ES LA PROGRESION DE LA ENFERMEDAD EN UN ANO, CARACTERISTICAS DE LA ENFERMEDAD\n", + "#442" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Now explore what are contained in the *data* portion as well as the *target* portion of `diabetes`. \n", + "\n", + "Scikit-learn typically takes in 2D numpy arrays as input (though pandas dataframes are also accepted). Inspect the shape of `data` and `target`. Confirm they are consistent with the data description." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0.03807591 0.05068012 0.06169621 ... -0.00259226 0.01990842\n", + " -0.01764613]\n", + " [-0.00188202 -0.04464164 -0.05147406 ... -0.03949338 -0.06832974\n", + " -0.09220405]\n", + " [ 0.08529891 0.05068012 0.04445121 ... -0.00259226 0.00286377\n", + " -0.02593034]\n", + " ...\n", + " [ 0.04170844 0.05068012 -0.01590626 ... -0.01107952 -0.04687948\n", + " 0.01549073]\n", + " [-0.04547248 -0.04464164 0.03906215 ... 0.02655962 0.04452837\n", + " -0.02593034]\n", + " [-0.04547248 -0.04464164 -0.0730303 ... -0.03949338 -0.00421986\n", + " 0.00306441]]\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(diabetes.data)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[151. 75. 141. 206. 135. 97. 138. 63. 110. 310. 101. 69. 179. 185.\n", + " 118. 171. 166. 144. 97. 168. 68. 49. 68. 245. 184. 202. 137. 85.\n", + " 131. 283. 129. 59. 341. 87. 65. 102. 265. 276. 252. 90. 100. 55.\n", + " 61. 92. 259. 53. 190. 142. 75. 142. 155. 225. 59. 104. 182. 128.\n", + " 52. 37. 170. 170. 61. 144. 52. 128. 71. 163. 150. 97. 160. 178.\n", + " 48. 270. 202. 111. 85. 42. 170. 200. 252. 113. 143. 51. 52. 210.\n", + " 65. 141. 55. 134. 42. 111. 98. 164. 48. 96. 90. 162. 150. 279.\n", + " 92. 83. 128. 102. 302. 198. 95. 53. 134. 144. 232. 81. 104. 59.\n", + " 246. 297. 258. 229. 275. 281. 179. 200. 200. 173. 180. 84. 121. 161.\n", + " 99. 109. 115. 268. 274. 158. 107. 83. 103. 272. 85. 280. 336. 281.\n", + " 118. 317. 235. 60. 174. 259. 178. 128. 96. 126. 288. 88. 292. 71.\n", + " 197. 186. 25. 84. 96. 195. 53. 217. 172. 131. 214. 59. 70. 220.\n", + " 268. 152. 47. 74. 295. 101. 151. 127. 237. 225. 81. 151. 107. 64.\n", + " 138. 185. 265. 101. 137. 143. 141. 79. 292. 178. 91. 116. 86. 122.\n", + " 72. 129. 142. 90. 158. 39. 196. 222. 277. 99. 196. 202. 155. 77.\n", + " 191. 70. 73. 49. 65. 263. 248. 296. 214. 185. 78. 93. 252. 150.\n", + " 77. 208. 77. 108. 160. 53. 220. 154. 259. 90. 246. 124. 67. 72.\n", + " 257. 262. 275. 177. 71. 47. 187. 125. 78. 51. 258. 215. 303. 243.\n", + " 91. 150. 310. 153. 346. 63. 89. 50. 39. 103. 308. 116. 145. 74.\n", + " 45. 115. 264. 87. 202. 127. 182. 241. 66. 94. 283. 64. 102. 200.\n", + " 265. 94. 230. 181. 156. 233. 60. 219. 80. 68. 332. 248. 84. 200.\n", + " 55. 85. 89. 31. 129. 83. 275. 65. 198. 236. 253. 124. 44. 172.\n", + " 114. 142. 109. 180. 144. 163. 147. 97. 220. 190. 109. 191. 122. 230.\n", + " 242. 248. 249. 192. 131. 237. 78. 135. 244. 199. 270. 164. 72. 96.\n", + " 306. 91. 214. 95. 216. 263. 178. 113. 200. 139. 139. 88. 148. 88.\n", + " 243. 71. 77. 109. 272. 60. 54. 221. 90. 311. 281. 182. 321. 58.\n", + " 262. 206. 233. 242. 123. 167. 63. 197. 71. 168. 140. 217. 121. 235.\n", + " 245. 40. 52. 104. 132. 88. 69. 219. 72. 201. 110. 51. 277. 63.\n", + " 118. 69. 273. 258. 43. 198. 242. 232. 175. 93. 168. 275. 293. 281.\n", + " 72. 140. 189. 181. 209. 136. 261. 113. 131. 174. 257. 55. 84. 42.\n", + " 146. 212. 233. 91. 111. 152. 120. 67. 310. 94. 183. 66. 173. 72.\n", + " 49. 64. 48. 178. 104. 132. 220. 57.]\n" + ] + } + ], + "source": [ + "print(diabetes.target)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(442, 10)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes.data.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(442,)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes.target.shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# tienen la misma cantidad de filas y columnas" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 2 - Perform Supervised Learning on the Dataset" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The data have already been split to predictor (*data*) and response (*target*) variables. Given this information, we'll apply what we have previously learned about linear regression and apply the algorithm to the diabetes dataset.\n", + "\n", + "#### Let's briefly revisit the linear regression formula:\n", + "\n", + "```\n", + "y = β0 + β1X1 + β2X2 + ... + βnXn + ϵ\n", + "```\n", + "\n", + "...where:\n", + "\n", + "- X1-Xn: data \n", + "- β0: intercept \n", + "- β1-βn: coefficients \n", + "- ϵ: error (cannot explained by model)\n", + "- y: target\n", + "\n", + "Also take a look at the `sklearn.linear_model.LinearRegression` [documentation](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html).\n", + "\n", + "#### In the cell below, import the `linear_model` class from `sklearn`. " + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn import linear_model\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "diabetes_model=linear_model.LinearRegression()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Next, let's split the training and test data.\n", + "\n", + "Define `diabetes_data_train`, `diabetes_target_train`, `diabetes_data_test`, and `diabetes_target_test`. Use the last 20 records for the test data and the rest for the training data." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "X_train,X_test,y_train,y_test=train_test_split(diabetes['data'],diabetes['target'],test_size=0.2,\n", + " random_state=42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fit the training data and target to `diabetes_model`. Print the *intercept* and *coefficients* of the model." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "151.3456553477407" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array([ 37.90031426, -241.96624835, 542.42575342, 347.70830529,\n", + " -931.46126093, 518.04405547, 163.40353476, 275.31003837,\n", + " 736.18909839, 48.67112488])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes_model.fit(X_train,y_train)\n", + "display(diabetes_model.intercept_)\n", + "diabetes_model.coef_" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Inspecting the results\n", + "\n", + "From the outputs you should have seen:\n", + "\n", + "- The intercept is a float number.\n", + "- The coefficients are an array containing 10 float numbers.\n", + "\n", + "This is the linear regression model fitted to your training dataset.\n", + "\n", + "#### Using your fitted linear regression model, predict the *y* of `diabetes_data_test`." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([139.5483133 , 179.52030578, 134.04133298, 291.41193598,\n", + " 123.78723656, 92.17357677, 258.23409704, 181.33895238,\n", + " 90.22217862, 108.63143298, 94.13938654, 168.43379636,\n", + " 53.50669663, 206.63040068, 100.13238561, 130.66881649,\n", + " 219.53270758, 250.78291772, 196.36682356, 218.57497401,\n", + " 207.35002447, 88.48361667, 70.43428801, 188.95725301,\n", + " 154.88720039, 159.35957695, 188.31587948, 180.38835506,\n", + " 47.98988446, 108.97514644, 174.78080029, 86.36598906,\n", + " 132.95890535, 184.5410226 , 173.83298051, 190.35863287,\n", + " 124.41740796, 119.65426903, 147.95402494, 59.05311211,\n", + " 71.62636914, 107.68722902, 165.45544477, 155.00784964,\n", + " 171.04558668, 61.45763075, 71.66975626, 114.96330486,\n", + " 51.57808027, 167.57781958, 152.52505798, 62.95827693,\n", + " 103.49862017, 109.20495627, 175.63844013, 154.60247734,\n", + " 94.41476124, 210.74244148, 120.25601864, 77.61590087,\n", + " 187.93503183, 206.49543321, 140.63018684, 105.59463059,\n", + " 130.704246 , 202.18650868, 171.1330116 , 164.91246096,\n", + " 124.72637597, 144.81210187, 181.99631481, 199.41234515,\n", + " 234.21402489, 145.96053305, 79.86349114, 157.36828831,\n", + " 192.74737754, 208.8980067 , 158.58505486, 206.0226849 ,\n", + " 107.47978402, 140.93428553, 54.81856678, 55.92807758,\n", + " 115.00974554, 78.95886675, 81.55731377, 54.3774778 ,\n", + " 166.25477778])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred=diabetes_model.predict(X_test)\n", + "y_pred" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Print your `diabetes_target_test` and compare with the prediction. " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([219., 70., 202., 230., 111., 84., 242., 272., 94., 96., 94.,\n", + " 252., 99., 297., 135., 67., 295., 264., 170., 275., 310., 64.,\n", + " 128., 232., 129., 118., 263., 77., 48., 107., 140., 113., 90.,\n", + " 164., 180., 233., 42., 84., 172., 63., 48., 108., 156., 168.,\n", + " 90., 52., 200., 87., 90., 258., 136., 158., 69., 72., 171.,\n", + " 95., 72., 151., 168., 60., 122., 52., 187., 102., 214., 248.,\n", + " 181., 110., 140., 202., 101., 222., 281., 61., 89., 91., 186.,\n", + " 220., 237., 233., 68., 190., 96., 72., 153., 98., 37., 63.,\n", + " 184.])" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.45260660216173787" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "display(y_test)\n", + "from sklearn.metrics import r2_score\n", + "r2_score(y_test,y_pred)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Is `diabetes_target_test` exactly the same as the model prediction? Explain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your explanation here:\n", + "# no predice muy bien la variable" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge 1 - Hypothesis Testing with `statsmodels`\n", + "\n", + "After generating the linear regression model from the dataset, you probably wonder: then what? What is the statistical way to know if my model is reliable or not?\n", + "\n", + "Good question. We'll discuss that using Scikit-Learn in Challenge 5. But for now, let's use a fool-proof way by using the ([Linear Regression class of StatsModels](https://www.statsmodels.org/dev/regression.html)) which can also conduct linear regression analysis plus much more such as calcuating the F-score of the linear model as well as the standard errors and t-scores for each coefficient. The F-score and t-scores will tell you whether you can trust your linear model.\n", + "\n", + "To understand the statistical meaning of conducting hypothesis testing (e.g. F-test, t-test) for slopes, read [this webpage](https://onlinecourses.science.psu.edu/stat501/node/297/) at your leisure time. We'll give you a brief overview next.\n", + "\n", + "* The F-test of your linear model is to verify whether at least one of your coefficients is significantly different from zero. Translating that into the *null hypothesis* and *alternative hypothesis*, that is:\n", + "\n", + " ```\n", + " H0 : β1 = β2 = ... = β10 = 0\n", + " HA : At least one βj ≠ 0 (for j = 1, 2, ..., 10)\n", + " ```\n", + "\n", + "* The t-tests on each coefficient is to check whether the confidence interval for the variable contains zero. If the confidence interval contains zero, it means the null hypothesis for that variable is not rejected. In other words, this particular vaiable is not contributing to your linear model and you can remove it from your formula.\n", + "\n", + "Read the documentations of [StatsModels Linear Regression](https://www.statsmodels.org/dev/regression.html) as well as its [`OLS` class](https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.html) which stands for *ordinary least squares*.\n", + "\n", + "#### In the next cell, analyze `diabetes_data_train` and `diabetes_target_train` with the linear regression model of `statsmodels`. Print the fit summary.\n", + "\n", + "Your output should look like:\n", + "\n", + "![statsmodels regression](../statsmodels.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Interpreting hypothesis testing results\n", + "\n", + "Answer the following questions in the cell below:\n", + "\n", + "1. What is the F-score of your linear model and is the null hypothesis rejected?\n", + "\n", + "1. Does any of the t-tests of the coefficients produce a confidence interval containing zero? What are they?\n", + "\n", + "1. How will you modify your linear reguression model according to the test results above?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your answers here:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 3 - Peform Supervised Learning on a Pandas Dataframe" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we have dealt with data that has been formatted for scikit-learn, let's look at data that we will need to format ourselves.\n", + "\n", + "In the next cell, load the `auto-mpg.csv` file included in this folder and assign it to a variable called `auto`." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "auto=pd.read_csv('../auto-mpg.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Look at the first 5 rows using the `head()` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "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": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.head(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Evaluate the data to ensure that all numeric columns are correctly detected as such by pandas. If a column is misclassified as object, coerce it to numeric." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "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:\n", + "auto.info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What is the newest model year and the oldest model year?" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "70" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "\n", + "auto.model_year.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "82" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.model_year.max()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Check the dataset for missing values and remove all rows containing at least one missing value." + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mpg 0\n", + "cylinders 0\n", + "displacement 0\n", + "horse_power 6\n", + "weight 0\n", + "acceleration 0\n", + "model_year 0\n", + "car_name 0\n", + "dtype: int64" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.isna().sum()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "auto=auto.dropna()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Find the frequency table for the `cylinders` column using the `value_counts()` function. How many possible values of cylinders are there?" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "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": 46, + "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", + "\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", + "*Hint: To separate data for training and test, use the `train_test_split` method we used in previous labs.*" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "X_train,X_test,y_train,y_test=train_test_split(auto.drop(['car_name','mpg'],axis=1),auto['mpg'],test_size=0.2,\n", + " random_state=42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we will processed and peform linear regression on this data to predict the mpg for each vehicle. \n", + "\n", + "#### In the next cell, create an instance of the linear regression model and call it `auto_model`. Fit `auto_model` with your training data." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto_model=linear_model.LinearRegression()\n", + "auto_model.fit(X_train,y_train)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 4 - Evaluate the Model\n", + "\n", + "In addition to evaluating your model with F-test and t-test, you can also use the *Coefficient of Determination* (a.k.a. *r squared score*). This method does not simply tell *yes* or *no* about the model fit but instead indicates how much variation can be explained by the model. Based on the r squared score, you can decide whether to improve your model in order to obtain a better fit.\n", + "\n", + "You can learn about the r squared score [here](). Its formula is:\n", + "\n", + "![R Squared](../r-squared.png)\n", + "\n", + "...where:\n", + "\n", + "* yi is an actual data point.\n", + "* ŷi is the corresponding data point on the estimated regression line.\n", + "\n", + "By adding the squares of the difference between all yi-ŷi pairs, we have a measure called SSE (*error sum of squares*) which is an application of the r squared score to indicate the extent to which the estimated regression model is different from the actual data. And we attribute that difference to the random error that is unavoidable in the real world. Obviously, we want the SSE value to be as small as possible.\n", + "\n", + "#### In the next cell, compute the predicted *y* based on `X_train` and call it `y_pred`. Then calcualte the r squared score between `y_pred` and `y_train` which indicates how well the estimated regression model fits the training data.\n", + "\n", + "*Hint: r squared score can be calculated using `sklearn.metrics.r2_score` ([documentation](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html)).*" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8107227953093896" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred=auto_model.predict(X_train)\n", + "r2_score(y_train,y_pred)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + "\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": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.794234907542859" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_test_pred=auto_model.predict(X_test)\n", + "r2_score(y_test,y_test_pred)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Explaining the results\n", + "\n", + "The r squared scores of the training data and the test data are pretty close (0.8146 vs 0.7818). This means our model is not overfitted. However, there is still room to improve the model fit. Move on to the next challenge." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "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", + "\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": 51, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "X_train09,X_test09,y_train09,y_test09=train_test_split(auto.drop(['car_name','mpg'],axis=1),\n", + " auto['mpg'],test_size=0.1,random_state=42)" + ] + }, + { + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto_model09=linear_model.LinearRegression()\n", + "auto_model09.fit(X_train09,y_train09)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute the predicted values and r squared score for our new model and new sample data." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8468911998183242" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred09=auto_model09.predict(X_test09)\n", + "r2_score(y_test09,y_pred09)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute the r squared score for the smaller test set. Is there an improvement in the test r squared?" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8047940166959004" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_test_pred09=auto_model09.predict(X_train09)\n", + "r2_score(y_train09,y_test_pred09)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge 2 - Backward Elimination \n", + "\n", + "The main way to produce a simpler linear regression model is to reduce the number of variables used in the model. In scikit-learn, we can do this by using recursive feature elimination. You can read more about RFE [here](https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.RFE.html).\n", + "\n", + "In the next cell, we will import RFE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.feature_selection import RFE" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Follow the documentation and initialize an RFE model using the `auto_model` linear regression model. Set `n_features_to_select=3`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Fit the model and print the ranking" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "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", + "\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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Generate a new model called `auto_model_reduced` and fit this model. Then proceed to compute the r squared score for the model. Did this cause an improvement in the r squared score?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here: \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Conclusion\n", + "\n", + "You may obtain the impression from this lab that without knowing statistical methods in depth, it is difficult to make major progress in machine learning. That is correct. If you are motivated to become a data scientist, statistics is the subject you must be proficient in and there is no shortcut. \n", + "\n", + "Completing these labs is not likely to make you a data scientist. But you will have a good sense about what are there in machine learning and what are good for you. In your future career, you can choose one of the three tracks:\n", + "\n", + "* Data scientists who need to be proficient in statistical methods.\n", + "\n", + "* Data engineers who need to be good at programming.\n", + "\n", + "* Data integration specialists who are business or content experts but also understand data and programming. This cross-disciplinary track brings together data, technology, and business and will be in high demands in the next decade." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0102ef9..2dd5525 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "# Import your libraries:\n" + "# Import your libraries:\n", + "import numpy as np\n", + "import pandas as pd\n", + "\n", + "from sklearn.datasets import load_boston\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.linear_model import LinearRegression, Ridge, Lasso, ElasticNet\n", + "from sklearn.ensemble import RandomForestRegressor\n", + "from sklearn.preprocessing import StandardScaler\n", + "from sklearn import datasets" ] }, { @@ -37,11 +46,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "diabetes = datasets.load_diabetes()\n" ] }, { @@ -53,11 +64,91 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 7, + "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, total serum cholesterol\\n - s2 ldl, low-density lipoproteins\\n - s3 hdl, high-density lipoproteins\\n - s4 tch, total cholesterol / HDL\\n - s5 ltg, possibly log of serum triglycerides level\\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': 'diabetes_data.csv.gz',\n", + " 'target_filename': 'diabetes_target.csv.gz',\n", + " 'data_module': 'sklearn.datasets.data'}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes" ] }, { @@ -73,13 +164,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "scrolled": false }, - "outputs": [], - "source": [ - "# Your code here:\n" + "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, total serum cholesterol\n", + " - s2 ldl, low-density lipoproteins\n", + " - s3 hdl, high-density lipoproteins\n", + " - s4 tch, total cholesterol / HDL\n", + " - s5 ltg, possibly log of serum triglycerides level\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)" ] }, { @@ -101,7 +238,10 @@ "metadata": {}, "outputs": [], "source": [ - "# Enter your answer here:\n" + "# Enter your answer here:\n", + "# HAY 10 ATRIBUTOS EN EN LA DATA\n", + "# ES LA PROGRESION DE LA ENFERMEDAD EN UN ANO, CARACTERISTICAS DE LA ENFERMEDAD\n", + "#442" ] }, { @@ -113,13 +253,131 @@ "Scikit-learn typically takes in 2D numpy arrays as input (though pandas dataframes are also accepted). Inspect the shape of `data` and `target`. Confirm they are consistent with the data description." ] }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0.03807591 0.05068012 0.06169621 ... -0.00259226 0.01990842\n", + " -0.01764613]\n", + " [-0.00188202 -0.04464164 -0.05147406 ... -0.03949338 -0.06832974\n", + " -0.09220405]\n", + " [ 0.08529891 0.05068012 0.04445121 ... -0.00259226 0.00286377\n", + " -0.02593034]\n", + " ...\n", + " [ 0.04170844 0.05068012 -0.01590626 ... -0.01107952 -0.04687948\n", + " 0.01549073]\n", + " [-0.04547248 -0.04464164 0.03906215 ... 0.02655962 0.04452837\n", + " -0.02593034]\n", + " [-0.04547248 -0.04464164 -0.0730303 ... -0.03949338 -0.00421986\n", + " 0.00306441]]\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(diabetes.data)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[151. 75. 141. 206. 135. 97. 138. 63. 110. 310. 101. 69. 179. 185.\n", + " 118. 171. 166. 144. 97. 168. 68. 49. 68. 245. 184. 202. 137. 85.\n", + " 131. 283. 129. 59. 341. 87. 65. 102. 265. 276. 252. 90. 100. 55.\n", + " 61. 92. 259. 53. 190. 142. 75. 142. 155. 225. 59. 104. 182. 128.\n", + " 52. 37. 170. 170. 61. 144. 52. 128. 71. 163. 150. 97. 160. 178.\n", + " 48. 270. 202. 111. 85. 42. 170. 200. 252. 113. 143. 51. 52. 210.\n", + " 65. 141. 55. 134. 42. 111. 98. 164. 48. 96. 90. 162. 150. 279.\n", + " 92. 83. 128. 102. 302. 198. 95. 53. 134. 144. 232. 81. 104. 59.\n", + " 246. 297. 258. 229. 275. 281. 179. 200. 200. 173. 180. 84. 121. 161.\n", + " 99. 109. 115. 268. 274. 158. 107. 83. 103. 272. 85. 280. 336. 281.\n", + " 118. 317. 235. 60. 174. 259. 178. 128. 96. 126. 288. 88. 292. 71.\n", + " 197. 186. 25. 84. 96. 195. 53. 217. 172. 131. 214. 59. 70. 220.\n", + " 268. 152. 47. 74. 295. 101. 151. 127. 237. 225. 81. 151. 107. 64.\n", + " 138. 185. 265. 101. 137. 143. 141. 79. 292. 178. 91. 116. 86. 122.\n", + " 72. 129. 142. 90. 158. 39. 196. 222. 277. 99. 196. 202. 155. 77.\n", + " 191. 70. 73. 49. 65. 263. 248. 296. 214. 185. 78. 93. 252. 150.\n", + " 77. 208. 77. 108. 160. 53. 220. 154. 259. 90. 246. 124. 67. 72.\n", + " 257. 262. 275. 177. 71. 47. 187. 125. 78. 51. 258. 215. 303. 243.\n", + " 91. 150. 310. 153. 346. 63. 89. 50. 39. 103. 308. 116. 145. 74.\n", + " 45. 115. 264. 87. 202. 127. 182. 241. 66. 94. 283. 64. 102. 200.\n", + " 265. 94. 230. 181. 156. 233. 60. 219. 80. 68. 332. 248. 84. 200.\n", + " 55. 85. 89. 31. 129. 83. 275. 65. 198. 236. 253. 124. 44. 172.\n", + " 114. 142. 109. 180. 144. 163. 147. 97. 220. 190. 109. 191. 122. 230.\n", + " 242. 248. 249. 192. 131. 237. 78. 135. 244. 199. 270. 164. 72. 96.\n", + " 306. 91. 214. 95. 216. 263. 178. 113. 200. 139. 139. 88. 148. 88.\n", + " 243. 71. 77. 109. 272. 60. 54. 221. 90. 311. 281. 182. 321. 58.\n", + " 262. 206. 233. 242. 123. 167. 63. 197. 71. 168. 140. 217. 121. 235.\n", + " 245. 40. 52. 104. 132. 88. 69. 219. 72. 201. 110. 51. 277. 63.\n", + " 118. 69. 273. 258. 43. 198. 242. 232. 175. 93. 168. 275. 293. 281.\n", + " 72. 140. 189. 181. 209. 136. 261. 113. 131. 174. 257. 55. 84. 42.\n", + " 146. 212. 233. 91. 111. 152. 120. 67. 310. 94. 183. 66. 173. 72.\n", + " 49. 64. 48. 178. 104. 132. 220. 57.]\n" + ] + } + ], + "source": [ + "print(diabetes.target)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(442, 10)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes.data.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(442,)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes.target.shape" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# tienen la misma cantidad de filas y columnas" ] }, { @@ -129,6 +387,13 @@ "# Challenge 2 - Perform Supervised Learning on the Dataset" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -156,27 +421,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn import linear_model\n" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": 32, "metadata": {}, + "outputs": [], "source": [ - "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." + "# Your code here:\n", + "\n", + "diabetes_model=linear_model.LinearRegression()" ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# Your code here:\n" + "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." ] }, { @@ -190,11 +457,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "X_train,X_test,y_train,y_test=train_test_split(diabetes['data'],diabetes['target'],test_size=0.2,\n", + " random_state=42)" ] }, { @@ -206,11 +475,36 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "151.3456553477407" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "array([ 37.90031426, -241.96624835, 542.42575342, 347.70830529,\n", + " -931.46126093, 518.04405547, 163.40353476, 275.31003837,\n", + " 736.18909839, 48.67112488])" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes_model.fit(X_train,y_train)\n", + "display(diabetes_model.intercept_)\n", + "diabetes_model.coef_" ] }, { @@ -231,11 +525,46 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([139.5483133 , 179.52030578, 134.04133298, 291.41193598,\n", + " 123.78723656, 92.17357677, 258.23409704, 181.33895238,\n", + " 90.22217862, 108.63143298, 94.13938654, 168.43379636,\n", + " 53.50669663, 206.63040068, 100.13238561, 130.66881649,\n", + " 219.53270758, 250.78291772, 196.36682356, 218.57497401,\n", + " 207.35002447, 88.48361667, 70.43428801, 188.95725301,\n", + " 154.88720039, 159.35957695, 188.31587948, 180.38835506,\n", + " 47.98988446, 108.97514644, 174.78080029, 86.36598906,\n", + " 132.95890535, 184.5410226 , 173.83298051, 190.35863287,\n", + " 124.41740796, 119.65426903, 147.95402494, 59.05311211,\n", + " 71.62636914, 107.68722902, 165.45544477, 155.00784964,\n", + " 171.04558668, 61.45763075, 71.66975626, 114.96330486,\n", + " 51.57808027, 167.57781958, 152.52505798, 62.95827693,\n", + " 103.49862017, 109.20495627, 175.63844013, 154.60247734,\n", + " 94.41476124, 210.74244148, 120.25601864, 77.61590087,\n", + " 187.93503183, 206.49543321, 140.63018684, 105.59463059,\n", + " 130.704246 , 202.18650868, 171.1330116 , 164.91246096,\n", + " 124.72637597, 144.81210187, 181.99631481, 199.41234515,\n", + " 234.21402489, 145.96053305, 79.86349114, 157.36828831,\n", + " 192.74737754, 208.8980067 , 158.58505486, 206.0226849 ,\n", + " 107.47978402, 140.93428553, 54.81856678, 55.92807758,\n", + " 115.00974554, 78.95886675, 81.55731377, 54.3774778 ,\n", + " 166.25477778])" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred=diabetes_model.predict(X_test)\n", + "y_pred" ] }, { @@ -247,11 +576,42 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([219., 70., 202., 230., 111., 84., 242., 272., 94., 96., 94.,\n", + " 252., 99., 297., 135., 67., 295., 264., 170., 275., 310., 64.,\n", + " 128., 232., 129., 118., 263., 77., 48., 107., 140., 113., 90.,\n", + " 164., 180., 233., 42., 84., 172., 63., 48., 108., 156., 168.,\n", + " 90., 52., 200., 87., 90., 258., 136., 158., 69., 72., 171.,\n", + " 95., 72., 151., 168., 60., 122., 52., 187., 102., 214., 248.,\n", + " 181., 110., 140., 202., 101., 222., 281., 61., 89., 91., 186.,\n", + " 220., 237., 233., 68., 190., 96., 72., 153., 98., 37., 63.,\n", + " 184.])" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "0.45260660216173787" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "display(y_test)\n", + "from sklearn.metrics import r2_score\n", + "r2_score(y_test,y_pred)" ] }, { @@ -267,7 +627,8 @@ "metadata": {}, "outputs": [], "source": [ - "# Your explanation here:\n" + "# Your explanation here:\n", + "# no predice muy bien la variable" ] }, { @@ -351,11 +712,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "auto=pd.read_csv('../auto-mpg.csv')" ] }, { @@ -367,11 +729,124 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 37, + "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": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.head(5)" ] }, { @@ -383,11 +858,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 38, + "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:\n", + "auto.info()" ] }, { @@ -399,11 +897,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "70" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "\n", + "auto.model_year.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 43, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "82" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "auto.model_year.max()" ] }, { @@ -415,11 +946,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mpg 0\n", + "cylinders 0\n", + "displacement 0\n", + "horse_power 6\n", + "weight 0\n", + "acceleration 0\n", + "model_year 0\n", + "car_name 0\n", + "dtype: int64" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.isna().sum()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto=auto.dropna()" ] }, { @@ -431,11 +991,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 46, + "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": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.cylinders.value_counts()" ] }, { @@ -451,11 +1028,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "X_train,X_test,y_train,y_test=train_test_split(auto.drop(['car_name','mpg'],axis=1),auto['mpg'],test_size=0.2,\n", + " random_state=42)" ] }, { @@ -469,11 +1048,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "auto_model=linear_model.LinearRegression()\n", + "auto_model.fit(X_train,y_train)" ] }, { @@ -502,11 +1094,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.8107227953093896" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_pred=auto_model.predict(X_train)\n", + "r2_score(y_train,y_pred)" ] }, { @@ -522,11 +1127,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.794234907542859" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_test_pred=auto_model.predict(X_test)\n", + "r2_score(y_test,y_test_pred)" ] }, { @@ -551,11 +1169,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "X_train09,X_test09,y_train09,y_test09=train_test_split(auto.drop(['car_name','mpg'],axis=1),\n", + " auto['mpg'],test_size=0.1,random_state=42)" ] }, { @@ -567,11 +1187,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "auto_model09=linear_model.LinearRegression()\n", + "auto_model09.fit(X_train09,y_train09)" ] }, { @@ -583,11 +1216,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.8468911998183242" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_pred09=auto_model09.predict(X_test09)\n", + "r2_score(y_test09,y_pred09)" ] }, { @@ -599,11 +1245,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.8047940166959004" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_test_pred09=auto_model09.predict(X_train09)\n", + "r2_score(y_train09,y_test_pred09)" ] }, { @@ -712,7 +1371,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -726,7 +1385,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.9.6" } }, "nbformat": 4,