diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 8818016..aac055b 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -2,99 +2,101 @@ "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!" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 1, - "metadata": {}, - "outputs": [], "source": [ "# Libraries\n", "import pandas as pd\n", "import numpy as np\n", "from sklearn import datasets\n" - ] + ], + "outputs": [], + "metadata": {} }, { "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." - ] + ], + "metadata": {} }, { "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))." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], + "execution_count": 7, "source": [ - "# your code here\n" - ] + "from sklearn.datasets import load_diabetes" + ], + "outputs": [], + "metadata": {} }, { "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." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 3, - "metadata": {}, + "execution_count": 8, + "source": [ + "diabetes = load_diabetes()\n", + "\n", + "diabetes.keys()" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])" ] }, - "execution_count": 3, "metadata": {}, - "output_type": "execute_result" + "execution_count": 8 } ], - "source": [ - "# your code here\n" - ] + "metadata": {} }, { "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.*" - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "scrolled": false - }, + "execution_count": 11, + "source": [ + "print(diabetes.DESCR)" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ ".. _diabetes_dataset:\n", "\n", @@ -119,11 +121,11 @@ " - sex\n", " - bmi body mass index\n", " - bp average blood pressure\n", - " - s1 tc, T-Cells (a type of white blood cells)\n", + " - s1 tc, total serum cholesterol\n", " - s2 ldl, low-density lipoproteins\n", " - s3 hdl, high-density lipoproteins\n", - " - s4 tch, thyroid stimulating hormone\n", - " - s5 ltg, lamotrigine\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", @@ -137,13 +139,12 @@ ] } ], - "source": [ - "# your code here\n" - ] + "metadata": { + "scrolled": false + } }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Based on the data description, answer the following questions:\n", "\n", @@ -152,88 +153,110 @@ "1. What is the relation between `diabetes['data']` and `diabetes['target']`?\n", "\n", "1. How many records are there in the data?" - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], + "execution_count": 22, "source": [ - "# your answer here \n" - ] + " " + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(442, 10)" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": 16, + "source": [ + "There are 10 features in the DataSet.\n", + "They are describing, age, sex, bmi and several other body and blood measurments.\n", + "The Data table shows what was measured and the target is what was excpeted of the patients tests.\n", + "There are 442 records in data." + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "7" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ], + "metadata": {} }, { "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." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, + "execution_count": 23, + "source": [ + "diabetes[\"data\"].shape" + ], "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Shape of 'data' :\n" - ] - }, - { + "output_type": "execute_result", "data": { "text/plain": [ "(442, 10)" ] }, - "execution_count": 6, "metadata": {}, - "output_type": "execute_result" + "execution_count": 23 } ], - "source": [ - "# your code here\n" - ] + "metadata": {} }, { "cell_type": "code", - "execution_count": 7, - "metadata": {}, + "execution_count": 25, + "source": [ + "diabetes[\"target\"].shape" + ], "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Shape of 'target' :\n" - ] - }, - { + "output_type": "execute_result", "data": { "text/plain": [ "(442,)" ] }, - "execution_count": 7, "metadata": {}, - "output_type": "execute_result" + "execution_count": 25 } ], - "source": [] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "# Challenge 2 - Perform Supervised Learning on the Dataset" - ] + ], + "metadata": {} }, { "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", @@ -254,113 +277,124 @@ "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`. " - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], + "execution_count": 28, "source": [ - "# your code here\n" - ] + "from sklearn.linear_model import LinearRegression\n", + "from sklearn.preprocessing import tra" + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], + "execution_count": 29, "source": [ - "# your code here\n" - ] + "diabetes_model = LinearRegression()\n", + "\n" + ], + "outputs": [], + "metadata": {} }, { "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." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], + "execution_count": 30, "source": [ - "# your code here\n" - ] + "pd.DataFrame(diabetes[\"data\"])\n", + "\n", + "pd.DataFrame(diabetes[\"target\"])\n", + "\n", + "diabetes_data_train = diabetes[\"data\"][:-20]\n", + "diabetes_target_train = diabetes[\"target\"][:-20]\n", + "\n", + "\n", + "\n", + "diabetes_data_test = diabetes[\"data\"][-20:]\n", + "diabetes_target_test = diabetes[\"target\"][-20:]" + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "Fit the training data and target to `diabetes_model`. Print the *intercept* and *coefficients* of the model." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 14, - "metadata": {}, + "execution_count": 31, + "source": [ + "model = diabetes_model.fit(diabetes_data_train, diabetes_target_train)" + ], + "outputs": [], + "metadata": {} + }, + { + "cell_type": "code", + "execution_count": 32, + "source": [ + "model.intercept_" + ], "outputs": [ { + "output_type": "execute_result", "data": { "text/plain": [ - "LinearRegression()" + "152.76430691633442" ] }, - "execution_count": 14, "metadata": {}, - "output_type": "execute_result" + "execution_count": 32 } ], - "source": [ - "# your code here\n" - ] + "metadata": {} }, { "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Intercept: 152.76430691633442\n" - ] - } + "execution_count": 33, + "source": [ + "model.coef_" ], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "Coefficients: [ 3.03499549e-01 -2.37639315e+02 5.10530605e+02 3.27736980e+02\n", - " -8.14131709e+02 4.92814588e+02 1.02848452e+02 1.84606489e+02\n", - " 7.43519617e+02 7.60951722e+01]\n" - ] + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 3.03499549e-01, -2.37639315e+02, 5.10530605e+02, 3.27736980e+02,\n", + " -8.14131709e+02, 4.92814588e+02, 1.02848452e+02, 1.84606489e+02,\n", + " 7.43519617e+02, 7.60951722e+01])" + ] + }, + "metadata": {}, + "execution_count": 33 } ], - "source": [] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Inspecting the results\n", "\n", @@ -372,65 +406,75 @@ "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`." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], + "execution_count": 34, "source": [ - "# your code here\n" - ] + "model.predict(diabetes_data_test)\n" + ], + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([197.61846908, 155.43979328, 172.88665147, 111.53537279,\n", + " 164.80054784, 131.06954875, 259.12237761, 100.47935157,\n", + " 117.0601052 , 124.30503555, 218.36632793, 61.19831284,\n", + " 132.25046751, 120.3332925 , 52.54458691, 194.03798088,\n", + " 102.57139702, 123.56604987, 211.0346317 , 52.60335674])" + ] + }, + "metadata": {}, + "execution_count": 34 + } + ], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Print your `diabetes_target_test` and compare with the prediction. " - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 23, - "metadata": {}, + "execution_count": 35, + "source": [ + "diabetes_target_test\n" + ], "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "diabetes_target_test:\n" - ] - }, - { + "output_type": "execute_result", "data": { "text/plain": [ "array([233., 91., 111., 152., 120., 67., 310., 94., 183., 66., 173.,\n", " 72., 49., 64., 48., 178., 104., 132., 220., 57.])" ] }, - "execution_count": 23, "metadata": {}, - "output_type": "execute_result" + "execution_count": 35 } ], - "source": [ - "# your code here\n" - ] + "metadata": {} }, { "cell_type": "code", "execution_count": 24, - "metadata": {}, + "source": [], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ "test prediction:\n" ] }, { + "output_type": "execute_result", "data": { "text/plain": [ "array([197.61846908, 155.43979328, 172.88665147, 111.53537279,\n", @@ -440,32 +484,30 @@ " 102.57139702, 123.56604987, 211.0346317 , 52.60335674])" ] }, - "execution_count": 24, "metadata": {}, - "output_type": "execute_result" + "execution_count": 24 } ], - "source": [] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Is `diabetes_target_test` exactly the same as the model prediction? Explain." - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 25, - "metadata": {}, - "outputs": [], "source": [ - "# your answer here \n" - ] + "No it is not exactly the same. Some of our predictions are way off, we need to train the model better or use a better model all toghetrer.\n" + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "# Bonus Challenge 1 - Hypothesis Testing with `statsmodels`\n", "\n", @@ -491,16 +533,21 @@ "Your output should look like:\n", "\n", "
" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 19, - "metadata": {}, + "source": [ + "# your code here\n", + "import statsmodels.api as sm\n", + "\n" + ], "outputs": [ { - "name": "stdout", "output_type": "stream", + "name": "stdout", "text": [ " OLS Regression Results \n", "==============================================================================\n", @@ -539,15 +586,10 @@ ] } ], - "source": [ - "# your code here\n", - "import statsmodels.api as sm\n", - "\n" - ] + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "#### Interpreting hypothesis testing results\n", "\n", @@ -558,56 +600,75 @@ "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?" - ] + ], + "metadata": {} }, { "cell_type": "code", "execution_count": 41, - "metadata": {}, - "outputs": [], "source": [ "# your answer here\n" - ] + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "# Challenge 3 - Peform Supervised Learning on a Pandas Dataframe" - ] + ], + "metadata": {} }, { "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`." - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [], + "execution_count": 3, "source": [ - "# your code here\n" - ] + "auto = pd.read_csv(\"/Users/m.soren/Desktop/Ironhack/Labs/lab-supervised-learning-sklearn/data/auto-mpg.csv\")" + ], + "outputs": [], + "metadata": {} }, { "cell_type": "markdown", - "metadata": {}, "source": [ "Look at the first 5 rows using the `head()` function:" - ] + ], + "metadata": {} }, { "cell_type": "code", - "execution_count": 27, - "metadata": {}, + "execution_count": 4, + "source": [ + "auto.head()" + ], "outputs": [ { + "output_type": "execute_result", "data": { + "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\" " + ], "text/html": [ "
\n", "