From deff1cf8cabbfddd38151f3a052fd0f77853bed5 Mon Sep 17 00:00:00 2001 From: Mariana Sousa Date: Mon, 28 Aug 2023 10:21:08 +0100 Subject: [PATCH] marianaslab --- auto-mpg.csv => your-code/auto-mpg.csv | 0 your-code/main.ipynb | 1053 +++++++++++++++++++++--- 2 files changed, 959 insertions(+), 94 deletions(-) rename auto-mpg.csv => your-code/auto-mpg.csv (100%) diff --git a/auto-mpg.csv b/your-code/auto-mpg.csv similarity index 100% rename from auto-mpg.csv rename to your-code/auto-mpg.csv diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 8a9fa9e..b67e989 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ - "# Import your libraries:\n" + "import pandas as pd\n", + "import numpy as np" ] }, { @@ -37,11 +38,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn.datasets import load_diabetes\n", + "\n", + "\n", + "diabetes = load_diabetes()" ] }, { @@ -53,11 +57,90 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'data': array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n", + " 0.01990749, -0.01764613],\n", + " [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n", + " -0.06833155, -0.09220405],\n", + " [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n", + " 0.00286131, -0.02593034],\n", + " ...,\n", + " [ 0.04170844, 0.05068012, -0.01590626, ..., -0.01107952,\n", + " -0.04688253, 0.01549073],\n", + " [-0.04547248, -0.04464164, 0.03906215, ..., 0.02655962,\n", + " 0.04452873, -0.02593034],\n", + " [-0.04547248, -0.04464164, -0.0730303 , ..., -0.03949338,\n", + " -0.00422151, 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 the square root of `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',\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_raw.csv.gz',\n", + " 'target_filename': 'diabetes_target.csv.gz',\n", + " 'data_module': 'sklearn.datasets.data'}" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes" ] }, { @@ -73,13 +156,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "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 the square root of `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", + "\n" + ] + } + ], + "source": [ + "print(diabetes[\"DESCR\"])\n" ] }, { @@ -101,7 +230,25 @@ "metadata": {}, "outputs": [], "source": [ - "# Enter your answer here:\n" + "\"\"\"\n", + "1) - 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", + "2) data are thge features, target is a quantitative measure of disease \n", + "progression one year after baseline\n", + "\n", + "3) 442\n", + "\n", + "\n", + "\"\"\"\n" ] }, { @@ -115,11 +262,253 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0 151.0\n", + "1 75.0\n", + "2 141.0\n", + "3 206.0\n", + "4 135.0\n", + " ... \n", + "437 178.0\n", + "438 104.0\n", + "439 132.0\n", + "440 220.0\n", + "441 57.0\n", + "Name: label, Length: 442, dtype: float64" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "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", + " \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", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
agesexbmibps1s2s3s4s5s6
00.0380760.0506800.0616960.021872-0.044223-0.034821-0.043401-0.0025920.019907-0.017646
1-0.001882-0.044642-0.051474-0.026328-0.008449-0.0191630.074412-0.039493-0.068332-0.092204
20.0852990.0506800.044451-0.005670-0.045599-0.034194-0.032356-0.0025920.002861-0.025930
3-0.089063-0.044642-0.011595-0.0366560.0121910.024991-0.0360380.0343090.022688-0.009362
40.005383-0.044642-0.0363850.0218720.0039350.0155960.008142-0.002592-0.031988-0.046641
.................................
4370.0417080.0506800.0196620.059744-0.005697-0.002566-0.028674-0.0025920.0311930.007207
438-0.0055150.050680-0.015906-0.0676420.0493410.079165-0.0286740.034309-0.0181140.044485
4390.0417080.050680-0.0159060.017293-0.037344-0.013840-0.024993-0.011080-0.0468830.015491
440-0.045472-0.0446420.0390620.0012150.0163180.015283-0.0286740.0265600.044529-0.025930
441-0.045472-0.044642-0.073030-0.0814130.0837400.0278090.173816-0.039493-0.0042220.003064
\n", + "

442 rows × 10 columns

\n", + "
" + ], + "text/plain": [ + " age sex bmi bp s1 s2 s3 \\\n", + "0 0.038076 0.050680 0.061696 0.021872 -0.044223 -0.034821 -0.043401 \n", + "1 -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163 0.074412 \n", + "2 0.085299 0.050680 0.044451 -0.005670 -0.045599 -0.034194 -0.032356 \n", + "3 -0.089063 -0.044642 -0.011595 -0.036656 0.012191 0.024991 -0.036038 \n", + "4 0.005383 -0.044642 -0.036385 0.021872 0.003935 0.015596 0.008142 \n", + ".. ... ... ... ... ... ... ... \n", + "437 0.041708 0.050680 0.019662 0.059744 -0.005697 -0.002566 -0.028674 \n", + "438 -0.005515 0.050680 -0.015906 -0.067642 0.049341 0.079165 -0.028674 \n", + "439 0.041708 0.050680 -0.015906 0.017293 -0.037344 -0.013840 -0.024993 \n", + "440 -0.045472 -0.044642 0.039062 0.001215 0.016318 0.015283 -0.028674 \n", + "441 -0.045472 -0.044642 -0.073030 -0.081413 0.083740 0.027809 0.173816 \n", + "\n", + " s4 s5 s6 \n", + "0 -0.002592 0.019907 -0.017646 \n", + "1 -0.039493 -0.068332 -0.092204 \n", + "2 -0.002592 0.002861 -0.025930 \n", + "3 0.034309 0.022688 -0.009362 \n", + "4 -0.002592 -0.031988 -0.046641 \n", + ".. ... ... ... \n", + "437 -0.002592 0.031193 0.007207 \n", + "438 0.034309 -0.018114 0.044485 \n", + "439 -0.011080 -0.046883 0.015491 \n", + "440 0.026560 0.044529 -0.025930 \n", + "441 -0.039493 -0.004222 0.003064 \n", + "\n", + "[442 rows x 10 columns]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "features = pd.DataFrame(diabetes[\"data\"], columns = diabetes[\"feature_names\"])\n", + "\n", + "labels = pd.Series(diabetes[\"target\"], name = \"label\")\n", + "\n", + "\n", + "display(labels)\n", + "display(features)" ] }, { @@ -156,11 +545,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn.linear_model import LogisticRegression\n", + "\n", + "\n" ] }, { @@ -172,11 +563,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "\n", + "diabetes_model=LogisticRegression()" ] }, { @@ -190,11 +582,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "from sklearn.model_selection import train_test_split\n", + "\n", + "\n", + "\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size = 0.20)\n" ] }, { @@ -206,13 +603,97 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
LogisticRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "LogisticRegression()" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "diabetes_model.fit(X_train, y_train)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-0.47689552 -0.47723459 0.21560118 -0.47667844 0.62110288 -0.47698746\n", + " -0.4763342 -0.47708329 0.21630932 0.62052278 0.21614374 -0.47644956\n", + " 0.62081338 -0.47685838 0.21623374 -0.47662734 0.90761814 -0.47745359\n", + " -0.4762852 0.90728517 0.62057415 0.21598736 0.62090204 0.62084092\n", + " 0.62073102 0.21699923 -0.47632035 0.62128729 0.62154661 -0.47618159\n", + " 0.90873068 1.31074639 0.21604018 0.21592037 0.90822907 0.21634848\n", + " -0.47672768 -0.47641754 0.21671762 0.62165669 0.6219222 0.90891517\n", + " -0.4766664 -0.476843 0.21641717 0.21660099 0.90822368 0.62266996\n", + " 0.21607003 0.21585605 0.21652342 0.21634715 0.90737124 0.90842949\n", + " 0.21591773 -0.47621582 0.21618062 0.62202628 0.21596724 0.62155393\n", + " -0.47640924 -0.47632385 0.21701796 0.62199984 0.21551639 -0.47704884\n", + " 0.21597003 0.21623317 -0.47680295 -0.47610324 -0.47650029 0.21692389\n", + " -0.47603015 -0.47640856 -0.47705748 -0.47629911 0.90865379 0.2172712\n", + " 0.62278928 -0.47623103 0.21569791 0.21640676 0.21615442 -0.47679345\n", + " 0.21696382 -0.47637285 0.21722189 0.6218892 0.21602199 0.62186493\n", + " -0.47643498 0.21652023 0.21685638 0.21647865 -0.47674635 -0.47645125\n", + " 0.21718938 -0.47608667 0.21551223 0.21624524 -0.47607394 -0.47693236\n", + " 0.21754279 0.21720666 0.2158243 0.21645498 -0.47630029 0.21675263\n", + " 0.2171801 0.21694254 -0.47641655 0.62285564 -0.47675062 0.21732625\n", + " 0.21643697 0.21612473 -0.47676375 -0.47669488 0.21667753 -0.47614199\n", + " -0.4765325 -0.47601034 0.21647025 0.21653822 -0.47608697 0.21715085\n", + " -0.47612233 0.62282823 -0.47624697 1.31352737 -0.47701178 0.62270112\n", + " 0.21648274 -0.47573685 -0.4762694 -0.47673962 -0.475978 0.21590237\n", + " -0.47609224 -0.47638284 0.2173044 0.21684879 0.91032832 -0.4762047\n", + " -0.4759967 -0.47619413 0.21727463 -0.47577161 0.62262313 -0.47607776\n", + " 0.21739588 -0.4758404 -0.47585055 -0.47577283 -0.47602958 0.21777814\n", + " -0.4767747 -0.47610836 -0.47620865 -0.47651365 -0.47627927 0.21750564\n", + " 0.62258028 0.62311818 -0.47591999 0.21693575 -0.47596576 -0.47564754\n", + " 0.21714073 0.21725039 0.217717 -0.47619297 -0.4757573 -0.4759048\n", + " 0.62317053 -0.47665213 0.21798603 -0.47630709 -0.47604755 0.62365219\n", + " 0.21724255 0.21721716 -0.47583636 -0.47596502 -0.47573988 -0.47624783\n", + " -0.47575109 0.21751156 -0.4760063 -0.47626633 -0.47575311 -0.47576111\n", + " -0.47562952 -0.4761679 ]\n", + "[[ 0.00452603 -0.04275983 -0.00567168 ... -0.03817018 -0.06225322\n", + " -0.05365243]\n", + " [-0.02434857 -0.04263529 -0.06034434 ... -0.0746719 -0.05346289\n", + " -0.06583663]\n", + " [ 0.01271749 0.00995962 -0.05811031 ... -0.11230295 -0.12766612\n", + " 0.01298831]\n", + " ...\n", + " [ 0.06215184 0.05205105 0.07877698 ... 0.03482613 0.03160552\n", + " 0.04501583]\n", + " [ 0.0367773 0.0520906 0.11323831 ... -0.0018967 0.02974745\n", + " 0.07400235]\n", + " [-0.0464436 -0.04295159 0.16286211 ... 0.03503874 0.02986824\n", + " 0.01212249]]\n" + ] + } + ], + "source": [ + "print(diabetes_model.intercept_)\n", + "print(diabetes_model.coef_)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -231,11 +712,32 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([200., 72., 72., 72., 200., 200., 200., 72., 72., 200., 72.,\n", + " 72., 200., 200., 72., 200., 200., 200., 72., 200., 200., 200.,\n", + " 200., 72., 200., 200., 200., 72., 200., 200., 72., 200., 72.,\n", + " 200., 200., 200., 72., 72., 200., 72., 72., 200., 200., 72.,\n", + " 72., 200., 72., 72., 72., 72., 200., 200., 200., 72., 72.,\n", + " 200., 200., 200., 200., 200., 200., 72., 200., 200., 200., 200.,\n", + " 200., 200., 200., 200., 200., 200., 72., 72., 72., 72., 72.,\n", + " 72., 72., 200., 72., 72., 200., 200., 72., 200., 72., 72.,\n", + " 200.])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pred = diabetes_model.predict(X_test)\n", + "\n", + "pred" ] }, { @@ -247,11 +749,33 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "254 310.0\n", + "136 85.0\n", + "231 154.0\n", + "261 103.0\n", + "117 281.0\n", + " ... \n", + "69 178.0\n", + "53 104.0\n", + "247 51.0\n", + "170 47.0\n", + "140 118.0\n", + "Name: label, Length: 89, dtype: float64" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_test\n" ] }, { @@ -267,7 +791,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Your explanation here:\n" + "# completly different.\n" ] }, { @@ -351,11 +875,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto = pd.read_csv(\"auto-mpg.csv\")\n" ] }, { @@ -367,11 +891,123 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 36, + "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": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.head(5)\n" ] }, { @@ -383,11 +1019,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mpg float64\n", + "cylinders int64\n", + "displacement float64\n", + "horse_power float64\n", + "weight int64\n", + "acceleration float64\n", + "model_year int64\n", + "car_name object\n", + "dtype: object" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.dtypes" ] }, { @@ -399,11 +1054,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "70" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "auto[\"model_year\"].min()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "82" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto[\"model_year\"].max()" ] }, { @@ -415,11 +1101,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, + "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": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.isna().sum()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(398, 8)" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 52, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto.dropna(inplace=True)" ] }, { @@ -431,11 +1165,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 53, + "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": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto[\"cylinders\"].value_counts()\n" ] }, { @@ -451,11 +1201,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "auto.drop(\"car_name\", axis=1, inplace = True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "features = auto.drop(\"mpg\", axis=1)\n", + "\n", + "target = auto[\"mpg\"]\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.20)" ] }, { @@ -469,11 +1232,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.linear_model import LinearRegression\n", + "\n", + "#from sklearn.linear_model import LogisticRegression\n", + "\n", + "auto_model = LinearRegression()\n", + "\n", + "auto_model.fit(X_train, y_train)" ] }, { @@ -493,11 +1276,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "y_pred = auto_model.predict(X_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8102602725452244" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.metrics import r2_score\n", + "\n", + "r2_score(y_train, y_pred)\n", + "\n" ] }, { @@ -513,11 +1319,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 71, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7996092739567106" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_test_pred = auto_model.predict(X_test)\n", + "\n", + "\n", + "r2_score(y_test, y_test_pred)" ] }, { @@ -542,11 +1362,11 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "X_train09, X_test09, y_train09, y_test09 = train_test_split(features, target, test_size=0.1)\n" ] }, { @@ -558,11 +1378,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
LinearRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
" + ], + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto_model09 = LinearRegression()\n", + "\n", + "auto_model09.fit(X_train09, y_train09)" ] }, { @@ -574,11 +1410,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.805263470298514" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred_train09 = auto_model09.predict(X_train09)\n", + "\n", + "r2_score(y_train09, y_pred_train09)" ] }, { @@ -590,11 +1439,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.8452046003269366" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y_pred_test09 = auto_model09.predict(X_test09)\n", + "\n", + "r2_score(y_test09, y_pred_test09)\n", + "\n", + "\n", + "#there is some improvemente =)" ] }, { @@ -703,7 +1568,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -717,7 +1582,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.10.9" } }, "nbformat": 4,