{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8b66d061",
   "metadata": {},
   "source": [
    "### Creates a correlation matrix\n",
    "This used to see if we should remove or combine some features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "557ed2b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 180900 characters]",
      "text/plain": [
       "<Figure size 800x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>workclass</th>\n",
       "      <th>fnlwgt</th>\n",
       "      <th>education</th>\n",
       "      <th>education.num</th>\n",
       "      <th>marital.status</th>\n",
       "      <th>occupation</th>\n",
       "      <th>relationship</th>\n",
       "      <th>race</th>\n",
       "      <th>sex</th>\n",
       "      <th>capital.gain</th>\n",
       "      <th>capital.loss</th>\n",
       "      <th>hours.per.week</th>\n",
       "      <th>native.country</th>\n",
       "      <th>income</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>90</td>\n",
       "      <td>?</td>\n",
       "      <td>77053</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Widowed</td>\n",
       "      <td>?</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>4356</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>82</td>\n",
       "      <td>Private</td>\n",
       "      <td>132870</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Widowed</td>\n",
       "      <td>Exec-managerial</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>4356</td>\n",
       "      <td>18</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>66</td>\n",
       "      <td>?</td>\n",
       "      <td>186061</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Widowed</td>\n",
       "      <td>?</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>Black</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>4356</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>54</td>\n",
       "      <td>Private</td>\n",
       "      <td>140359</td>\n",
       "      <td>7th-8th</td>\n",
       "      <td>4</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Machine-op-inspct</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3900</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>41</td>\n",
       "      <td>Private</td>\n",
       "      <td>264663</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Separated</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Own-child</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3900</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>34</td>\n",
       "      <td>Private</td>\n",
       "      <td>216864</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Other-service</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3770</td>\n",
       "      <td>45</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>38</td>\n",
       "      <td>Private</td>\n",
       "      <td>150601</td>\n",
       "      <td>10th</td>\n",
       "      <td>6</td>\n",
       "      <td>Separated</td>\n",
       "      <td>Adm-clerical</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>3770</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>74</td>\n",
       "      <td>State-gov</td>\n",
       "      <td>88638</td>\n",
       "      <td>Doctorate</td>\n",
       "      <td>16</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Other-relative</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3683</td>\n",
       "      <td>20</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>68</td>\n",
       "      <td>Federal-gov</td>\n",
       "      <td>422013</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3683</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>41</td>\n",
       "      <td>Private</td>\n",
       "      <td>70037</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Craft-repair</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>3004</td>\n",
       "      <td>60</td>\n",
       "      <td>?</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>45</td>\n",
       "      <td>Private</td>\n",
       "      <td>172274</td>\n",
       "      <td>Doctorate</td>\n",
       "      <td>16</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Unmarried</td>\n",
       "      <td>Black</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>3004</td>\n",
       "      <td>35</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>38</td>\n",
       "      <td>Self-emp-not-inc</td>\n",
       "      <td>164526</td>\n",
       "      <td>Prof-school</td>\n",
       "      <td>15</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>45</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>52</td>\n",
       "      <td>Private</td>\n",
       "      <td>129177</td>\n",
       "      <td>Bachelors</td>\n",
       "      <td>13</td>\n",
       "      <td>Widowed</td>\n",
       "      <td>Other-service</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>20</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>32</td>\n",
       "      <td>Private</td>\n",
       "      <td>136204</td>\n",
       "      <td>Masters</td>\n",
       "      <td>14</td>\n",
       "      <td>Separated</td>\n",
       "      <td>Exec-managerial</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>55</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>51</td>\n",
       "      <td>?</td>\n",
       "      <td>172175</td>\n",
       "      <td>Doctorate</td>\n",
       "      <td>16</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>?</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>46</td>\n",
       "      <td>Private</td>\n",
       "      <td>45363</td>\n",
       "      <td>Prof-school</td>\n",
       "      <td>15</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Prof-specialty</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>45</td>\n",
       "      <td>Private</td>\n",
       "      <td>172822</td>\n",
       "      <td>11th</td>\n",
       "      <td>7</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Transport-moving</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>76</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>57</td>\n",
       "      <td>Private</td>\n",
       "      <td>317847</td>\n",
       "      <td>Masters</td>\n",
       "      <td>14</td>\n",
       "      <td>Divorced</td>\n",
       "      <td>Exec-managerial</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>50</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>22</td>\n",
       "      <td>Private</td>\n",
       "      <td>119592</td>\n",
       "      <td>Assoc-acdm</td>\n",
       "      <td>12</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Handlers-cleaners</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>40</td>\n",
       "      <td>?</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>34</td>\n",
       "      <td>Private</td>\n",
       "      <td>203034</td>\n",
       "      <td>Bachelors</td>\n",
       "      <td>13</td>\n",
       "      <td>Separated</td>\n",
       "      <td>Sales</td>\n",
       "      <td>Not-in-family</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>2824</td>\n",
       "      <td>50</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    age         workclass  fnlwgt     education  education.num marital.status  \\\n",
       "0    90                 ?   77053       HS-grad              9        Widowed   \n",
       "1    82           Private  132870       HS-grad              9        Widowed   \n",
       "2    66                 ?  186061  Some-college             10        Widowed   \n",
       "3    54           Private  140359       7th-8th              4       Divorced   \n",
       "4    41           Private  264663  Some-college             10      Separated   \n",
       "5    34           Private  216864       HS-grad              9       Divorced   \n",
       "6    38           Private  150601          10th              6      Separated   \n",
       "7    74         State-gov   88638     Doctorate             16  Never-married   \n",
       "8    68       Federal-gov  422013       HS-grad              9       Divorced   \n",
       "9    41           Private   70037  Some-college             10  Never-married   \n",
       "10   45           Private  172274     Doctorate             16       Divorced   \n",
       "11   38  Self-emp-not-inc  164526   Prof-school             15  Never-married   \n",
       "12   52           Private  129177     Bachelors             13        Widowed   \n",
       "13   32           Private  136204       Masters             14      Separated   \n",
       "14   51                 ?  172175     Doctorate             16  Never-married   \n",
       "15   46           Private   45363   Prof-school             15       Divorced   \n",
       "16   45           Private  172822          11th              7       Divorced   \n",
       "17   57           Private  317847       Masters             14       Divorced   \n",
       "18   22           Private  119592    Assoc-acdm             12  Never-married   \n",
       "19   34           Private  203034     Bachelors             13      Separated   \n",
       "\n",
       "           occupation    relationship   race     sex  capital.gain  \\\n",
       "0                   ?   Not-in-family  White  Female             0   \n",
       "1     Exec-managerial   Not-in-family  White  Female             0   \n",
       "2                   ?       Unmarried  Black  Female             0   \n",
       "3   Machine-op-inspct       Unmarried  White  Female             0   \n",
       "4      Prof-specialty       Own-child  White  Female             0   \n",
       "5       Other-service       Unmarried  White  Female             0   \n",
       "6        Adm-clerical       Unmarried  White    Male             0   \n",
       "7      Prof-specialty  Other-relative  White  Female             0   \n",
       "8      Prof-specialty   Not-in-family  White  Female             0   \n",
       "9        Craft-repair       Unmarried  White    Male             0   \n",
       "10     Prof-specialty       Unmarried  Black  Female             0   \n",
       "11     Prof-specialty   Not-in-family  White    Male             0   \n",
       "12      Other-service   Not-in-family  White  Female             0   \n",
       "13    Exec-managerial   Not-in-family  White    Male             0   \n",
       "14                  ?   Not-in-family  White    Male             0   \n",
       "15     Prof-specialty   Not-in-family  White    Male             0   \n",
       "16   Transport-moving   Not-in-family  White    Male             0   \n",
       "17    Exec-managerial   Not-in-family  White    Male             0   \n",
       "18  Handlers-cleaners   Not-in-family  Black    Male             0   \n",
       "19              Sales   Not-in-family  White    Male             0   \n",
       "\n",
       "    capital.loss  hours.per.week native.country income  \n",
       "0           4356              40  United-States  <=50K  \n",
       "1           4356              18  United-States  <=50K  \n",
       "2           4356              40  United-States  <=50K  \n",
       "3           3900              40  United-States  <=50K  \n",
       "4           3900              40  United-States  <=50K  \n",
       "5           3770              45  United-States  <=50K  \n",
       "6           3770              40  United-States  <=50K  \n",
       "7           3683              20  United-States   >50K  \n",
       "8           3683              40  United-States  <=50K  \n",
       "9           3004              60              ?   >50K  \n",
       "10          3004              35  United-States   >50K  \n",
       "11          2824              45  United-States   >50K  \n",
       "12          2824              20  United-States   >50K  \n",
       "13          2824              55  United-States   >50K  \n",
       "14          2824              40  United-States   >50K  \n",
       "15          2824              40  United-States   >50K  \n",
       "16          2824              76  United-States   >50K  \n",
       "17          2824              50  United-States   >50K  \n",
       "18          2824              40              ?   >50K  \n",
       "19          2824              50  United-States   >50K  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "import seaborn as sns\n",
    "\n",
    "df = pd.read_csv('./Datasets/adult.csv', comment = '#')\n",
    "\n",
    "# First, create a copy of the dataframe to avoid modifying the original\n",
    "df_encoded = df.copy()\n",
    "df_encoded.drop(['fnlwgt', 'education'], axis=1, inplace=True)\n",
    "\n",
    "# drop all rows that contain '?'\n",
    "df_encoded = df_encoded[(df_encoded != '?').all(axis=1)]\n",
    "\n",
    "# Apply label encoding to categorical columns\n",
    "categorical_columns = ['workclass', 'marital.status', 'occupation', \n",
    "                      'relationship', 'race', 'sex', 'native.country', 'income']\n",
    "\n",
    "for column in categorical_columns:\n",
    "    le = LabelEncoder()\n",
    "    df_encoded[column] = le.fit_transform(df_encoded[column])\n",
    "\n",
    "matrix = df_encoded.corr()\n",
    "\n",
    "plt.figure(figsize=(8,6))\n",
    "sns.heatmap(matrix, annot=True, cmap=\"coolwarm\", fmt=\".2f\", linewidths=0.5)\n",
    "plt.title(\"Correlation Heatmap\")\n",
    "plt.show()\n",
    "\n",
    "#df_encoded.head(10)\n",
    "#df_encoded.info()\n",
    "df.head(20)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e55b361b",
   "metadata": {},
   "source": [
    "### Creates a pairplot\n",
    "This demonstrates that we need a model that is appropriate for discrete data (Decision Tree, Random Forest, Naive Bayes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8e3bd968",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1814256 characters]",
      "text/plain": [
       "<Figure size 3250x3250 with 182 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.pairplot(df_encoded, diag_kind=\"kde\")\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
