{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <p style=\"text-align: right;\"> &#9989; Put your name here.</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# In-Class: MidTerm Review\n",
    "## Modeling the World Happiness Index Using a Variety of Techniques"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Goals for this homework assignment\n",
    "By the end of this assignment, you should be able to:\n",
    "* Debug Python code.\n",
    "* Get more experience with Pandas using excel spread sheets.\n",
    "* Make plots of your model using <code>matplotlib</code>.\n",
    "* Practice writing and executing a Python script.\n",
    "\n",
    "You will build a classification model that investigates what predicts World Happiness Indicies above/below the mean index."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 1: Reading, Describing, and Cleaning the Data\n",
    "Before we read in the data and begin working with it, let's import the libraries that we would typically use for this task. You can always come back to this cell and import additional libraries that you need."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score, roc_curve, roc_auc_score, confusion_matrix\n",
    "import seaborn as sea\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The United Nations compiles an annual publication called the [World Happiness Report](https://en.wikipedia.org/wiki/World_Happiness_Report) that ranks countries by the \"happiness score\" or \"life ladder\". These scores are based on a surveys of citizens of each country on a variety of factors such as:\n",
    "* Gross Domestic Product (GDP) per capita\n",
    "* Perceptions of social support\n",
    "* Life expectancy\n",
    "* Freedom of choice\n",
    "\n",
    "[Data from the UN](https://worldhappiness.report/) is available from 2005-2019, which we have downloaded in a `.csv` format. The survey has changed over time, so that some factors and questions have been omitted. So some of the data is incomplete or missing.\n",
    "\n",
    "### 1.1 Read the data\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Read in the survey data from `happiness_index.csv` into a DataFrame and print the `.head()` of the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 Clean the data\n",
    "\n",
    "#### 1.2.1 Remove and track excess data\n",
    "\n",
    "In order to build a classification model to predict the happiness score above or below the mean (`Life Ladder`), we need to remove the excess features that are not present in every year's data. \n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Drop the columns that are not the following:\n",
    "* Country name\n",
    "* year\n",
    "* Life Ladder\n",
    "* Log GDP per capita\n",
    "* Social support\n",
    "* Healthy life expectancy at birth\n",
    "* Freedom to make life choices\n",
    "* Generosity\n",
    "* Perceptions of corruption\n",
    "* Positive affect\n",
    "* Negative affect\n",
    "* Confidence in national government\n",
    "* Democratic Quality\n",
    "* Delivery Quality\n",
    "\n",
    "Make sure to make a list of the columns that you dropped. This is important if you need to report what you didn't consider in the model. Print the `.head()` of the DataFrame after dropping the unneeded columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.2 Focus on the 2017 data\n",
    "\n",
    "Your data should include scores for a variety of years (2005-2019) for each country. Let's focus on a single year: 2017. \n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Filter your data to focus only on the latest available data from 2017. Try to use a mask. Print the `.head()` of that DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2.3 Missing data\n",
    "\n",
    "Not all the countries have reported values for every feature. You should check if any countries have missing data. We could impute these missing data in a variety of ways, but instead to make our work a little simpler, we will simply drop a country if it has any missing data. Unlike the homework, don't worry about keeping track of those countries unless you want to.\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Drop any country from the data that is missing data. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 Describe the data\n",
    "\n",
    "Now that you have a cleaned data set, let's look into the data. We can start that by describing the data and making histograms of it. \n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Focusing on the data collected in the survey (i.e., not the country name or year), describe the data and make histograms describing the distributions of the various features."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.4 Make a machine learning classfication problem\n",
    "\n",
    "On the homework, you solved this using linear regression to predict the value of `Life Ladder`. For this review, we will instead make `Life Ladder` a binary value based on being above or below the mean value for `Life Ladder`. We will then use the train/test paradigm with a variety of models (starting with Logistic Regression) to investigate the accracy of the models.\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Replace the `Life Ladder` variable with a binary 0/1 based on if the value of `Life Ladder` is above or below the mean for all the remaining data. Print `.head()` of the new data frame to confirm the changes were made."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Making a model\n",
    "\n",
    "### 2.1 The correlation matrix\n",
    "\n",
    "To get our model started, we need to first investigate the correlations between the variables. \n",
    "\n",
    "#### 2.1.1 Visualizing the correlation matrix\n",
    "Just the matrix isn't as useful as a visualization like a heat map of the correlation matrix.\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - For the variables in the survey (i.e., not the country and year), create a heat map that represents the correlation matrix. Answer the questions below as well."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1.2 Visualizing Feature Spaces\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Choose two variables with high correlations with the binary class from the correlation matrix above. Make a scatter p the feature space for those two features, make sure to color the points based on the class label."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1.3 Determining which features will matter\n",
    "\n",
    "Look at your correlation matrix visualization, specifically at how the variables correlate with `Life Ladder`. Which variables do you expect to be most important to your linear model? How can you tell from the correlation matrix (be specific)?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=8 color=\"#009600\">&#9998;</font> Answer here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 Constructing and evaluating the full model\n",
    "\n",
    "We will start modeling our data using `LogisticRegression` by predicting the `Life Ladder` class based on all the features. We will also evaluate the model using a variety of techniques.\n",
    "\n",
    "#### 2.2.1 Standardize and split the data\n",
    "\n",
    " Do This - Using the approach we have developed, let's first split off the features from the data, then standardize all the features (using `StandardScaler`), and then split the features and labels into training and testing sets. **Remember not to standard scale the labels.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2.2 Build the model and predict the labels\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Construct a logistic regression model that predicts the `Life Ladder` class from the other features using the test data. Using the test features, predict the labels and print them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2.3 Evaluating fit using predicted values\n",
    "\n",
    "Using the metrics of `sklearn`, let's investigate the quality of the model. \n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Print the confusion matrix for the model and the accuracy score."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How well do the predicted values of your model compare to the actual values? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=8 color=\"#009600\">&#9998;</font> Answer here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2.4 ROC Curve\n",
    "\n",
    "As we can do with any binary classifer, we can construct an ROC Curve to see how well our model functions for different thresholds.\n",
    "\n",
    "<font size=8 color=\"#009600\">&#9998;</font> Do This - Plot an ROC Curve for this model and print the AUC. Include the \"chance\" line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "### your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Creating and evaluating other models\n",
    "\n",
    "It is very common to evaluate other models that are attempting to perform the same task. In this case, you have learned about two additional models:\n",
    "* K Nearest Neighbors\n",
    "* Support Vector Machines (with linear and RBF kernels)\n",
    "\n",
    "Below, construct models of your data with each of these three models and evaluate them in same way as you did for Logisitic Regression. Do they perform better or worse than Logistic Regression? Which appears to perform the best?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font size=8 color=\"#009600\">&#9998;</font> Answer here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 KNN\n",
    "\n",
    "Make a KNN model using `n_neighbors = 3` and evaluate it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2 SVM with Linear Kernel\n",
    "\n",
    "Make an SVM model using a linear kernel with `C = 10` and evaluate it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "## your code here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.3 SVM with RBF Kernel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make an SVM model using an `RBF` kernel with `C = 10`, `gamma = 0.01`, and evaluate it."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "### Assignment wrap-up\n",
    "\n",
    "Please fill out the form that appears when you run the code below.  **You must completely fill this out in order to receive credit for the assignment!**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<iframe \n",
       "\tsrc=\"https://docs.google.com/forms/d/e/1FAIpQLSeiTaur6kllgiUknZSulXqe9JFkD88EUjf1r8pWBV2DuABXdQ/viewform?usp=sf_link\" \n",
       "\twidth=\"800px\" \n",
       "\theight=\"600px\" \n",
       "\tframeborder=\"0\" \n",
       "\tmarginheight=\"0\" \n",
       "\tmarginwidth=\"0\">\n",
       "\tLoading...\n",
       "</iframe>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "HTML(\n",
    "\"\"\"\n",
    "<iframe \n",
    "\tsrc=\"https://docs.google.com/forms/d/e/1FAIpQLSeiTaur6kllgiUknZSulXqe9JFkD88EUjf1r8pWBV2DuABXdQ/viewform?usp=sf_link\" \n",
    "\twidth=\"800px\" \n",
    "\theight=\"600px\" \n",
    "\tframeborder=\"0\" \n",
    "\tmarginheight=\"0\" \n",
    "\tmarginwidth=\"0\">\n",
    "\tLoading...\n",
    "</iframe>\n",
    "\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Congratulations, you're done!\n",
    "\n",
    "Submit this assignment by uploading it to the course Desire2Learn web page.  Go to the \"Homework Assignments\" folder, find the dropbox link for Homework #2, and upload it there."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&#169; Copyright Michigan State University Board of Trustees"
   ]
  }
 ],
 "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
