diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb
new file mode 100644
index 0000000..52f95ab
--- /dev/null
+++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb
@@ -0,0 +1,1633 @@
+{
+ "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": 1,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Import your libraries:\n",
+ "\n",
+ "import pandas as pd\n",
+ "pd.set_option('display.max_columns', None)\n",
+ "import numpy as np\n",
+ "\n",
+ "import pylab as plt\n",
+ "import seaborn as sns\n",
+ "%matplotlib inline\n",
+ "from sklearn.model_selection import train_test_split as tts\n",
+ "\n",
+ "from sklearn.linear_model import LinearRegression as LinReg\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "\n",
+ "import warnings\n",
+ "warnings.simplefilter('ignore')"
+ ]
+ },
+ {
+ "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": 2,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "from sklearn import datasets\n",
+ "\n",
+ "diabetesDataset = 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": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "diabetesDataset.keys()"
+ ]
+ },
+ {
+ "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": 4,
+ "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(diabetesDataset.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": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "((442, 10), (442,))"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Enter your answer here:\n",
+ "# 1: 10 2: las data son variables independientes que afectan a la var depen. 3: 442\n",
+ "\n",
+ "diabetesDataset['data'].shape, diabetesDataset['target'].shape"
+ ]
+ },
+ {
+ "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": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n",
+ " 0.01990842, -0.01764613],\n",
+ " [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n",
+ " -0.06832974, -0.09220405],\n",
+ " [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n",
+ " 0.00286377, -0.02593034],\n",
+ " ...,\n",
+ " [ 0.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]])"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "diabetesDataset.data"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Challenge 2 - Perform Supervised Learning on the Dataset"
+ ]
+ },
+ {
+ "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": 7,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "from sklearn.linear_model import LinearRegression as LinReg"
+ ]
+ },
+ {
+ "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": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "diabetes_model= LinReg()"
+ ]
+ },
+ {
+ "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": 9,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "X=diabetesDataset.data\n",
+ "\n",
+ "y=diabetesDataset.target\n",
+ "\n",
+ "X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2, train_size=0.8, 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": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "151.3456553477407"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "diabetes_model.fit(X_train, y_train)\n",
+ "diabetes_model.intercept_"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "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": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "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": 12,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "from sklearn.metrics import confusion_matrix\n",
+ "from sklearn.metrics import accuracy_score\n",
+ "from sklearn.metrics import r2_score\n",
+ "y_pred=diabetes_model.predict(X_test)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### Print your `diabetes_target_test` and compare with the prediction. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[139.5483133 179.52030578 134.04133298 291.41193598 123.78723656\n",
+ " 92.17357677 258.23409704 181.33895238 90.22217862 108.63143298\n",
+ " 94.13938654 168.43379636 53.50669663 206.63040068 100.13238561\n",
+ " 130.66881649 219.53270758 250.78291772 196.36682356 218.57497401\n",
+ " 207.35002447 88.48361667 70.43428801 188.95725301 154.88720039\n",
+ " 159.35957695 188.31587948 180.38835506 47.98988446 108.97514644\n",
+ " 174.78080029 86.36598906 132.95890535 184.5410226 173.83298051\n",
+ " 190.35863287 124.41740796 119.65426903 147.95402494 59.05311211\n",
+ " 71.62636914 107.68722902 165.45544477 155.00784964 171.04558668\n",
+ " 61.45763075 71.66975626 114.96330486 51.57808027 167.57781958\n",
+ " 152.52505798 62.95827693 103.49862017 109.20495627 175.63844013\n",
+ " 154.60247734 94.41476124 210.74244148 120.25601864 77.61590087\n",
+ " 187.93503183 206.49543321 140.63018684 105.59463059 130.704246\n",
+ " 202.18650868 171.1330116 164.91246096 124.72637597 144.81210187\n",
+ " 181.99631481 199.41234515 234.21402489 145.96053305 79.86349114\n",
+ " 157.36828831 192.74737754 208.8980067 158.58505486 206.0226849\n",
+ " 107.47978402 140.93428553 54.81856678 55.92807758 115.00974554\n",
+ " 78.95886675 81.55731377 54.3774778 166.25477778]\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "print(y_pred)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.45260660216173787"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "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": 15,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your explanation here:\n",
+ "# Este es un modelo malo.\n",
+ "# El R2 es 0.45, por lo que tenemos valores que el modelo no es capaz de predecir correctamente."
+ ]
+ },
+ {
+ "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",
+ ""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "import statsmodels.api as sm"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " OLS Regression Results \n",
+ "=======================================================================================\n",
+ "Dep. Variable: y R-squared (uncentered): 0.136\n",
+ "Model: OLS Adj. R-squared (uncentered): 0.110\n",
+ "Method: Least Squares F-statistic: 5.378\n",
+ "Date: Tue, 12 Oct 2021 Prob (F-statistic): 2.13e-07\n",
+ "Time: 21:20:23 Log-Likelihood: -2293.0\n",
+ "No. Observations: 353 AIC: 4606.\n",
+ "Df Residuals: 343 BIC: 4645.\n",
+ "Df Model: 10 \n",
+ "Covariance Type: nonrobust \n",
+ "==============================================================================\n",
+ " coef std err t P>|t| [0.025 0.975]\n",
+ "------------------------------------------------------------------------------\n",
+ "x1 125.2081 206.271 0.607 0.544 -280.507 530.924\n",
+ "x2 -258.2036 204.877 -1.260 0.208 -661.177 144.770\n",
+ "x3 627.4875 229.885 2.730 0.007 175.325 1079.650\n",
+ "x4 346.3186 213.207 1.624 0.105 -73.039 765.676\n",
+ "x5 -803.2240 1347.949 -0.596 0.552 -3454.510 1848.062\n",
+ "x6 312.8181 1087.888 0.288 0.774 -1826.953 2452.589\n",
+ "x7 45.0676 696.193 0.065 0.948 -1324.277 1414.412\n",
+ "x8 211.9779 553.944 0.383 0.702 -877.577 1301.532\n",
+ "x9 700.8248 574.988 1.219 0.224 -430.122 1831.771\n",
+ "x10 144.6897 219.349 0.660 0.510 -286.749 576.128\n",
+ "==============================================================================\n",
+ "Omnibus: 0.382 Durbin-Watson: 0.209\n",
+ "Prob(Omnibus): 0.826 Jarque-Bera (JB): 0.510\n",
+ "Skew: 0.033 Prob(JB): 0.775\n",
+ "Kurtosis: 2.826 Cond. No. 20.8\n",
+ "==============================================================================\n",
+ "\n",
+ "Notes:\n",
+ "[1] R² is computed without centering (uncentered) since the model does not contain a constant.\n",
+ "[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
+ ]
+ }
+ ],
+ "source": [
+ "mod = sm.OLS(y_train, X_train)\n",
+ "\n",
+ "res = mod.fit()\n",
+ "\n",
+ "print(res.summary())"
+ ]
+ },
+ {
+ "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": 18,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your answers here:\n",
+ "\n",
+ "#Rechaz la H0 de que todas son significativas\n",
+ "#Todas menos la masa corporal\n",
+ "#eliminando las columnas con peor significatividad"
+ ]
+ },
+ {
+ "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": 19,
+ "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": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " mpg | \n",
+ " cylinders | \n",
+ " displacement | \n",
+ " horse_power | \n",
+ " weight | \n",
+ " acceleration | \n",
+ " model_year | \n",
+ " car_name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 307.0 | \n",
+ " 130.0 | \n",
+ " 3504 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"chevrolet chevelle malibu\" | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 15.0 | \n",
+ " 8 | \n",
+ " 350.0 | \n",
+ " 165.0 | \n",
+ " 3693 | \n",
+ " 11.5 | \n",
+ " 70 | \n",
+ " \\t\"buick skylark 320\" | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 318.0 | \n",
+ " 150.0 | \n",
+ " 3436 | \n",
+ " 11.0 | \n",
+ " 70 | \n",
+ " \\t\"plymouth satellite\" | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 16.0 | \n",
+ " 8 | \n",
+ " 304.0 | \n",
+ " 150.0 | \n",
+ " 3433 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"amc rebel sst\" | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 17.0 | \n",
+ " 8 | \n",
+ " 302.0 | \n",
+ " 140.0 | \n",
+ " 3449 | \n",
+ " 10.5 | \n",
+ " 70 | \n",
+ " \\t\"ford torino\" | \n",
+ "
\n",
+ " \n",
+ "
\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": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.head()"
+ ]
+ },
+ {
+ "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": 21,
+ "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": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.dtypes"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "What is the newest model year and the oldest model year?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "mpg 28.0\n",
+ "cylinders 4\n",
+ "displacement 112.0\n",
+ "horse_power 88.0\n",
+ "weight 2605\n",
+ "acceleration 19.6\n",
+ "model_year 82\n",
+ "car_name \\t\"chevrolet cavalier\"\n",
+ "Name: 367, dtype: object"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.loc[auto.model_year.argmax()]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "El modelo mas nuevo es 82\n",
+ "El modelo mas viejo es 70\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(f'El modelo mas nuevo es {auto[\"model_year\"].max()}')\n",
+ "print(f'El modelo mas viejo es {auto[\"model_year\"].min()}')"
+ ]
+ },
+ {
+ "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": 24,
+ "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": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.isnull().sum()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(398, 8)"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "\n",
+ "auto.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "auto = auto.dropna()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(392, 8)"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "auto.shape"
+ ]
+ },
+ {
+ "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": 28,
+ "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": 28,
+ "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": 29,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " mpg | \n",
+ " cylinders | \n",
+ " displacement | \n",
+ " horse_power | \n",
+ " weight | \n",
+ " acceleration | \n",
+ " model_year | \n",
+ " car_name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 307.0 | \n",
+ " 130.0 | \n",
+ " 3504 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"chevrolet chevelle malibu\" | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 15.0 | \n",
+ " 8 | \n",
+ " 350.0 | \n",
+ " 165.0 | \n",
+ " 3693 | \n",
+ " 11.5 | \n",
+ " 70 | \n",
+ " \\t\"buick skylark 320\" | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 318.0 | \n",
+ " 150.0 | \n",
+ " 3436 | \n",
+ " 11.0 | \n",
+ " 70 | \n",
+ " \\t\"plymouth satellite\" | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 16.0 | \n",
+ " 8 | \n",
+ " 304.0 | \n",
+ " 150.0 | \n",
+ " 3433 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"amc rebel sst\" | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 17.0 | \n",
+ " 8 | \n",
+ " 302.0 | \n",
+ " 140.0 | \n",
+ " 3449 | \n",
+ " 10.5 | \n",
+ " 70 | \n",
+ " \\t\"ford torino\" | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 393 | \n",
+ " 27.0 | \n",
+ " 4 | \n",
+ " 140.0 | \n",
+ " 86.0 | \n",
+ " 2790 | \n",
+ " 15.6 | \n",
+ " 82 | \n",
+ " \\t\"ford mustang gl\" | \n",
+ "
\n",
+ " \n",
+ " | 394 | \n",
+ " 44.0 | \n",
+ " 4 | \n",
+ " 97.0 | \n",
+ " 52.0 | \n",
+ " 2130 | \n",
+ " 24.6 | \n",
+ " 82 | \n",
+ " \\t\"vw pickup\" | \n",
+ "
\n",
+ " \n",
+ " | 395 | \n",
+ " 32.0 | \n",
+ " 4 | \n",
+ " 135.0 | \n",
+ " 84.0 | \n",
+ " 2295 | \n",
+ " 11.6 | \n",
+ " 82 | \n",
+ " \\t\"dodge rampage\" | \n",
+ "
\n",
+ " \n",
+ " | 396 | \n",
+ " 28.0 | \n",
+ " 4 | \n",
+ " 120.0 | \n",
+ " 79.0 | \n",
+ " 2625 | \n",
+ " 18.6 | \n",
+ " 82 | \n",
+ " \\t\"ford ranger\" | \n",
+ "
\n",
+ " \n",
+ " | 397 | \n",
+ " 31.0 | \n",
+ " 4 | \n",
+ " 119.0 | \n",
+ " 82.0 | \n",
+ " 2720 | \n",
+ " 19.4 | \n",
+ " 82 | \n",
+ " \\t\"chevy s-10\" | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
392 rows × 8 columns
\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",
+ "393 27.0 4 140.0 86.0 2790 15.6 \n",
+ "394 44.0 4 97.0 52.0 2130 24.6 \n",
+ "395 32.0 4 135.0 84.0 2295 11.6 \n",
+ "396 28.0 4 120.0 79.0 2625 18.6 \n",
+ "397 31.0 4 119.0 82.0 2720 19.4 \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\" \n",
+ ".. ... ... \n",
+ "393 82 \\t\"ford mustang gl\" \n",
+ "394 82 \\t\"vw pickup\" \n",
+ "395 82 \\t\"dodge rampage\" \n",
+ "396 82 \\t\"ford ranger\" \n",
+ "397 82 \\t\"chevy s-10\" \n",
+ "\n",
+ "[392 rows x 8 columns]"
+ ]
+ },
+ "execution_count": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "auto"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "X=auto.drop(columns=['mpg', 'car_name'])._get_numeric_data()\n",
+ "\n",
+ "y=auto.mpg\n",
+ "\n",
+ "X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2, train_size=0.8, random_state=42)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "((313, 6), (313,), (79, 6), (79,))"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "X_train.shape, y_train.shape, X_test.shape, y_test.shape"
+ ]
+ },
+ {
+ "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": 32,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "from sklearn.linear_model import LinearRegression as LinReg\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "\n",
+ "auto_model=LinReg()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "LinearRegression()"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "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",
+ "\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": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.7665327707747775"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "y_pred=auto_model.predict(X_train)\n",
+ "r2_score(y_pred, y_train)"
+ ]
+ },
+ {
+ "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": 35,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.8037468831261811"
+ ]
+ },
+ "execution_count": 35,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "\n",
+ "y_test_pred=auto_model.predict(X_test)\n",
+ "r2_score(y_test_pred, y_test)"
+ ]
+ },
+ {
+ "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": 36,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "X_train09, X_test09, y_train09, y_test09 = tts(X, y, test_size=0.1, train_size=0.9, 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": 37,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "LinearRegression()"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto_model09=LinReg()\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": 38,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "y_pred09=auto_model09.predict(X_test09)\n",
+ "\n"
+ ]
+ },
+ {
+ "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": 39,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.8363926533372654"
+ ]
+ },
+ "execution_count": 39,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "r2_score(y_pred09, y_test09)"
+ ]
+ },
+ {
+ "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": 52,
+ "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": 53,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Your code here:\n",
+ "selector = RFE(auto_model, n_features_to_select=3)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Fit the model and print the ranking"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 54,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "RFE(estimator=LinearRegression(), n_features_to_select=3)"
+ ]
+ },
+ "execution_count": 54,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "selector.fit(X_train, y_train)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 55,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[1 4 3 2 1 1]\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(selector.ranking_)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 56,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "Index(['cylinders', 'displacement', 'horse_power', 'weight', 'acceleration',\n",
+ " 'model_year'],\n",
+ " dtype='object')"
+ ]
+ },
+ "execution_count": 56,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "X_train.columns"
+ ]
+ },
+ {
+ "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": 61,
+ "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": 44,
+ "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..52f95ab 100755
--- a/your-code/main.ipynb
+++ b/your-code/main.ipynb
@@ -12,11 +12,27 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
- "# Import your libraries:\n"
+ "# Import your libraries:\n",
+ "\n",
+ "import pandas as pd\n",
+ "pd.set_option('display.max_columns', None)\n",
+ "import numpy as np\n",
+ "\n",
+ "import pylab as plt\n",
+ "import seaborn as sns\n",
+ "%matplotlib inline\n",
+ "from sklearn.model_selection import train_test_split as tts\n",
+ "\n",
+ "from sklearn.linear_model import LinearRegression as LinReg\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "\n",
+ "import warnings\n",
+ "warnings.simplefilter('ignore')"
]
},
{
@@ -37,11 +53,15 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "\n",
+ "from sklearn import datasets\n",
+ "\n",
+ "diabetesDataset = datasets.load_diabetes()\n"
]
},
{
@@ -53,11 +73,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 3,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "diabetesDataset.keys()"
]
},
{
@@ -73,13 +105,59 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 4,
"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(diabetesDataset.DESCR)"
]
},
{
@@ -97,11 +175,25 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Enter your answer here:\n"
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "((442, 10), (442,))"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Enter your answer here:\n",
+ "# 1: 10 2: las data son variables independientes que afectan a la var depen. 3: 442\n",
+ "\n",
+ "diabetesDataset['data'].shape, diabetesDataset['target'].shape"
]
},
{
@@ -115,11 +207,36 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "array([[ 0.03807591, 0.05068012, 0.06169621, ..., -0.00259226,\n",
+ " 0.01990842, -0.01764613],\n",
+ " [-0.00188202, -0.04464164, -0.05147406, ..., -0.03949338,\n",
+ " -0.06832974, -0.09220405],\n",
+ " [ 0.08529891, 0.05068012, 0.04445121, ..., -0.00259226,\n",
+ " 0.00286377, -0.02593034],\n",
+ " ...,\n",
+ " [ 0.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]])"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "diabetesDataset.data"
]
},
{
@@ -156,11 +273,12 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "from sklearn.linear_model import LinearRegression as LinReg"
]
},
{
@@ -172,11 +290,13 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "\n",
+ "diabetes_model= LinReg()"
]
},
{
@@ -190,11 +310,16 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "X=diabetesDataset.data\n",
+ "\n",
+ "y=diabetesDataset.target\n",
+ "\n",
+ "X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2, train_size=0.8, random_state=42)"
]
},
{
@@ -206,11 +331,47 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "151.3456553477407"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "diabetes_model.fit(X_train, y_train)\n",
+ "diabetes_model.intercept_"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "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": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "diabetes_model.coef_"
]
},
{
@@ -231,11 +392,15 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "from sklearn.metrics import confusion_matrix\n",
+ "from sklearn.metrics import accuracy_score\n",
+ "from sklearn.metrics import r2_score\n",
+ "y_pred=diabetes_model.predict(X_test)"
]
},
{
@@ -247,11 +412,58 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[139.5483133 179.52030578 134.04133298 291.41193598 123.78723656\n",
+ " 92.17357677 258.23409704 181.33895238 90.22217862 108.63143298\n",
+ " 94.13938654 168.43379636 53.50669663 206.63040068 100.13238561\n",
+ " 130.66881649 219.53270758 250.78291772 196.36682356 218.57497401\n",
+ " 207.35002447 88.48361667 70.43428801 188.95725301 154.88720039\n",
+ " 159.35957695 188.31587948 180.38835506 47.98988446 108.97514644\n",
+ " 174.78080029 86.36598906 132.95890535 184.5410226 173.83298051\n",
+ " 190.35863287 124.41740796 119.65426903 147.95402494 59.05311211\n",
+ " 71.62636914 107.68722902 165.45544477 155.00784964 171.04558668\n",
+ " 61.45763075 71.66975626 114.96330486 51.57808027 167.57781958\n",
+ " 152.52505798 62.95827693 103.49862017 109.20495627 175.63844013\n",
+ " 154.60247734 94.41476124 210.74244148 120.25601864 77.61590087\n",
+ " 187.93503183 206.49543321 140.63018684 105.59463059 130.704246\n",
+ " 202.18650868 171.1330116 164.91246096 124.72637597 144.81210187\n",
+ " 181.99631481 199.41234515 234.21402489 145.96053305 79.86349114\n",
+ " 157.36828831 192.74737754 208.8980067 158.58505486 206.0226849\n",
+ " 107.47978402 140.93428553 54.81856678 55.92807758 115.00974554\n",
+ " 78.95886675 81.55731377 54.3774778 166.25477778]\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "print(y_pred)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.45260660216173787"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "r2_score(y_test, y_pred)"
]
},
{
@@ -263,11 +475,13 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
- "# Your explanation here:\n"
+ "# Your explanation here:\n",
+ "# Este es un modelo malo.\n",
+ "# El R2 es 0.45, por lo que tenemos valores que el modelo no es capaz de predecir correctamente."
]
},
{
@@ -302,11 +516,67 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "\n",
+ "import statsmodels.api as sm"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ " OLS Regression Results \n",
+ "=======================================================================================\n",
+ "Dep. Variable: y R-squared (uncentered): 0.136\n",
+ "Model: OLS Adj. R-squared (uncentered): 0.110\n",
+ "Method: Least Squares F-statistic: 5.378\n",
+ "Date: Tue, 12 Oct 2021 Prob (F-statistic): 2.13e-07\n",
+ "Time: 21:20:23 Log-Likelihood: -2293.0\n",
+ "No. Observations: 353 AIC: 4606.\n",
+ "Df Residuals: 343 BIC: 4645.\n",
+ "Df Model: 10 \n",
+ "Covariance Type: nonrobust \n",
+ "==============================================================================\n",
+ " coef std err t P>|t| [0.025 0.975]\n",
+ "------------------------------------------------------------------------------\n",
+ "x1 125.2081 206.271 0.607 0.544 -280.507 530.924\n",
+ "x2 -258.2036 204.877 -1.260 0.208 -661.177 144.770\n",
+ "x3 627.4875 229.885 2.730 0.007 175.325 1079.650\n",
+ "x4 346.3186 213.207 1.624 0.105 -73.039 765.676\n",
+ "x5 -803.2240 1347.949 -0.596 0.552 -3454.510 1848.062\n",
+ "x6 312.8181 1087.888 0.288 0.774 -1826.953 2452.589\n",
+ "x7 45.0676 696.193 0.065 0.948 -1324.277 1414.412\n",
+ "x8 211.9779 553.944 0.383 0.702 -877.577 1301.532\n",
+ "x9 700.8248 574.988 1.219 0.224 -430.122 1831.771\n",
+ "x10 144.6897 219.349 0.660 0.510 -286.749 576.128\n",
+ "==============================================================================\n",
+ "Omnibus: 0.382 Durbin-Watson: 0.209\n",
+ "Prob(Omnibus): 0.826 Jarque-Bera (JB): 0.510\n",
+ "Skew: 0.033 Prob(JB): 0.775\n",
+ "Kurtosis: 2.826 Cond. No. 20.8\n",
+ "==============================================================================\n",
+ "\n",
+ "Notes:\n",
+ "[1] R² is computed without centering (uncentered) since the model does not contain a constant.\n",
+ "[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
+ ]
+ }
+ ],
+ "source": [
+ "mod = sm.OLS(y_train, X_train)\n",
+ "\n",
+ "res = mod.fit()\n",
+ "\n",
+ "print(res.summary())"
]
},
{
@@ -326,11 +596,15 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
- "# Your answers here:"
+ "# Your answers here:\n",
+ "\n",
+ "#Rechaz la H0 de que todas son significativas\n",
+ "#Todas menos la masa corporal\n",
+ "#eliminando las columnas con peor significatividad"
]
},
{
@@ -351,11 +625,12 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "auto = pd.read_csv('../auto-mpg.csv')"
]
},
{
@@ -367,11 +642,124 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " mpg | \n",
+ " cylinders | \n",
+ " displacement | \n",
+ " horse_power | \n",
+ " weight | \n",
+ " acceleration | \n",
+ " model_year | \n",
+ " car_name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 307.0 | \n",
+ " 130.0 | \n",
+ " 3504 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"chevrolet chevelle malibu\" | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 15.0 | \n",
+ " 8 | \n",
+ " 350.0 | \n",
+ " 165.0 | \n",
+ " 3693 | \n",
+ " 11.5 | \n",
+ " 70 | \n",
+ " \\t\"buick skylark 320\" | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 318.0 | \n",
+ " 150.0 | \n",
+ " 3436 | \n",
+ " 11.0 | \n",
+ " 70 | \n",
+ " \\t\"plymouth satellite\" | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 16.0 | \n",
+ " 8 | \n",
+ " 304.0 | \n",
+ " 150.0 | \n",
+ " 3433 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"amc rebel sst\" | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 17.0 | \n",
+ " 8 | \n",
+ " 302.0 | \n",
+ " 140.0 | \n",
+ " 3449 | \n",
+ " 10.5 | \n",
+ " 70 | \n",
+ " \\t\"ford torino\" | \n",
+ "
\n",
+ " \n",
+ "
\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": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.head()"
]
},
{
@@ -383,11 +771,31 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 21,
+ "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": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.dtypes"
]
},
{
@@ -399,11 +807,50 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "mpg 28.0\n",
+ "cylinders 4\n",
+ "displacement 112.0\n",
+ "horse_power 88.0\n",
+ "weight 2605\n",
+ "acceleration 19.6\n",
+ "model_year 82\n",
+ "car_name \\t\"chevrolet cavalier\"\n",
+ "Name: 367, dtype: object"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.loc[auto.model_year.argmax()]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "El modelo mas nuevo es 82\n",
+ "El modelo mas viejo es 70\n"
+ ]
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "print(f'El modelo mas nuevo es {auto[\"model_year\"].max()}')\n",
+ "print(f'El modelo mas viejo es {auto[\"model_year\"].min()}')"
]
},
{
@@ -415,11 +862,81 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 24,
+ "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": 24,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.isnull().sum()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(398, 8)"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "\n",
+ "auto.shape"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "auto = auto.dropna()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 27,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "(392, 8)"
+ ]
+ },
+ "execution_count": 27,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "auto.shape"
]
},
{
@@ -431,11 +948,28 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 28,
+ "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": 28,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "auto.cylinders.value_counts()"
]
},
{
@@ -451,11 +985,239 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 29,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/html": [
+ "\n",
+ "\n",
+ "
\n",
+ " \n",
+ " \n",
+ " | \n",
+ " mpg | \n",
+ " cylinders | \n",
+ " displacement | \n",
+ " horse_power | \n",
+ " weight | \n",
+ " acceleration | \n",
+ " model_year | \n",
+ " car_name | \n",
+ "
\n",
+ " \n",
+ " \n",
+ " \n",
+ " | 0 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 307.0 | \n",
+ " 130.0 | \n",
+ " 3504 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"chevrolet chevelle malibu\" | \n",
+ "
\n",
+ " \n",
+ " | 1 | \n",
+ " 15.0 | \n",
+ " 8 | \n",
+ " 350.0 | \n",
+ " 165.0 | \n",
+ " 3693 | \n",
+ " 11.5 | \n",
+ " 70 | \n",
+ " \\t\"buick skylark 320\" | \n",
+ "
\n",
+ " \n",
+ " | 2 | \n",
+ " 18.0 | \n",
+ " 8 | \n",
+ " 318.0 | \n",
+ " 150.0 | \n",
+ " 3436 | \n",
+ " 11.0 | \n",
+ " 70 | \n",
+ " \\t\"plymouth satellite\" | \n",
+ "
\n",
+ " \n",
+ " | 3 | \n",
+ " 16.0 | \n",
+ " 8 | \n",
+ " 304.0 | \n",
+ " 150.0 | \n",
+ " 3433 | \n",
+ " 12.0 | \n",
+ " 70 | \n",
+ " \\t\"amc rebel sst\" | \n",
+ "
\n",
+ " \n",
+ " | 4 | \n",
+ " 17.0 | \n",
+ " 8 | \n",
+ " 302.0 | \n",
+ " 140.0 | \n",
+ " 3449 | \n",
+ " 10.5 | \n",
+ " 70 | \n",
+ " \\t\"ford torino\" | \n",
+ "
\n",
+ " \n",
+ " | ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ " ... | \n",
+ "
\n",
+ " \n",
+ " | 393 | \n",
+ " 27.0 | \n",
+ " 4 | \n",
+ " 140.0 | \n",
+ " 86.0 | \n",
+ " 2790 | \n",
+ " 15.6 | \n",
+ " 82 | \n",
+ " \\t\"ford mustang gl\" | \n",
+ "
\n",
+ " \n",
+ " | 394 | \n",
+ " 44.0 | \n",
+ " 4 | \n",
+ " 97.0 | \n",
+ " 52.0 | \n",
+ " 2130 | \n",
+ " 24.6 | \n",
+ " 82 | \n",
+ " \\t\"vw pickup\" | \n",
+ "
\n",
+ " \n",
+ " | 395 | \n",
+ " 32.0 | \n",
+ " 4 | \n",
+ " 135.0 | \n",
+ " 84.0 | \n",
+ " 2295 | \n",
+ " 11.6 | \n",
+ " 82 | \n",
+ " \\t\"dodge rampage\" | \n",
+ "
\n",
+ " \n",
+ " | 396 | \n",
+ " 28.0 | \n",
+ " 4 | \n",
+ " 120.0 | \n",
+ " 79.0 | \n",
+ " 2625 | \n",
+ " 18.6 | \n",
+ " 82 | \n",
+ " \\t\"ford ranger\" | \n",
+ "
\n",
+ " \n",
+ " | 397 | \n",
+ " 31.0 | \n",
+ " 4 | \n",
+ " 119.0 | \n",
+ " 82.0 | \n",
+ " 2720 | \n",
+ " 19.4 | \n",
+ " 82 | \n",
+ " \\t\"chevy s-10\" | \n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
392 rows × 8 columns
\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",
+ "393 27.0 4 140.0 86.0 2790 15.6 \n",
+ "394 44.0 4 97.0 52.0 2130 24.6 \n",
+ "395 32.0 4 135.0 84.0 2295 11.6 \n",
+ "396 28.0 4 120.0 79.0 2625 18.6 \n",
+ "397 31.0 4 119.0 82.0 2720 19.4 \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\" \n",
+ ".. ... ... \n",
+ "393 82 \\t\"ford mustang gl\" \n",
+ "394 82 \\t\"vw pickup\" \n",
+ "395 82 \\t\"dodge rampage\" \n",
+ "396 82 \\t\"ford ranger\" \n",
+ "397 82 \\t\"chevy s-10\" \n",
+ "\n",
+ "[392 rows x 8 columns]"
+ ]
+ },
+ "execution_count": 29,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "auto"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "X=auto.drop(columns=['mpg', 'car_name'])._get_numeric_data()\n",
+ "\n",
+ "y=auto.mpg\n",
+ "\n",
+ "X_train, X_test, y_train, y_test = tts(X, y, test_size=0.2, train_size=0.8, random_state=42)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 31,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "((313, 6), (313,), (79, 6), (79,))"
+ ]
+ },
+ "execution_count": 31,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "X_train.shape, y_train.shape, X_test.shape, y_test.shape"
]
},
{
@@ -469,11 +1231,37 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "\n",
+ "from sklearn.linear_model import LinearRegression as LinReg\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "\n",
+ "auto_model=LinReg()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 33,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "LinearRegression()"
+ ]
+ },
+ "execution_count": 33,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "auto_model.fit(X_train, y_train)"
]
},
{
@@ -502,11 +1290,26 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.7665327707747775"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "from sklearn.metrics import r2_score\n",
+ "y_pred=auto_model.predict(X_train)\n",
+ "r2_score(y_pred, y_train)"
]
},
{
@@ -522,11 +1325,26 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
- "# Your code here:\n"
+ "execution_count": 35,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.8037468831261811"
+ ]
+ },
+ "execution_count": 35,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Your code here:\n",
+ "\n",
+ "\n",
+ "y_test_pred=auto_model.predict(X_test)\n",
+ "r2_score(y_test_pred, y_test)"
]
},
{
@@ -551,11 +1369,12 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "X_train09, X_test09, y_train09, y_test09 = tts(X, y, test_size=0.1, train_size=0.9, random_state=42)"
]
},
{
@@ -567,11 +1386,24 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 37,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "LinearRegression()"
+ ]
+ },
+ "execution_count": 37,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "auto_model09=LinReg()\n",
+ "auto_model09.fit(X_train09, y_train09)"
]
},
{
@@ -583,11 +1415,14 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "\n",
+ "y_pred09=auto_model09.predict(X_test09)\n",
+ "\n"
]
},
{
@@ -599,11 +1434,23 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 39,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0.8363926533372654"
+ ]
+ },
+ "execution_count": 39,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "r2_score(y_pred09, y_test09)"
]
},
{
@@ -619,7 +1466,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
@@ -635,11 +1482,12 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "selector = RFE(auto_model, n_features_to_select=3)"
]
},
{
@@ -651,11 +1499,62 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 54,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "RFE(estimator=LinearRegression(), n_features_to_select=3)"
+ ]
+ },
+ "execution_count": 54,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
"source": [
- "# Your code here:\n"
+ "# Your code here:\n",
+ "selector.fit(X_train, y_train)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 55,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[1 4 3 2 1 1]\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(selector.ranking_)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 56,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "Index(['cylinders', 'displacement', 'horse_power', 'weight', 'acceleration',\n",
+ " 'model_year'],\n",
+ " dtype='object')"
+ ]
+ },
+ "execution_count": 56,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "X_train.columns"
]
},
{
@@ -669,7 +1568,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
@@ -685,7 +1584,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
@@ -712,7 +1611,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "Python 3",
+ "display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -726,7 +1625,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.6.6"
+ "version": "3.9.6"
}
},
"nbformat": 4,