From 390aeba209b2fbf70a562623bd178c9ecc652e2a Mon Sep 17 00:00:00 2001 From: Samuel Simeone Date: Mon, 11 Oct 2021 22:00:22 -0400 Subject: [PATCH] lab_supervised_sklearn [Samuel Simeone] --- .../.ipynb_checkpoints/main-checkpoint.ipynb | 1499 +++++++++++++++++ your-code/main.ipynb | 983 +++++++++-- 2 files changed, 2373 insertions(+), 109 deletions(-) create mode 100644 your-code/.ipynb_checkpoints/main-checkpoint.ipynb diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..a63c02b --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,1499 @@ +{ + "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": 64, + "metadata": {}, + "outputs": [], + "source": [ + "# Import your libraries:\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "\n", + "from sklearn.linear_model import LinearRegression\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import r2_score" + ] + }, + { + "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": 41, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\n", + "from sklearn import datasets\n", + "diabetes = datasets.load_diabetes(as_frame=True)" + ] + }, + { + "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": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "\n", + "diabetes.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": 43, + "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, T-Cells (a type of white blood cells)\n", + " - s2 ldl, low-density lipoproteins\n", + " - s3 hdl, high-density lipoproteins\n", + " - s4 tch, thyroid stimulating hormone\n", + " - s5 ltg, lamotrigine\n", + " - s6 glu, blood sugar level\n", + "\n", + "Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\n", + "\n", + "Source URL:\n", + "https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\n", + "\n", + "For more information see:\n", + "Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\n", + "(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(diabetes['DESCR'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Based on the data description, answer the following questions:\n", + "\n", + "1. How many attributes are there in the data? What do they mean?\n", + "\n", + "1. What is the relation between `diabetes['data']` and `diabetes['target']`?\n", + "\n", + "1. How many records are there in the data?" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((442, 10), (442,))" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Enter your answer here:\n", + "diabetes['data'].shape, diabetes['target'].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "# Tenemos 10 columnas en nuestros datos. Todas las columnas tienen datos numericos.\n", + "\n", + "# Tenemos la edad del paciente, asi como el sexo. La masa corporal, precion sanguinea y seis mediciones de suero sanguíneo.\n", + "# En la data tenemos un total de 10 columnas y 442 registros.\n", + "\n", + "# La diferencia entre data y target es que la data contiene los datos y el target es el objetivo de regresión, es decir,\n", + "# la variable que queremos predecir. Representa la medida cuantitativa de la progresión de la enfermedad un año después\n", + "# del inicio" + ] + }, + { + "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": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((442, 10), (442,))" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes['data'].shape, diabetes['target'].shape" + ] + }, + { + "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": 49, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "from sklearn.linear_model import LinearRegression" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Create a new instance of the linear regression model and assign the new instance to the variable `diabetes_model`." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "diabetes_model = LinearRegression()" + ] + }, + { + "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": 56, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "x = diabetes['data']\n", + "y = diabetes['target']\n", + "\n", + "diabetes_data_train = x[:-20]\n", + "diabetes_target_train = y[:-20]\n", + "diabetes_data_test = x[-20:]\n", + "diabetes_target_test = y[-20:]" + ] + }, + { + "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": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3.03499549e-01, -2.37639315e+02, 5.10530605e+02, 3.27736980e+02,\n", + " -8.14131709e+02, 4.92814588e+02, 1.02848452e+02, 1.84606489e+02,\n", + " 7.43519617e+02, 7.60951722e+01])" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes_model.fit(diabetes_data_train, diabetes_target_train)\n", + "diabetes_model.coef_" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "152.76430691633442" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes_model.intercept_" + ] + }, + { + "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": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([197.61846908, 155.43979328, 172.88665147, 111.53537279,\n", + " 164.80054784, 131.06954875, 259.12237761, 100.47935157,\n", + " 117.0601052 , 124.30503555, 218.36632793, 61.19831284,\n", + " 132.25046751, 120.3332925 , 52.54458691, 194.03798088,\n", + " 102.57139702, 123.56604987, 211.0346317 , 52.60335674])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred = diabetes_model.predict(diabetes_data_test)\n", + "y_pred" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Print your `diabetes_target_test` and compare with the prediction. " + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "422 233.0\n", + "423 91.0\n", + "424 111.0\n", + "425 152.0\n", + "426 120.0\n", + "427 67.0\n", + "428 310.0\n", + "429 94.0\n", + "430 183.0\n", + "431 66.0\n", + "432 173.0\n", + "433 72.0\n", + "434 49.0\n", + "435 64.0\n", + "436 48.0\n", + "437 178.0\n", + "438 104.0\n", + "439 132.0\n", + "440 220.0\n", + "441 57.0\n", + "Name: target, dtype: float64" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes_target_test" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.5850753022690572" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "r2_score(diabetes_target_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": 78, + "metadata": {}, + "outputs": [], + "source": [ + "# Your explanation here:\n", + "# No, no es igual y eso se debe a que el modelo no es capaz de predecir correctamente el 100% de las variables. Vemos\n", + "# que tenemos un R2 del 0.58, por lo que tenemos valores que el modelo no es capaz de predecir correctamente.\n", + "\n", + "# Este es un modelo malo. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus Challenge 1 - Hypothesis Testing with `statsmodels`\n", + "\n", + "After generating the linear regression model from the dataset, you probably wonder: then what? What is the statistical way to know if my model is reliable or not?\n", + "\n", + "Good question. We'll discuss that using Scikit-Learn in Challenge 5. But for now, let's use a fool-proof way by using the ([Linear Regression class of StatsModels](https://www.statsmodels.org/dev/regression.html)) which can also conduct linear regression analysis plus much more such as calcuating the F-score of the linear model as well as the standard errors and t-scores for each coefficient. The F-score and t-scores will tell you whether you can trust your linear model.\n", + "\n", + "To understand the statistical meaning of conducting hypothesis testing (e.g. F-test, t-test) for slopes, read [this webpage](https://onlinecourses.science.psu.edu/stat501/node/297/) at your leisure time. We'll give you a brief overview next.\n", + "\n", + "* The F-test of your linear model is to verify whether at least one of your coefficients is significantly different from zero. Translating that into the *null hypothesis* and *alternative hypothesis*, that is:\n", + "\n", + " ```\n", + " H0 : β1 = β2 = ... = β10 = 0\n", + " HA : At least one βj ≠ 0 (for j = 1, 2, ..., 10)\n", + " ```\n", + "\n", + "* The t-tests on each coefficient is to check whether the confidence interval for the variable contains zero. If the confidence interval contains zero, it means the null hypothesis for that variable is not rejected. In other words, this particular vaiable is not contributing to your linear model and you can remove it from your formula.\n", + "\n", + "Read the documentations of [StatsModels Linear Regression](https://www.statsmodels.org/dev/regression.html) as well as its [`OLS` class](https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.html) which stands for *ordinary least squares*.\n", + "\n", + "#### In the next cell, analyze `diabetes_data_train` and `diabetes_target_train` with the linear regression model of `statsmodels`. Print the fit summary.\n", + "\n", + "Your output should look like:\n", + "\n", + "![statsmodels regression](../statsmodels.png)" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
OLS Regression Results
Dep. Variable: target R-squared: 0.512
Model: OLS Adj. R-squared: 0.500
Method: Least Squares F-statistic: 43.16
Date: Mon, 11 Oct 2021 Prob (F-statistic): 4.64e-58
Time: 14:24:41 Log-Likelihood: -2281.1
No. Observations: 422 AIC: 4584.
Df Residuals: 411 BIC: 4629.
Df Model: 10
Covariance Type: nonrobust
\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
coef std err t P>|t| [0.025 0.975]
const 152.7643 2.658 57.469 0.000 147.539 157.990
age 0.3035 61.286 0.005 0.996 -120.169 120.776
sex -237.6393 62.837 -3.782 0.000 -361.162 -114.117
bmi 510.5306 68.156 7.491 0.000 376.553 644.508
bp 327.7370 66.876 4.901 0.000 196.275 459.199
s1 -814.1317 424.044 -1.920 0.056 -1647.697 19.434
s2 492.8146 344.227 1.432 0.153 -183.850 1169.480
s3 102.8485 219.463 0.469 0.640 -328.561 534.258
s4 184.6065 167.336 1.103 0.271 -144.334 513.547
s5 743.5196 175.359 4.240 0.000 398.807 1088.232
s6 76.0952 68.293 1.114 0.266 -58.152 210.343
\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
Omnibus: 1.544 Durbin-Watson: 2.026
Prob(Omnibus): 0.462 Jarque-Bera (JB): 1.421
Skew: 0.004 Prob(JB): 0.491
Kurtosis: 2.716 Cond. No. 224.


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." + ], + "text/plain": [ + "\n", + "\"\"\"\n", + " OLS Regression Results \n", + "==============================================================================\n", + "Dep. Variable: target R-squared: 0.512\n", + "Model: OLS Adj. R-squared: 0.500\n", + "Method: Least Squares F-statistic: 43.16\n", + "Date: Mon, 11 Oct 2021 Prob (F-statistic): 4.64e-58\n", + "Time: 14:24:41 Log-Likelihood: -2281.1\n", + "No. Observations: 422 AIC: 4584.\n", + "Df Residuals: 411 BIC: 4629.\n", + "Df Model: 10 \n", + "Covariance Type: nonrobust \n", + "==============================================================================\n", + " coef std err t P>|t| [0.025 0.975]\n", + "------------------------------------------------------------------------------\n", + "const 152.7643 2.658 57.469 0.000 147.539 157.990\n", + "age 0.3035 61.286 0.005 0.996 -120.169 120.776\n", + "sex -237.6393 62.837 -3.782 0.000 -361.162 -114.117\n", + "bmi 510.5306 68.156 7.491 0.000 376.553 644.508\n", + "bp 327.7370 66.876 4.901 0.000 196.275 459.199\n", + "s1 -814.1317 424.044 -1.920 0.056 -1647.697 19.434\n", + "s2 492.8146 344.227 1.432 0.153 -183.850 1169.480\n", + "s3 102.8485 219.463 0.469 0.640 -328.561 534.258\n", + "s4 184.6065 167.336 1.103 0.271 -144.334 513.547\n", + "s5 743.5196 175.359 4.240 0.000 398.807 1088.232\n", + "s6 76.0952 68.293 1.114 0.266 -58.152 210.343\n", + "==============================================================================\n", + "Omnibus: 1.544 Durbin-Watson: 2.026\n", + "Prob(Omnibus): 0.462 Jarque-Bera (JB): 1.421\n", + "Skew: 0.004 Prob(JB): 0.491\n", + "Kurtosis: 2.716 Cond. No. 224.\n", + "==============================================================================\n", + "\n", + "Notes:\n", + "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", + "\"\"\"" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "import statsmodels.api as sm\n", + "\n", + "X = sm.add_constant(diabetes_data_train)\n", + "ols_model= sm.OLS(diabetes_target_train, X).fit()\n", + "ols_model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4.643057835649836e-58" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ols_model.f_pvalue" + ] + }, + { + "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": 85, + "metadata": {}, + "outputs": [], + "source": [ + "# Your answers here:\n", + "\n", + "# Podemos ver que el valor del p-value para el F-test fue de 4.643057835649836e-58, como es menor a 0.05 podemos rechazar\n", + "# la hipotesis nula. Por lo tanto, por lo menos uno de nuestros coeficientes es significativamente distinto de 0.\n", + "\n", + "# Para ver que variables no aportan nada a nuestro modelo, observamos los p-value. Podemos ver que las columnas de \n", + "# age, s1, s2, s3, s4 y s6 tienen un p-value mayor a 0.05, por lo que la hipotesis nula se cumple, es decir,\n", + "# no nos aportan nada a nuestro model. Podemos ver para cada uno de las variables el 0 se encuentra dentro de su intervalo\n", + "# de confianza.\n", + "\n", + "# De acuerdo a los analisis realizados, se deben eliminar del modelo las columnas ya mencionadas, ya que no aportan\n", + "# nada a nuestro modelo. Pero esto seguira proporcionando el mismo r2, es deci, nuestro modelo no mejorara por hacer \n", + "# esto." + ] + }, + { + "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": 86, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "auto = pd.read_csv(r'../auto-mpg.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Look at the first 5 rows using the `head()` function:" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mpgcylindersdisplacementhorse_powerweightaccelerationmodel_yearcar_name
018.08307.0130.0350412.070\\t\"chevrolet chevelle malibu\"
115.08350.0165.0369311.570\\t\"buick skylark 320\"
218.08318.0150.0343611.070\\t\"plymouth satellite\"
316.08304.0150.0343312.070\\t\"amc rebel sst\"
417.08302.0140.0344910.570\\t\"ford torino\"
\n", + "
" + ], + "text/plain": [ + " mpg cylinders displacement horse_power weight acceleration \\\n", + "0 18.0 8 307.0 130.0 3504 12.0 \n", + "1 15.0 8 350.0 165.0 3693 11.5 \n", + "2 18.0 8 318.0 150.0 3436 11.0 \n", + "3 16.0 8 304.0 150.0 3433 12.0 \n", + "4 17.0 8 302.0 140.0 3449 10.5 \n", + "\n", + " model_year car_name \n", + "0 70 \\t\"chevrolet chevelle malibu\" \n", + "1 70 \\t\"buick skylark 320\" \n", + "2 70 \\t\"plymouth satellite\" \n", + "3 70 \\t\"amc rebel sst\" \n", + "4 70 \\t\"ford torino\" " + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.head(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Evaluate the data to ensure that all numeric columns are correctly detected as such by pandas. If a column is misclassified as object, coerce it to numeric." + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 398 entries, 0 to 397\n", + "Data columns (total 8 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 mpg 398 non-null float64\n", + " 1 cylinders 398 non-null int64 \n", + " 2 displacement 398 non-null float64\n", + " 3 horse_power 392 non-null float64\n", + " 4 weight 398 non-null int64 \n", + " 5 acceleration 398 non-null float64\n", + " 6 model_year 398 non-null int64 \n", + " 7 car_name 398 non-null object \n", + "dtypes: float64(4), int64(3), object(1)\n", + "memory usage: 25.0+ KB\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "auto.info()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What is the newest model year and the oldest model year?" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "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()}')" + ] + }, + { + "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": 92, + "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": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.isnull().sum()\n", + "\n", + "# Tenemos valores nulos en horse power. Los eliminamos" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mpg 0\n", + "cylinders 0\n", + "displacement 0\n", + "horse_power 0\n", + "weight 0\n", + "acceleration 0\n", + "model_year 0\n", + "car_name 0\n", + "dtype: int64" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.dropna(inplace=True)\n", + "auto.isnull().sum()" + ] + }, + { + "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": 102, + "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": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto['cylinders'].value_counts()\n", + "\n", + "# Tenemos 5 valores posibles de cilindros" + ] + }, + { + "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": 103, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "x = auto.drop(columns=['mpg', 'car_name'])\n", + "y = auto['mpg']\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we will processed and peform linear regression on this data to predict the mpg for each vehicle. \n", + "\n", + "#### In the next cell, create an instance of the linear regression model and call it `auto_model`. Fit `auto_model` with your training data." + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.794234907542859\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "auto_model = LinearRegression()\n", + "auto_model.fit(X_train, y_train)\n", + "\n", + "y_auto_pred = auto_model.predict(X_test)\n", + "print(r2_score(y_test, y_auto_pred))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 4 - Evaluate the Model\n", + "\n", + "In addition to evaluating your model with F-test and t-test, you can also use the *Coefficient of Determination* (a.k.a. *r squared score*). This method does not simply tell *yes* or *no* about the model fit but instead indicates how much variation can be explained by the model. Based on the r squared score, you can decide whether to improve your model in order to obtain a better fit.\n", + "\n", + "You can learn about the r squared score [here](). Its formula is:\n", + "\n", + "![R Squared](../r-squared.png)\n", + "\n", + "...where:\n", + "\n", + "* yi is an actual data point.\n", + "* ŷi is the corresponding data point on the estimated regression line.\n", + "\n", + "By adding the squares of the difference between all yi-ŷi pairs, we have a measure called SSE (*error sum of squares*) which is an application of the r squared score to indicate the extent to which the estimated regression model is different from the actual data. And we attribute that difference to the random error that is unavoidable in the real world. Obviously, we want the SSE value to be as small as possible.\n", + "\n", + "#### In the next cell, compute the predicted *y* based on `X_train` and call it `y_pred`. Then calcualte the r squared score between `y_pred` and `y_train` which indicates how well the estimated regression model fits the training data.\n", + "\n", + "*Hint: r squared score can be calculated using `sklearn.metrics.r2_score` ([documentation](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html)).*" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.8107227953093896\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "y_pred = auto_model.predict(X_train)\n", + "print(r2_score(y_train, y_pred))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Our next step is to evaluate the model using the test data. \n", + "\n", + "We would like to ensure that our model is not overfitting the data. This means that our model was made to fit too closely to the training data by being overly complex. If a model is overfitted, it is not generalizable to data outside the training data. In that case, we need to reduce the complexity of the model by removing certain features (variables).\n", + "\n", + "In the cell below, use the model to generate the predicted values for the test data and assign them to `y_test_pred`. Compute the r squared score of the predicted `y_test_pred` and the oberserved `y_test` data." + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.794234907542859" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_test_pred = auto_model.predict(X_test)\n", + "r2_score(y_test, y_test_pred)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Explaining the results\n", + "\n", + "The r squared scores of the training data and the test data are pretty close (0.8146 vs 0.7818). This means our model is not overfitted. However, there is still room to improve the model fit. Move on to the next challenge." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge 5 - Improve the Model Fit\n", + "\n", + "While the most common way to improve the fit of a model is by using [regularization](https://datanice.github.io/machine-learning-101-what-is-regularization-interactive.html), there are other simpler ways to improve model fit. The first is to create a simpler model. The second is to increase the train sample size.\n", + "\n", + "Let us start with the easier option and increase our train sample size to 90% of the data. Create a new test train split and name the new predictors and response variables `X_train09`, `X_test09`, `y_train09`, `y_test09`." + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "X_train09, X_test09, y_train09, y_test09 = train_test_split(x, y, test_size=0.1, random_state=42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Initialize a new linear regression model. Name this model `auto_model09`. Fit the model to the new sample (training) data." + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto_model09 = LinearRegression()\n", + "auto_model09.fit(X_train09, y_train09)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Compute the predicted values and r squared score for our new model and new sample data." + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El r2 para la data de train es de 0.8047940166959004\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "y_pred_train_09 = auto_model09.predict(X_train09)\n", + "y_pred_test_09 = auto_model09.predict(X_test09)\n", + "\n", + "print(f'El r2 para la data de train es de {r2_score(y_train09, y_pred_train_09)}')" + ] + }, + { + "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": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El r2 para la data de test es de 0.8468911998183243\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(f'El r2 para la data de test es de {r2_score(y_test09, y_pred_test_09)}')\n", + "\n", + "# Vemos como nuestro r2 mejoro con esta nueva division de las muestras." + ] + }, + { + "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": 114, + "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": 115, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "\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": 116, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "RFE(estimator=LinearRegression(), n_features_to_select=3)" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "selector.fit(X_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 4 3 2 1 1]\n" + ] + } + ], + "source": [ + "print(selector.ranking_)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['cylinders', 'displacement', 'horse_power', 'weight', 'acceleration',\n", + " 'model_year'],\n", + " dtype='object')" + ] + }, + "execution_count": 118, + "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": 122, + "metadata": {}, + "outputs": [], + "source": [ + "# Your code here:\n", + "x_reduced = x[['cylinders', 'acceleration', 'model_year']]\n", + "X_train_reduced, X_test_reduced, y_train_reduced, y_test_reduced = train_test_split(x_reduced, y,\n", + " test_size=0.2,\n", + " random_state=42)" + ] + }, + { + "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": 123, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7144839092209849" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here: \n", + "auto_model_reduced = LinearRegression()\n", + "auto_model_reduced.fit(X_train_reduced, y_train_reduced)\n", + "\n", + "y_pred_reduced = auto_model_reduced.predict(X_train_reduced)\n", + "r2_score(y_train_reduced, y_pred_reduced)" + ] + }, + { + "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", + "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.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0102ef9..a63c02b 100755 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -12,11 +12,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, "outputs": [], "source": [ - "# Import your libraries:\n" + "# Import your libraries:\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "\n", + "from sklearn.linear_model import LinearRegression\n", + "from sklearn.model_selection import train_test_split\n", + "from sklearn.metrics import r2_score" ] }, { @@ -37,11 +47,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "from sklearn import datasets\n", + "diabetes = datasets.load_diabetes(as_frame=True)" ] }, { @@ -53,11 +66,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['data', 'target', 'frame', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "\n", + "diabetes.keys()" ] }, { @@ -73,13 +99,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "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, T-Cells (a type of white blood cells)\n", + " - s2 ldl, low-density lipoproteins\n", + " - s3 hdl, high-density lipoproteins\n", + " - s4 tch, thyroid stimulating hormone\n", + " - s5 ltg, lamotrigine\n", + " - s6 glu, blood sugar level\n", + "\n", + "Note: Each of these 10 feature variables have been mean centered and scaled by the standard deviation times `n_samples` (i.e. the sum of squares of each column totals 1).\n", + "\n", + "Source URL:\n", + "https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html\n", + "\n", + "For more information see:\n", + "Bradley Efron, Trevor Hastie, Iain Johnstone and Robert Tibshirani (2004) \"Least Angle Regression,\" Annals of Statistics (with discussion), 407-499.\n", + "(https://web.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf)\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(diabetes['DESCR'])" ] }, { @@ -97,11 +169,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((442, 10), (442,))" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Enter your answer here:\n", + "diabetes['data'].shape, diabetes['target'].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ - "# Enter your answer here:\n" + "# Tenemos 10 columnas en nuestros datos. Todas las columnas tienen datos numericos.\n", + "\n", + "# Tenemos la edad del paciente, asi como el sexo. La masa corporal, precion sanguinea y seis mediciones de suero sanguíneo.\n", + "# En la data tenemos un total de 10 columnas y 442 registros.\n", + "\n", + "# La diferencia entre data y target es que la data contiene los datos y el target es el objetivo de regresión, es decir,\n", + "# la variable que queremos predecir. Representa la medida cuantitativa de la progresión de la enfermedad un año después\n", + "# del inicio" ] }, { @@ -115,11 +215,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "((442, 10), (442,))" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "diabetes['data'].shape, diabetes['target'].shape" ] }, { @@ -156,11 +268,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "from sklearn.linear_model import LinearRegression" ] }, { @@ -172,11 +285,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "diabetes_model = LinearRegression()" ] }, { @@ -190,11 +304,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "x = diabetes['data']\n", + "y = diabetes['target']\n", + "\n", + "diabetes_data_train = x[:-20]\n", + "diabetes_target_train = y[:-20]\n", + "diabetes_data_test = x[-20:]\n", + "diabetes_target_test = y[-20:]" ] }, { @@ -206,11 +327,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3.03499549e-01, -2.37639315e+02, 5.10530605e+02, 3.27736980e+02,\n", + " -8.14131709e+02, 4.92814588e+02, 1.02848452e+02, 1.84606489e+02,\n", + " 7.43519617e+02, 7.60951722e+01])" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "diabetes_model.fit(diabetes_data_train, diabetes_target_train)\n", + "diabetes_model.coef_" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "152.76430691633442" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "diabetes_model.intercept_" ] }, { @@ -231,11 +387,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([197.61846908, 155.43979328, 172.88665147, 111.53537279,\n", + " 164.80054784, 131.06954875, 259.12237761, 100.47935157,\n", + " 117.0601052 , 124.30503555, 218.36632793, 61.19831284,\n", + " 132.25046751, 120.3332925 , 52.54458691, 194.03798088,\n", + " 102.57139702, 123.56604987, 211.0346317 , 52.60335674])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "y_pred = diabetes_model.predict(diabetes_data_test)\n", + "y_pred" ] }, { @@ -247,11 +420,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "422 233.0\n", + "423 91.0\n", + "424 111.0\n", + "425 152.0\n", + "426 120.0\n", + "427 67.0\n", + "428 310.0\n", + "429 94.0\n", + "430 183.0\n", + "431 66.0\n", + "432 173.0\n", + "433 72.0\n", + "434 49.0\n", + "435 64.0\n", + "436 48.0\n", + "437 178.0\n", + "438 104.0\n", + "439 132.0\n", + "440 220.0\n", + "441 57.0\n", + "Name: target, dtype: float64" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "diabetes_target_test" + ] + }, + { + "cell_type": "code", + "execution_count": 77, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.5850753022690572" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "r2_score(diabetes_target_test, y_pred)" ] }, { @@ -263,11 +488,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 78, "metadata": {}, "outputs": [], "source": [ - "# Your explanation here:\n" + "# Your explanation here:\n", + "# No, no es igual y eso se debe a que el modelo no es capaz de predecir correctamente el 100% de las variables. Vemos\n", + "# que tenemos un R2 del 0.58, por lo que tenemos valores que el modelo no es capaz de predecir correctamente.\n", + "\n", + "# Este es un modelo malo. " ] }, { @@ -302,11 +531,167 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
OLS Regression Results
Dep. Variable: target R-squared: 0.512
Model: OLS Adj. R-squared: 0.500
Method: Least Squares F-statistic: 43.16
Date: Mon, 11 Oct 2021 Prob (F-statistic): 4.64e-58
Time: 14:24:41 Log-Likelihood: -2281.1
No. Observations: 422 AIC: 4584.
Df Residuals: 411 BIC: 4629.
Df Model: 10
Covariance Type: nonrobust
\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
coef std err t P>|t| [0.025 0.975]
const 152.7643 2.658 57.469 0.000 147.539 157.990
age 0.3035 61.286 0.005 0.996 -120.169 120.776
sex -237.6393 62.837 -3.782 0.000 -361.162 -114.117
bmi 510.5306 68.156 7.491 0.000 376.553 644.508
bp 327.7370 66.876 4.901 0.000 196.275 459.199
s1 -814.1317 424.044 -1.920 0.056 -1647.697 19.434
s2 492.8146 344.227 1.432 0.153 -183.850 1169.480
s3 102.8485 219.463 0.469 0.640 -328.561 534.258
s4 184.6065 167.336 1.103 0.271 -144.334 513.547
s5 743.5196 175.359 4.240 0.000 398.807 1088.232
s6 76.0952 68.293 1.114 0.266 -58.152 210.343
\n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + "
Omnibus: 1.544 Durbin-Watson: 2.026
Prob(Omnibus): 0.462 Jarque-Bera (JB): 1.421
Skew: 0.004 Prob(JB): 0.491
Kurtosis: 2.716 Cond. No. 224.


Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." + ], + "text/plain": [ + "\n", + "\"\"\"\n", + " OLS Regression Results \n", + "==============================================================================\n", + "Dep. Variable: target R-squared: 0.512\n", + "Model: OLS Adj. R-squared: 0.500\n", + "Method: Least Squares F-statistic: 43.16\n", + "Date: Mon, 11 Oct 2021 Prob (F-statistic): 4.64e-58\n", + "Time: 14:24:41 Log-Likelihood: -2281.1\n", + "No. Observations: 422 AIC: 4584.\n", + "Df Residuals: 411 BIC: 4629.\n", + "Df Model: 10 \n", + "Covariance Type: nonrobust \n", + "==============================================================================\n", + " coef std err t P>|t| [0.025 0.975]\n", + "------------------------------------------------------------------------------\n", + "const 152.7643 2.658 57.469 0.000 147.539 157.990\n", + "age 0.3035 61.286 0.005 0.996 -120.169 120.776\n", + "sex -237.6393 62.837 -3.782 0.000 -361.162 -114.117\n", + "bmi 510.5306 68.156 7.491 0.000 376.553 644.508\n", + "bp 327.7370 66.876 4.901 0.000 196.275 459.199\n", + "s1 -814.1317 424.044 -1.920 0.056 -1647.697 19.434\n", + "s2 492.8146 344.227 1.432 0.153 -183.850 1169.480\n", + "s3 102.8485 219.463 0.469 0.640 -328.561 534.258\n", + "s4 184.6065 167.336 1.103 0.271 -144.334 513.547\n", + "s5 743.5196 175.359 4.240 0.000 398.807 1088.232\n", + "s6 76.0952 68.293 1.114 0.266 -58.152 210.343\n", + "==============================================================================\n", + "Omnibus: 1.544 Durbin-Watson: 2.026\n", + "Prob(Omnibus): 0.462 Jarque-Bera (JB): 1.421\n", + "Skew: 0.004 Prob(JB): 0.491\n", + "Kurtosis: 2.716 Cond. No. 224.\n", + "==============================================================================\n", + "\n", + "Notes:\n", + "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", + "\"\"\"" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "import statsmodels.api as sm\n", + "\n", + "X = sm.add_constant(diabetes_data_train)\n", + "ols_model= sm.OLS(diabetes_target_train, X).fit()\n", + "ols_model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": 84, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4.643057835649836e-58" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "ols_model.f_pvalue" ] }, { @@ -326,11 +711,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": {}, "outputs": [], "source": [ - "# Your answers here:" + "# Your answers here:\n", + "\n", + "# Podemos ver que el valor del p-value para el F-test fue de 4.643057835649836e-58, como es menor a 0.05 podemos rechazar\n", + "# la hipotesis nula. Por lo tanto, por lo menos uno de nuestros coeficientes es significativamente distinto de 0.\n", + "\n", + "# Para ver que variables no aportan nada a nuestro modelo, observamos los p-value. Podemos ver que las columnas de \n", + "# age, s1, s2, s3, s4 y s6 tienen un p-value mayor a 0.05, por lo que la hipotesis nula se cumple, es decir,\n", + "# no nos aportan nada a nuestro model. Podemos ver para cada uno de las variables el 0 se encuentra dentro de su intervalo\n", + "# de confianza.\n", + "\n", + "# De acuerdo a los analisis realizados, se deben eliminar del modelo las columnas ya mencionadas, ya que no aportan\n", + "# nada a nuestro modelo. Pero esto seguira proporcionando el mismo r2, es deci, nuestro modelo no mejorara por hacer \n", + "# esto." ] }, { @@ -351,11 +748,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "auto = pd.read_csv(r'../auto-mpg.csv')" ] }, { @@ -367,11 +765,124 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
mpgcylindersdisplacementhorse_powerweightaccelerationmodel_yearcar_name
018.08307.0130.0350412.070\\t\"chevrolet chevelle malibu\"
115.08350.0165.0369311.570\\t\"buick skylark 320\"
218.08318.0150.0343611.070\\t\"plymouth satellite\"
316.08304.0150.0343312.070\\t\"amc rebel sst\"
417.08302.0140.0344910.570\\t\"ford torino\"
\n", + "
" + ], + "text/plain": [ + " mpg cylinders displacement horse_power weight acceleration \\\n", + "0 18.0 8 307.0 130.0 3504 12.0 \n", + "1 15.0 8 350.0 165.0 3693 11.5 \n", + "2 18.0 8 318.0 150.0 3436 11.0 \n", + "3 16.0 8 304.0 150.0 3433 12.0 \n", + "4 17.0 8 302.0 140.0 3449 10.5 \n", + "\n", + " model_year car_name \n", + "0 70 \\t\"chevrolet chevelle malibu\" \n", + "1 70 \\t\"buick skylark 320\" \n", + "2 70 \\t\"plymouth satellite\" \n", + "3 70 \\t\"amc rebel sst\" \n", + "4 70 \\t\"ford torino\" " + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.head(5)" ] }, { @@ -383,11 +894,34 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 398 entries, 0 to 397\n", + "Data columns (total 8 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 mpg 398 non-null float64\n", + " 1 cylinders 398 non-null int64 \n", + " 2 displacement 398 non-null float64\n", + " 3 horse_power 392 non-null float64\n", + " 4 weight 398 non-null int64 \n", + " 5 acceleration 398 non-null float64\n", + " 6 model_year 398 non-null int64 \n", + " 7 car_name 398 non-null object \n", + "dtypes: float64(4), int64(3), object(1)\n", + "memory usage: 25.0+ KB\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "auto.info()" ] }, { @@ -399,11 +933,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "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" + "# 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 +960,62 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 92, + "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": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto.isnull().sum()\n", + "\n", + "# Tenemos valores nulos en horse power. Los eliminamos" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "mpg 0\n", + "cylinders 0\n", + "displacement 0\n", + "horse_power 0\n", + "weight 0\n", + "acceleration 0\n", + "model_year 0\n", + "car_name 0\n", + "dtype: int64" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "auto.dropna(inplace=True)\n", + "auto.isnull().sum()" ] }, { @@ -431,11 +1027,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 102, + "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": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "auto['cylinders'].value_counts()\n", + "\n", + "# Tenemos 5 valores posibles de cilindros" ] }, { @@ -451,11 +1066,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "x = auto.drop(columns=['mpg', 'car_name'])\n", + "y = auto['mpg']\n", + "\n", + "X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)" ] }, { @@ -469,11 +1088,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.794234907542859\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "auto_model = LinearRegression()\n", + "auto_model.fit(X_train, y_train)\n", + "\n", + "y_auto_pred = auto_model.predict(X_test)\n", + "print(r2_score(y_test, y_auto_pred))" ] }, { @@ -502,11 +1134,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0.8107227953093896\n" + ] + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_pred = auto_model.predict(X_train)\n", + "print(r2_score(y_train, y_pred))" ] }, { @@ -522,11 +1164,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 107, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "0.794234907542859" + ] + }, + "execution_count": 107, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "y_test_pred = auto_model.predict(X_test)\n", + "r2_score(y_test, y_test_pred)" ] }, { @@ -551,11 +1206,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "X_train09, X_test09, y_train09, y_test09 = train_test_split(x, y, test_size=0.1, random_state=42)" ] }, { @@ -567,11 +1223,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 109, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "LinearRegression()" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "auto_model09 = LinearRegression()\n", + "auto_model09.fit(X_train09, y_train09)" ] }, { @@ -583,11 +1252,23 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El r2 para la data de train es de 0.8047940166959004\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "y_pred_train_09 = auto_model09.predict(X_train09)\n", + "y_pred_test_09 = auto_model09.predict(X_test09)\n", + "\n", + "print(f'El r2 para la data de train es de {r2_score(y_train09, y_pred_train_09)}')" ] }, { @@ -599,11 +1280,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here:\n" + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "El r2 para la data de test es de 0.8468911998183243\n" + ] + } + ], + "source": [ + "# Your code here:\n", + "print(f'El r2 para la data de test es de {r2_score(y_test09, y_pred_test_09)}')\n", + "\n", + "# Vemos como nuestro r2 mejoro con esta nueva division de las muestras." ] }, { @@ -619,7 +1311,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 114, "metadata": {}, "outputs": [], "source": [ @@ -635,11 +1327,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "\n", + "selector = RFE(auto_model, n_features_to_select=3)" ] }, { @@ -651,11 +1345,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 116, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "RFE(estimator=LinearRegression(), n_features_to_select=3)" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here:\n", + "selector.fit(X_train, y_train)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 4 3 2 1 1]\n" + ] + } + ], + "source": [ + "print(selector.ranking_)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Index(['cylinders', 'displacement', 'horse_power', 'weight', 'acceleration',\n", + " 'model_year'],\n", + " dtype='object')" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# Your code here:\n" + "X_train.columns" ] }, { @@ -669,11 +1414,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 122, "metadata": {}, "outputs": [], "source": [ - "# Your code here:\n" + "# Your code here:\n", + "x_reduced = x[['cylinders', 'acceleration', 'model_year']]\n", + "X_train_reduced, X_test_reduced, y_train_reduced, y_test_reduced = train_test_split(x_reduced, y,\n", + " test_size=0.2,\n", + " random_state=42)" ] }, { @@ -685,11 +1434,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Your code here: \n" + "execution_count": 123, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.7144839092209849" + ] + }, + "execution_count": 123, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Your code here: \n", + "auto_model_reduced = LinearRegression()\n", + "auto_model_reduced.fit(X_train_reduced, y_train_reduced)\n", + "\n", + "y_pred_reduced = auto_model_reduced.predict(X_train_reduced)\n", + "r2_score(y_train_reduced, y_pred_reduced)" ] }, { @@ -726,7 +1491,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.6" + "version": "3.8.8" } }, "nbformat": 4,