diff --git a/04_lab_supervised_learning_sklearn.ipynb b/04_lab_supervised_learning_sklearn.ipynb new file mode 100644 index 0000000..b3131f1 --- /dev/null +++ b/04_lab_supervised_learning_sklearn.ipynb @@ -0,0 +1,1548 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "dbbPhFWyrNFi" + }, + "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": 3, + "metadata": { + "id": "qGpZPuUVrNFm" + }, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "from sklearn.tree import export_graphviz\n", + "from sklearn.model_selection import cross_validate\n", + "from sklearn.linear_model import LogisticRegression\n", + "from sklearn.metrics import confusion_matrix, classification_report, recall_score, precision_score" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "beEVEuZPrNFo" + }, + "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": { + "id": "amsj-DKYrNFo" + }, + "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": null, + "metadata": { + "id": "FmrMpXQHrNFp" + }, + "outputs": [], + "source": [ + "from sklearn.datasets import load_diabetes\n", + "\n", + "diabetes = load_diabetes()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2AkbTjYYrNFp" + }, + "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": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "k21bfo8QrNFp", + "outputId": "9f586b6e-5678-4663-8d26-4e4e354696ca" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename', 'data_module'])" + ] + }, + "metadata": {}, + "execution_count": 75 + } + ], + "source": [ + "diabetes.keys()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ONtwtUK8rNFp" + }, + "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": null, + "metadata": { + "scrolled": false, + "id": "YI60EaQbrNFq" + }, + "outputs": [], + "source": [ + "print(diabetes[\"DESCR\"])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "32gbLqbHrNFq" + }, + "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": { + "id": "toLljdNprNFq" + }, + "outputs": [], + "source": [ + "# 1. Number of Attributes: First 10 columns are numeric predictive values < ----- FEATURES\n", + "# Ten baseline variables, age, sex, body mass index, average blood pressure, and six blood serum measurements were obtained for each of n = 442 diabetes patients" + ] + }, + { + "cell_type": "code", + "source": [ + "# 2. The 10 x variables [data -> features] have been standardized to have mean 0 and squared length = 1\n", + "# Given a collection of possible predictors [data -> features] we select the one having largest absolute correlation with the response y [--> the target]\n", + "# \"Target\" is a quantitative measure of disease progression one year after baseline <------ TARGET" + ], + "metadata": { + "id": "DVUPbRzV8mwW" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "# 3. n = 442 diabetes patients" + ], + "metadata": { + "id": "NmMhgfdu9NgK" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xCIJqUJdrNFr" + }, + "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": null, + "metadata": { + "id": "oE16uUNfrNFr" + }, + "outputs": [], + "source": [ + "features = pd.DataFrame(diabetes[\"data\"], columns = diabetes[\"feature_names\"])\n", + "label = pd.Series(diabetes[\"target\"], name = \"label\")\n", + "\n", + "display(features.head())" + ] + }, + { + "cell_type": "code", + "source": [ + "display(label.head())" + ], + "metadata": { + "id": "PHonlsg--Irl" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XucRkAourNFr" + }, + "source": [ + "# Challenge 2 - Perform Supervised Learning on the Dataset" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iyCOP8_orNFr" + }, + "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": null, + "metadata": { + "id": "G0SgGP44rNFs" + }, + "outputs": [], + "source": [ + "from sklearn.linear_model import LinearRegression" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6Y7RinPerNFs" + }, + "source": [ + "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "5rUdXQburNFt" + }, + "outputs": [], + "source": [ + "diabetes_model = LinearRegression()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hd7X7E8wrNFt" + }, + "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": null, + "metadata": { + "id": "AoLyYTN6rNFt" + }, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split\n", + "import matplotlib.pyplot as plt\n", + "\n", + "diabetes_data_train, diabetes_data_test, diabetes_target_train, diabetes_target_test = train_test_split(features, label, test_size = 0.20, random_state = 0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "65vSZuJrrNFu" + }, + "source": [ + "Fit the training data and target to `diabetes_model`. Print the *intercept* and *coefficients* of the model." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "U6cJIAiirNFu" + }, + "outputs": [], + "source": [ + "diabetes_model.fit(diabetes_data_train, diabetes_target_train)" + ] + }, + { + "cell_type": "code", + "source": [ + "model = LinearRegression()\n", + "\n", + "# we will train the model (fit). This allows LinearRegression model do understand our data and pick the best reg line\n", + "diabetes_model.fit(diabetes_data_train, diabetes_target_train)\n", + "\n", + "slope = diabetes_model.coef_\n", + "interception = diabetes_model.intercept_\n", + "\n", + "print(f\"Slope of our Reg line is {slope} \\n Y-intercept is {interception}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6RYMGaG6hqJO", + "outputId": "1b013d16-c1c3-4263-940e-72f229cf04e8" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Slope of our Reg line is [ -35.55025079 -243.16508959 562.76234744 305.46348218 -662.70290089\n", + " 324.20738537 24.74879489 170.3249615 731.63743545 43.0309307 ] \n", + " Y-intercept is 152.5380470138517\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#reg_line = interception + slope*diabetes_data_train #(b0 + b1 * SAT)\n", + "\n", + "#plt.plot(diabetes_data_train, reg_line,c=\"orange\")\n", + "#plt.scatter(diabetes_data_train, diabetes_target_train)\n", + "#plt.show()" + ], + "metadata": { + "id": "MFU9ohAAuLrz" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z0KqquxrrNFu" + }, + "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": null, + "metadata": { + "id": "NqBi1XzorNFu" + }, + "outputs": [], + "source": [ + "diabetes_model.score(diabetes_data_train, diabetes_target_train)\n", + "pred_y = diabetes_model.predict(diabetes_data_test)\n", + "pred_y" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wF480_strNFv" + }, + "source": [ + "#### Print your `diabetes_target_test` and compare with the prediction." + ] + }, + { + "cell_type": "code", + "source": [ + "diabetes_target_test.values" + ], + "metadata": { + "id": "8oudwF4R-LaH" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "POBibVRArNFv" + }, + "source": [ + "#### Is `diabetes_target_test` exactly the same as the model prediction? Explain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "0ZfK-SdUrNFv" + }, + "outputs": [], + "source": [ + "pred_tt_vs_y = diabetes_target_test.values - pred_y\n", + "pred_tt_vs_y\n", + "# Our prediction is based on the model, but won't be exactly the same - and it never will !!!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UZn2tX-hrNFv" + }, + "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": { + "id": "v5vS8ZXxrNFw" + }, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "WILUW-x1rNFw" + }, + "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": { + "id": "Ic65poP8rNFw" + }, + "outputs": [], + "source": [ + "# Your answers here:" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EpbI9bUyrNFw" + }, + "source": [ + "# Challenge 3 - Peform Supervised Learning on a Pandas Dataframe" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nkSKmL0ErNFx" + }, + "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": 4, + "metadata": { + "id": "2DSTlJEgrNFx" + }, + "outputs": [], + "source": [ + "auto = pd.read_csv(\"/content/auto-mpg.csv\")" + ] + }, + { + "cell_type": "code", + "source": [ + "print(f\"The actual shape of my data is {auto.shape[0]} rows and {auto.shape[1]} columns\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_oTM6TgVDH4x", + "outputId": "1f9a0f60-c2c3-4c00-f0f3-b4836b4c7ccf" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The actual shape of my data is 398 rows and 8 columns\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Lm-6aD2CrNFx" + }, + "source": [ + "Look at the first 5 rows using the `head()` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "IRvXKwbzrNFx", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 206 + }, + "outputId": "d899bd4e-678e-444a-c423-e12d4ee6e443" + }, + "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", + "
\n", + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \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", + "
\n", + "
\n", + "\n", + "
\n", + " \n", + "\n", + " \n", + "\n", + " \n", + "
\n", + "\n", + "\n", + "
\n", + " \n", + "\n", + "\n", + "\n", + " \n", + "
\n", + "
\n", + "
\n" + ] + }, + "metadata": {}, + "execution_count": 6 + } + ], + "source": [ + "auto.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CBM_XNP3rNFx" + }, + "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": 7, + "metadata": { + "id": "gT1AUqcNrNFy", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c2e8b55f-80d8-47d1-af6c-e2aeda94babe" + }, + "outputs": [ + { + "output_type": "execute_result", + "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" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "auto.dtypes # car_name is the only classified as an object (correctly), we will get there, right?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CdDrZDM8rNF4" + }, + "source": [ + "What is the newest model year and the oldest model year?" + ] + }, + { + "cell_type": "code", + "source": [ + "new = auto[\"model_year\"].max()\n", + "old = auto[\"model_year\"].min()\n", + "print(f\"The oldest model year is from 19{old} while the newest is from 19{new}!\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "3tVTmtE_CSxa", + "outputId": "621183b0-e262-42a6-be61-91bb230207a2" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "The oldest model year is from 1970 while the newest is from 1982!\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ebvC7l8JrNF4" + }, + "source": [ + "Check the dataset for missing values and remove all rows containing at least one missing value." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ohvhX7jxrNF4" + }, + "outputs": [], + "source": [ + "auto.isnull().sum()" + ] + }, + { + "cell_type": "code", + "source": [ + "auto.dropna(axis = 0, inplace = True)" + ], + "metadata": { + "id": "XSI-sL71D5YJ" + }, + "execution_count": 10, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "t3fpRvOrrNF5" + }, + "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": 11, + "metadata": { + "id": "Z1LNe4q7rNF5", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "775b7f7e-74cf-4001-c907-56f1197a405d" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4 199\n", + "8 103\n", + "6 83\n", + "3 4\n", + "5 3\n", + "Name: cylinders, dtype: int64" + ] + }, + "metadata": {}, + "execution_count": 11 + } + ], + "source": [ + "auto[\"cylinders\"].value_counts()\n", + "# 5 types of cylinders" + ] + }, + { + "cell_type": "code", + "source": [ + "auto" + ], + "metadata": { + "id": "y65HNfXMHN5I" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "p-QL7rIWrNF5" + }, + "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": 13, + "metadata": { + "id": "rz_GuSL8rNF5" + }, + "outputs": [], + "source": [ + "auto.drop(\"car_name\", axis = 1, inplace = True)\n", + "# drop done" + ] + }, + { + "cell_type": "code", + "source": [ + "from sklearn.model_selection import train_test_split\n", + "\n", + "features = auto.drop(columns = [\"mpg\"])\n", + "target = auto[\"mpg\"]\n", + "\n", + "X_train, x_test, y_train, x_test = train_test_split(features, target, test_size = 0.20, random_state = 0)" + ], + "metadata": { + "id": "55SqMvLKHecv" + }, + "execution_count": 29, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rMj7LXTKrNF6" + }, + "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": 25, + "metadata": { + "id": "3b-QY8HurNF6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 74 + }, + "outputId": "6960da19-c962-4b46-8071-aa884464feb0" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "LinearRegression()" + ], + "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.
" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ], + "source": [ + "from sklearn.linear_model import LinearRegression\n", + "\n", + "auto_model = LinearRegression()\n", + "auto_model.fit(X_train, y_train)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aTBdbo28rNF6" + }, + "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](https://en.wikipedia.org/wiki/Coefficient_of_determination). In the end, we want the r-squared score to be as high 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", + "source": [ + "auto_model = LinearRegression()\n", + "auto_model.fit(X_train, y_train)" + ], + "metadata": { + "id": "3PnD2oPetXBr" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "features = auto.drop(columns = [\"mpg\"])\n", + "target = auto[\"mpg\"]\n", + "\n", + "X_train, x_test, y_train, y_test = train_test_split(features, target, test_size = 0.20, random_state = 0)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "k50X9kMRuZAv", + "outputId": "f9bc48fa-5a03-4769-a78e-6a6e82c347c6" + }, + "execution_count": 43, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.8088490656511089" + ] + }, + "metadata": {}, + "execution_count": 43 + } + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "id": "51tXeflNrNF6", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "0852b5b4-4551-43b5-f8a7-ff2d6588cea1" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "r^2: 0.8088938602131774\n" + ] + } + ], + "source": [ + "from sklearn.metrics import mean_squared_error\n", + "\n", + "auto_model.score(X_train, y_train)\n", + "print(\"r^2: \", auto_model.score(x_test, y_test))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "O5AXf0-0rNF6" + }, + "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": 47, + "metadata": { + "id": "WmAQRBVwrNF7", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d87f94ed-af5f-41d9-a92d-854caf574656" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "RMSE: 3.446485498136574\n" + ] + } + ], + "source": [ + "y_test_pred = auto_model.predict(x_test) ## for RMSE we need a prediction\n", + "print(\"RMSE:\",np.sqrt(mean_squared_error(y_test_pred , y_test)))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "27PzNpXUrNF7" + }, + "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": { + "id": "wTLVZHtCrNF7" + }, + "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": 48, + "metadata": { + "id": "j0wfH2QNrNF8" + }, + "outputs": [], + "source": [ + "X_train09, X_test09, y_train09, y_test09 = train_test_split(features, target, test_size = 0.10, random_state = 0)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Mw-I1mZsrNF8" + }, + "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": null, + "metadata": { + "id": "vdcOuuiFrNF8" + }, + "outputs": [], + "source": [ + "auto_model09 = LinearRegression()\n", + "auto_model09.fit(X_train09, y_train09)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "M1dxjmfUrNF9" + }, + "source": [ + "Compute the predicted values and r squared score for our new model and new sample data." + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "id": "GX9NMlmbrNF9", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "147be69f-82f4-44c0-979b-bf2c2a6d3d40" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "r^2: 0.7966346926868111\n", + "RMSE: 3.458674654946117\n" + ] + } + ], + "source": [ + "auto_model.score(X_train09, y_train09)\n", + "print(\"r^2: \", auto_model.score(X_test09, y_test09))\n", + "\n", + "y_test_pred = auto_model.predict(X_test09) ## for RMSE we need a prediction\n", + "print(\"RMSE:\",np.sqrt(mean_squared_error(y_test_pred , y_test09)))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LfVJzgvOrNF9" + }, + "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": null, + "metadata": { + "id": "hXz_QL3jrNF9" + }, + "outputs": [], + "source": [ + "auto_model.score(X_train09, y_train09)\n", + "print(\"r^2: \", auto_model.score(X_test09, y_test09))\n", + "# did not get this one ... sorry" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Drf0YATTrNF-" + }, + "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": { + "id": "QWSAha4YrNF-" + }, + "outputs": [], + "source": [ + "from sklearn.feature_selection import RFE" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KL_U7IvmrNF-" + }, + "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": { + "id": "fxSbkZ--rNF_" + }, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DIllawM0rNF_" + }, + "source": [ + "Fit the model and print the ranking" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "2W5JkM2FrNGA" + }, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "OCQGCbt1rNGA" + }, + "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": { + "id": "SSbpV-92rNGA" + }, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "omKToGC_rNGB" + }, + "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": { + "id": "94jVvn7IrNGB" + }, + "outputs": [], + "source": [ + "# Your code here:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JJFNRjeWrNGB" + }, + "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", + "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.6.9" + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/.gitignore b/your-code/.gitignore similarity index 100% rename from .gitignore rename to your-code/.gitignore diff --git a/README.md b/your-code/README.md similarity index 100% rename from README.md rename to your-code/README.md 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/ols-results.png b/your-code/ols-results.png similarity index 100% rename from ols-results.png rename to your-code/ols-results.png diff --git a/statsmodels.png b/your-code/statsmodels.png similarity index 100% rename from statsmodels.png rename to your-code/statsmodels.png