{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Project: Identify Customer Segments\n",
    "\n",
    "In this project, you will apply unsupervised learning techniques to identify segments of the population that form the core customer base for a mail-order sales company in Germany. These segments can then be used to direct marketing campaigns towards audiences that will have the highest expected rate of returns. The data that you will use has been provided by our partners at Bertelsmann Arvato Analytics, and represents a real-life data science task.\n",
    "\n",
    "This notebook will help you complete this task by providing a framework within which you will perform your analysis steps. In each step of the project, you will see some text describing the subtask that you will perform, followed by one or more code cells for you to complete your work. **Feel free to add additional code and markdown cells as you go along so that you can explore everything in precise chunks.** The code cells provided in the base template will outline only the major tasks, and will usually not be enough to cover all of the minor tasks that comprise it.\n",
    "\n",
    "It should be noted that while there will be precise guidelines on how you should handle certain tasks in the project, there will also be places where an exact specification is not provided. **There will be times in the project where you will need to make and justify your own decisions on how to treat the data.** These are places where there may not be only one way to handle the data. In real-life tasks, there may be many valid ways to approach an analysis task. One of the most important things you can do is clearly document your approach so that other scientists can understand the decisions you've made.\n",
    "\n",
    "At the end of most sections, there will be a Markdown cell labeled **Discussion**. In these cells, you will report your findings for the completed section, as well as document the decisions that you made in your approach to each subtask. **Your project will be evaluated not just on the code used to complete the tasks outlined, but also your communication about your observations and conclusions at each stage.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import libraries here; add more as necessary\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# magic word for producing visualizations in notebook\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 0: Load the Data\n",
    "\n",
    "There are four files associated with this project (not including this one):\n",
    "\n",
    "- `Udacity_AZDIAS_Subset.csv`: Demographics data for the general population of Germany; 891211 persons (rows) x 85 features (columns).\n",
    "- `Udacity_CUSTOMERS_Subset.csv`: Demographics data for customers of a mail-order company; 191652 persons (rows) x 85 features (columns).\n",
    "- `Data_Dictionary.md`: Detailed information file about the features in the provided datasets.\n",
    "- `AZDIAS_Feature_Summary.csv`: Summary of feature attributes for demographics data; 85 features (rows) x 4 columns\n",
    "\n",
    "Each row of the demographics files represents a single person, but also includes information outside of individuals, including information about their household, building, and neighborhood. You will use this information to cluster the general population into groups with similar demographic properties. Then, you will see how the people in the customers dataset fit into those created clusters. The hope here is that certain clusters are over-represented in the customers data, as compared to the general population; those over-represented clusters will be assumed to be part of the core userbase. This information can then be used for further applications, such as targeting for a marketing campaign.\n",
    "\n",
    "To start off with, load in the demographics data for the general population into a pandas DataFrame, and do the same for the feature attributes summary. Note for all of the `.csv` data files in this project: they're semicolon (`;`) delimited, so you'll need an additional argument in your [`read_csv()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) call to read in the data properly. Also, considering the size of the main dataset, it may take some time for it to load completely.\n",
    "\n",
    "Once the dataset is loaded, it's recommended that you take a little bit of time just browsing the general structure of the dataset and feature summary file. You'll be getting deep into the innards of the cleaning in the first major step of the project, so gaining some general familiarity can help you get your bearings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load in the general demographics data.\n",
    "azdias = pd.read_csv('Udacity_AZDIAS_Subset.csv', sep=';')\n",
    "\n",
    "# Load in the feature summary file.\n",
    "feat_info = pd.read_csv('AZDIAS_Feature_Summary.csv', sep=';')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "azdias shape is (891221, 85). feat shape is (85, 4)\n"
     ]
    },
    {
     "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>AGER_TYP</th>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>-1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGER_TYP  ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  \\\n",
       "0        -1                     2          1            2.0   \n",
       "1        -1                     1          2            5.0   \n",
       "2        -1                     3          2            3.0   \n",
       "3         2                     4          2            2.0   \n",
       "4        -1                     3          1            5.0   \n",
       "5         3                     1          2            2.0   \n",
       "6        -1                     2          2            5.0   \n",
       "7        -1                     1          1            3.0   \n",
       "8        -1                     3          1            3.0   \n",
       "9        -1                     3          2            4.0   \n",
       "\n",
       "   FINANZ_MINIMALIST  FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "0                  3              4                 3               5   \n",
       "1                  1              5                 2               5   \n",
       "2                  1              4                 1               2   \n",
       "3                  4              2                 5               2   \n",
       "4                  4              3                 4               1   \n",
       "5                  3              1                 5               2   \n",
       "6                  1              5                 1               5   \n",
       "7                  3              3                 4               1   \n",
       "8                  4              4                 2               4   \n",
       "9                  2              4                 2               3   \n",
       "\n",
       "   FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER    ...     PLZ8_ANTG1  PLZ8_ANTG2  \\\n",
       "0                      5                 3    ...            NaN         NaN   \n",
       "1                      4                 5    ...            2.0         3.0   \n",
       "2                      3                 5    ...            3.0         3.0   \n",
       "3                      1                 2    ...            2.0         2.0   \n",
       "4                      3                 2    ...            2.0         4.0   \n",
       "5                      2                 5    ...            2.0         3.0   \n",
       "6                      4                 3    ...            3.0         3.0   \n",
       "7                      3                 2    ...            3.0         3.0   \n",
       "8                      2                 2    ...            2.0         3.0   \n",
       "9                      5                 4    ...            2.0         3.0   \n",
       "\n",
       "   PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "1         2.0         1.0          1.0       5.0       4.0     3.0   \n",
       "2         1.0         0.0          1.0       4.0       4.0     3.0   \n",
       "3         2.0         0.0          1.0       3.0       4.0     2.0   \n",
       "4         2.0         1.0          2.0       3.0       3.0     4.0   \n",
       "5         1.0         1.0          1.0       5.0       5.0     2.0   \n",
       "6         1.0         0.0          1.0       5.0       5.0     4.0   \n",
       "7         1.0         0.0          1.0       4.0       4.0     2.0   \n",
       "8         2.0         1.0          1.0       3.0       3.0     2.0   \n",
       "9         2.0         1.0          1.0       3.0       3.0     2.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          NaN       NaN  \n",
       "1          5.0       4.0  \n",
       "2          5.0       2.0  \n",
       "3          3.0       3.0  \n",
       "4          6.0       5.0  \n",
       "5          3.0       3.0  \n",
       "6          6.0       3.0  \n",
       "7          5.0       2.0  \n",
       "8          4.0       3.0  \n",
       "9          3.0       1.0  \n",
       "\n",
       "[10 rows x 85 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check the structure of the data after it's loaded (e.g. print the number of\n",
    "# rows and columns, print the first few rows).\n",
    "\n",
    "azdias_shape=azdias.shape\n",
    "feat_shape=feat_info.shape\n",
    "\n",
    "print(f'azdias shape is {azdias_shape}. feat shape is {feat_shape}')\n",
    "\n",
    "azdias.head(10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "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>attribute</th>\n",
       "      <th>information_level</th>\n",
       "      <th>type</th>\n",
       "      <th>missing_or_unknown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AGER_TYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ALTERSKATEGORIE_GROB</td>\n",
       "      <td>person</td>\n",
       "      <td>ordinal</td>\n",
       "      <td>[-1,0,9]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ANREDE_KZ</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CJT_GESAMTTYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FINANZ_MINIMALIST</td>\n",
       "      <td>person</td>\n",
       "      <td>ordinal</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              attribute information_level         type missing_or_unknown\n",
       "0              AGER_TYP            person  categorical             [-1,0]\n",
       "1  ALTERSKATEGORIE_GROB            person      ordinal           [-1,0,9]\n",
       "2             ANREDE_KZ            person  categorical             [-1,0]\n",
       "3         CJT_GESAMTTYP            person  categorical                [0]\n",
       "4     FINANZ_MINIMALIST            person      ordinal               [-1]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_info.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> **Tip**: Add additional cells to keep everything in reasonably-sized chunks! Keyboard shortcut `esc --> a` (press escape to enter command mode, then press the 'A' key) adds a new cell before the active cell, and `esc --> b` adds a new cell after the active cell. If you need to convert an active cell to a markdown cell, use `esc --> m` and to convert to a code cell, use `esc --> y`. \n",
    "\n",
    "## Step 1: Preprocessing\n",
    "\n",
    "### Step 1.1: Assess Missing Data\n",
    "\n",
    "The feature summary file contains a summary of properties for each demographics data column. You will use this file to help you make cleaning decisions during this stage of the project. First of all, you should assess the demographics data in terms of missing data. Pay attention to the following points as you perform your analysis, and take notes on what you observe. Make sure that you fill in the **Discussion** cell with your findings and decisions at the end of each step that has one!\n",
    "\n",
    "#### Step 1.1.1: Convert Missing Value Codes to NaNs\n",
    "The fourth column of the feature attributes summary (loaded in above as `feat_info`) documents the codes from the data dictionary that indicate missing or unknown data. While the file encodes this as a list (e.g. `[-1,0]`), this will get read in as a string object. You'll need to do a little bit of parsing to make use of it to identify and clean the data. Convert data that matches a 'missing' or 'unknown' value code into a numpy NaN value. You might want to see how much data takes on a 'missing' or 'unknown' code, and how much data is naturally missing, as a point of interest.\n",
    "\n",
    "**As one more reminder, you are encouraged to add additional cells to break up your analysis into manageable chunks.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following will replace the indexes with np.na in the dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "feat_list = feat_info['missing_or_unknown'].tolist()\n",
    "missing_list = []\n",
    "\n",
    "for i in feat_list:\n",
    "    subcount = 0\n",
    "    i = i.replace('[', '')\n",
    "    i = i.replace(']', '')\n",
    "    i = i.split(',')\n",
    "    missing_list.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace(value, items, **kwargs):\n",
    "    for i in items:\n",
    "        try:\n",
    "            if value == np.int(i):\n",
    "                return np.nan\n",
    "            else:\n",
    "                pass\n",
    "        except ValueError:\n",
    "            if value == str(i):\n",
    "                return np.nan\n",
    "            else:\n",
    "                pass\n",
    "    return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AGER_TYP 0\n",
      "ALTERSKATEGORIE_GROB 1\n",
      "ANREDE_KZ 2\n",
      "CJT_GESAMTTYP 3\n",
      "FINANZ_MINIMALIST 4\n",
      "FINANZ_SPARER 5\n",
      "FINANZ_VORSORGER 6\n",
      "FINANZ_ANLEGER 7\n",
      "FINANZ_UNAUFFAELLIGER 8\n",
      "FINANZ_HAUSBAUER 9\n",
      "FINANZTYP 10\n",
      "GEBURTSJAHR 11\n",
      "GFK_URLAUBERTYP 12\n",
      "GREEN_AVANTGARDE 13\n",
      "HEALTH_TYP 14\n",
      "LP_LEBENSPHASE_FEIN 15\n",
      "LP_LEBENSPHASE_GROB 16\n",
      "LP_FAMILIE_FEIN 17\n",
      "LP_FAMILIE_GROB 18\n",
      "LP_STATUS_FEIN 19\n",
      "LP_STATUS_GROB 20\n",
      "NATIONALITAET_KZ 21\n",
      "PRAEGENDE_JUGENDJAHRE 22\n",
      "RETOURTYP_BK_S 23\n",
      "SEMIO_SOZ 24\n",
      "SEMIO_FAM 25\n",
      "SEMIO_REL 26\n",
      "SEMIO_MAT 27\n",
      "SEMIO_VERT 28\n",
      "SEMIO_LUST 29\n",
      "SEMIO_ERL 30\n",
      "SEMIO_KULT 31\n",
      "SEMIO_RAT 32\n",
      "SEMIO_KRIT 33\n",
      "SEMIO_DOM 34\n",
      "SEMIO_KAEM 35\n",
      "SEMIO_PFLICHT 36\n",
      "SEMIO_TRADV 37\n",
      "SHOPPER_TYP 38\n",
      "SOHO_KZ 39\n",
      "TITEL_KZ 40\n",
      "VERS_TYP 41\n",
      "ZABEOTYP 42\n",
      "ALTER_HH 43\n",
      "ANZ_PERSONEN 44\n",
      "ANZ_TITEL 45\n",
      "HH_EINKOMMEN_SCORE 46\n",
      "KK_KUNDENTYP 47\n",
      "W_KEIT_KIND_HH 48\n",
      "WOHNDAUER_2008 49\n",
      "ANZ_HAUSHALTE_AKTIV 50\n",
      "ANZ_HH_TITEL 51\n",
      "GEBAEUDETYP 52\n",
      "KONSUMNAEHE 53\n",
      "MIN_GEBAEUDEJAHR 54\n",
      "OST_WEST_KZ 55\n",
      "WOHNLAGE 56\n",
      "CAMEO_DEUG_2015 57\n",
      "CAMEO_DEU_2015 58\n",
      "CAMEO_INTL_2015 59\n",
      "KBA05_ANTG1 60\n",
      "KBA05_ANTG2 61\n",
      "KBA05_ANTG3 62\n",
      "KBA05_ANTG4 63\n",
      "KBA05_BAUMAX 64\n",
      "KBA05_GBZ 65\n",
      "BALLRAUM 66\n",
      "EWDICHTE 67\n",
      "INNENSTADT 68\n",
      "GEBAEUDETYP_RASTER 69\n",
      "KKK 70\n",
      "MOBI_REGIO 71\n",
      "ONLINE_AFFINITAET 72\n",
      "REGIOTYP 73\n",
      "KBA13_ANZAHL_PKW 74\n",
      "PLZ8_ANTG1 75\n",
      "PLZ8_ANTG2 76\n",
      "PLZ8_ANTG3 77\n",
      "PLZ8_ANTG4 78\n",
      "PLZ8_BAUMAX 79\n",
      "PLZ8_HHZ 80\n",
      "PLZ8_GBZ 81\n",
      "ARBEIT 82\n",
      "ORTSGR_KLS9 83\n",
      "RELAT_AB 84\n"
     ]
    }
   ],
   "source": [
    "for col, index in zip(azdias, range(len(missing_list))):\n",
    "    print(col, index)\n",
    "    azdias.iloc[:,index] = azdias.iloc[:,index].apply(replace, items=missing_list[index], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "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>AGER_TYP</th>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>20 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    AGER_TYP  ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  \\\n",
       "0        NaN                   2.0          1            2.0   \n",
       "1        NaN                   1.0          2            5.0   \n",
       "2        NaN                   3.0          2            3.0   \n",
       "3        2.0                   4.0          2            2.0   \n",
       "4        NaN                   3.0          1            5.0   \n",
       "5        3.0                   1.0          2            2.0   \n",
       "6        NaN                   2.0          2            5.0   \n",
       "7        NaN                   1.0          1            3.0   \n",
       "8        NaN                   3.0          1            3.0   \n",
       "9        NaN                   3.0          2            4.0   \n",
       "10       NaN                   3.0          2            1.0   \n",
       "11       NaN                   2.0          1            6.0   \n",
       "12       NaN                   3.0          1            6.0   \n",
       "13       NaN                   1.0          2            5.0   \n",
       "14       NaN                   3.0          1            6.0   \n",
       "15       1.0                   4.0          2            4.0   \n",
       "16       NaN                   1.0          2            1.0   \n",
       "17       NaN                   2.0          1            6.0   \n",
       "18       NaN                   2.0          2            6.0   \n",
       "19       NaN                   3.0          1            3.0   \n",
       "\n",
       "    FINANZ_MINIMALIST  FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "0                   3              4                 3               5   \n",
       "1                   1              5                 2               5   \n",
       "2                   1              4                 1               2   \n",
       "3                   4              2                 5               2   \n",
       "4                   4              3                 4               1   \n",
       "5                   3              1                 5               2   \n",
       "6                   1              5                 1               5   \n",
       "7                   3              3                 4               1   \n",
       "8                   4              4                 2               4   \n",
       "9                   2              4                 2               3   \n",
       "10                  2              2                 5               3   \n",
       "11                  3              4                 3               5   \n",
       "12                  5              3                 4               2   \n",
       "13                  1              4                 3               5   \n",
       "14                  3              4                 3               5   \n",
       "15                  4              1                 5               1   \n",
       "16                  4              3                 1               4   \n",
       "17                  3              4                 3               5   \n",
       "18                  2              4                 1               5   \n",
       "19                  5              2                 3               1   \n",
       "\n",
       "    FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER    ...     PLZ8_ANTG1  PLZ8_ANTG2  \\\n",
       "0                       5                 3    ...            NaN         NaN   \n",
       "1                       4                 5    ...            2.0         3.0   \n",
       "2                       3                 5    ...            3.0         3.0   \n",
       "3                       1                 2    ...            2.0         2.0   \n",
       "4                       3                 2    ...            2.0         4.0   \n",
       "5                       2                 5    ...            2.0         3.0   \n",
       "6                       4                 3    ...            3.0         3.0   \n",
       "7                       3                 2    ...            3.0         3.0   \n",
       "8                       2                 2    ...            2.0         3.0   \n",
       "9                       5                 4    ...            2.0         3.0   \n",
       "10                      1                 5    ...            2.0         4.0   \n",
       "11                      5                 3    ...            NaN         NaN   \n",
       "12                      4                 1    ...            3.0         3.0   \n",
       "13                      5                 2    ...            2.0         1.0   \n",
       "14                      5                 3    ...            NaN         NaN   \n",
       "15                      1                 4    ...            NaN         NaN   \n",
       "16                      5                 1    ...            3.0         3.0   \n",
       "17                      5                 3    ...            NaN         NaN   \n",
       "18                      4                 1    ...            2.0         3.0   \n",
       "19                      3                 1    ...            2.0         4.0   \n",
       "\n",
       "    PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0          NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "1          2.0         1.0          1.0       5.0       4.0     3.0   \n",
       "2          1.0         0.0          1.0       4.0       4.0     3.0   \n",
       "3          2.0         0.0          1.0       3.0       4.0     2.0   \n",
       "4          2.0         1.0          2.0       3.0       3.0     4.0   \n",
       "5          1.0         1.0          1.0       5.0       5.0     2.0   \n",
       "6          1.0         0.0          1.0       5.0       5.0     4.0   \n",
       "7          1.0         0.0          1.0       4.0       4.0     2.0   \n",
       "8          2.0         1.0          1.0       3.0       3.0     2.0   \n",
       "9          2.0         1.0          1.0       3.0       3.0     2.0   \n",
       "10         2.0         0.0          2.0       3.0       3.0     4.0   \n",
       "11         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "12         1.0         0.0          1.0       5.0       5.0     3.0   \n",
       "13         1.0         1.0          1.0       3.0       3.0     3.0   \n",
       "14         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "15         NaN         NaN          NaN       NaN       NaN     4.0   \n",
       "16         1.0         0.0          1.0       3.0       4.0     1.0   \n",
       "17         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "18         2.0         1.0          1.0       3.0       3.0     3.0   \n",
       "19         2.0         1.0          2.0       5.0       4.0     4.0   \n",
       "\n",
       "    ORTSGR_KLS9  RELAT_AB  \n",
       "0           NaN       NaN  \n",
       "1           5.0       4.0  \n",
       "2           5.0       2.0  \n",
       "3           3.0       3.0  \n",
       "4           6.0       5.0  \n",
       "5           3.0       3.0  \n",
       "6           6.0       3.0  \n",
       "7           5.0       2.0  \n",
       "8           4.0       3.0  \n",
       "9           3.0       1.0  \n",
       "10          6.0       5.0  \n",
       "11          NaN       NaN  \n",
       "12          6.0       4.0  \n",
       "13          6.0       4.0  \n",
       "14          NaN       NaN  \n",
       "15          8.0       5.0  \n",
       "16          2.0       1.0  \n",
       "17          NaN       NaN  \n",
       "18          4.0       3.0  \n",
       "19          6.0       3.0  \n",
       "\n",
       "[20 rows x 85 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias.head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.1.2: Assess Missing Data in Each Column\n",
    "\n",
    "How much missing data is present in each column? There are a few columns that are outliers in terms of the proportion of values that are missing. You will want to use matplotlib's [`hist()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html) function to visualize the distribution of missing value counts to find these columns. Identify and document these columns. While some of these columns might have justifications for keeping or re-encoding the data, for this project you should just remove them from the dataframe. (Feel free to make remarks about these outlier columns in the discussion, however!)\n",
    "\n",
    "For the remaining features, are there any patterns in which columns have, or share, missing data?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Rather than using a histogram which is cumbersome to plot, we can achieve the same result by finding how many missing values make up the entire column. We can sort this in descending order and we can see which columns have the most missing values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Perform an assessment of how much missing data there is in each column of the\n",
    "# dataset.\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "null_col_count = azdias.isnull().sum(axis=0)\n",
    "# print(null_col_count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax_rows = azdias.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def findTotal(value, total):\n",
    "    return value/total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "anomalies = null_col_count.apply(findTotal, total=ax_rows).sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "There are six columns that I would consdier to be anomalies in terms of missing values, they are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TITEL_KZ        0.997576\n",
       "AGER_TYP        0.769554\n",
       "KK_KUNDENTYP    0.655967\n",
       "KBA05_BAUMAX    0.534687\n",
       "GEBURTSJAHR     0.440203\n",
       "ALTER_HH        0.348137\n",
       "dtype: float64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "anomalies[0:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can drop these columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdias.drop(['TITEL_KZ', 'AGER_TYP', 'KK_KUNDENTYP', 'KBA05_BAUMAX', 'GEBURTSJAHR', 'ALTER_HH'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert azdias_shape[1]-6 == azdias.shape[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "To find patterns we can use seaborn heatmap with pd.isnull()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "79"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fb35ec8e780>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 113616 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35ed3bda0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplots(figsize=(20,15))\n",
    "sns.heatmap(azdias.iloc[:,0:30].isnull(), cbar=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fb35e63fe80>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 115328 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35e5a37f0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplots(figsize=(20,15))\n",
    "sns.heatmap(azdias.iloc[:,30:60].isnull(), cbar=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fb35dcf23c8>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 101088 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35dd53828>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplots(figsize=(20,15))\n",
    "sns.heatmap(azdias.iloc[:,60:80].isnull(), cbar=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total no. of columns with pattern in nan values: 47\n",
      "Total no. of columns without pattern in nan values: 32\n"
     ]
    }
   ],
   "source": [
    "# Remove the outlier columns from the dataset. (You'll perform other data\n",
    "# engineering tasks such as re-encoding and imputation later.)\n",
    "\n",
    "columnList = azdias.columns.values\n",
    "columnPatternIndexes = [12, 13, 14, 15 ,16 , 19, 20, 36, 37, 38, 40, 41, 43, 44, 46,\n",
    "                        46, 47, 48, 49, 50 ,51, 52, 53, 54, 55, 56, 57, 58, 59, 60,\n",
    "                        61, 62, 63, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,\n",
    "                        77, 78]\n",
    "print(f'Total no. of columns with pattern in nan values: {len(columnPatternIndexes)}')\n",
    "print(f'Total no. of columns without pattern in nan values: {len(columnList)-len(columnPatternIndexes)}')\n",
    "# print(f'{anomalies[6:]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "#### Discussion 1.1.2: Assess Missing Data in Each Column\n",
    "\n",
    "In total I found 6 columns that I determined to have unusually high levels of nan values for the data and as such I dropped them from the dataframe.\n",
    "\n",
    "I found 47 columns that appear to have a pattern in missing data. We can see that this pattern is fairly consistent throughout the dataset. \n",
    "\n",
    "**There are a lot of columns in this data set**, however looking through the data dictionary I can see that many of the categories are of the form:\n",
    "\n",
    "N (detailed scale) or (rough scale)\n",
    "\n",
    "where N could be anything from Wealth status to family. As the data is looking at regional areas, it makes sense that if data is missing for one area, it would be missing for all the other categories as well.\n",
    "\n",
    "For example, PLZ8_ANTG1, PLZ8_ANTG2 and PLZ8_ANTG3 all describe the number of family houses in the PLZ8 region by size of family (1-2 people, 3-5 people etc). If this information is missing in PLZ8_ANTG1, then it makes sense that is also missing in the same region for the other categories. **This could explain why we see patterns throughout the entire dataset**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.1.3: Assess Missing Data in Each Row\n",
    "\n",
    "Now, you'll perform a similar assessment for the rows of the dataset. How much data is missing in each row? As with the columns, you should see some groups of points that have a very different numbers of missing values. Divide the data into two subsets: one for data points that are above some threshold for missing values, and a second subset for points below that threshold.\n",
    "\n",
    "In order to know what to do with the outlier rows, we should see if the distribution of data values on columns that are not missing data (or are missing very little data) are similar or different between the two groups. Select at least five of these columns and compare the distribution of values.\n",
    "- You can use seaborn's [`countplot()`](https://seaborn.pydata.org/generated/seaborn.countplot.html) function to create a bar chart of code frequencies and matplotlib's [`subplot()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot.html) function to put bar charts for the two subplots side by side.\n",
    "- To reduce repeated code, you might want to write a function that can perform this comparison, taking as one of its arguments a column to be compared.\n",
    "\n",
    "Depending on what you observe in your comparison, this will have implications on how you approach your conclusions later in the analysis. If the distributions of non-missing features look similar between the data with many missing values and the data with few or no missing values, then we could argue that simply dropping those points from the analysis won't present a major issue. On the other hand, if the data with many missing values looks very different from the data with few or no missing values, then we should make a note on those data as special. We'll revisit these data later on. **Either way, you should continue your analysis for now using just the subset of the data with few or no missing values.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "We can repeat what we did above but for the rows. I will transpose the dataframe and repeat the same steps as before."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Testing ignore this cell\n",
    "\n",
    "# How much data is missing in each row of the dataset?\n",
    "null_row_count = azdias.isnull().sum(axis=1)\n",
    "null_row_count = pd.DataFrame(null_row_count)\n",
    "null_row_count.columns = ['null_count']\n",
    "# null_row_count.sample(frac=1).head(30)\n",
    "# null_row_count.groupby('null_count').agg({'null_count': 'count'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "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>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>891211</th>\n",
       "      <th>891212</th>\n",
       "      <th>891213</th>\n",
       "      <th>891214</th>\n",
       "      <th>891215</th>\n",
       "      <th>891216</th>\n",
       "      <th>891217</th>\n",
       "      <th>891218</th>\n",
       "      <th>891219</th>\n",
       "      <th>891220</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 891221 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     0      1      2      3      4      5      6      7       \\\n",
       "ALTERSKATEGORIE_GROB      2      1      3      4      3      1      2      1   \n",
       "ANREDE_KZ                 1      2      2      2      1      2      2      1   \n",
       "\n",
       "                     8      9       ...   891211 891212 891213 891214 891215  \\\n",
       "ALTERSKATEGORIE_GROB      3      3  ...        3      4      4      1      2   \n",
       "ANREDE_KZ                 1      2  ...        1      1      2      2      2   \n",
       "\n",
       "                     891216 891217 891218 891219 891220  \n",
       "ALTERSKATEGORIE_GROB      3      2      2      1      4  \n",
       "ANREDE_KZ                 2      1      2      1      1  \n",
       "\n",
       "[2 rows x 891221 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiasT = azdias.transpose()\n",
    "azdiasT.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "79\n"
     ]
    }
   ],
   "source": [
    "null_row_count = azdiasT.isnull().sum(axis=0)\n",
    "\n",
    "axT_rows = azdiasT.shape[0]\n",
    "print(axT_rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    43\n",
      "1     0\n",
      "2     0\n",
      "3     7\n",
      "4     0\n",
      "dtype: int64\n",
      "\n",
      "(891221,)\n"
     ]
    }
   ],
   "source": [
    "print(null_row_count.head(5))\n",
    "print()\n",
    "print(null_row_count.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "643174    0.620253\n",
       "732775    0.620253\n",
       "472919    0.607595\n",
       "183108    0.594937\n",
       "139316    0.594937\n",
       "dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "anomaliesT_f = null_row_count.apply(findTotal, total=axT_rows).sort_values(ascending=False)\n",
    "anomaliesT_f.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   0\n",
      "count  891221.000000\n",
      "mean        0.071518\n",
      "std         0.167528\n",
      "min         0.000000\n",
      "25%         0.000000\n",
      "50%         0.000000\n",
      "75%         0.037975\n",
      "max         0.620253\n"
     ]
    }
   ],
   "source": [
    "anomaliesT_f = pd.DataFrame(anomaliesT_f)\n",
    "# anomaliesT.iloc[0:5,0]\n",
    "print(anomaliesT_f.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "85% percentile: 0.1013\n",
      "86% percentile: 0.1013\n",
      "87% percentile: 0.1266\n",
      "88% percentile: 0.1646\n",
      "89% percentile: 0.2025\n",
      "90% percentile: 0.4304\n",
      "91% percentile: 0.4304\n",
      "\n",
      "Limit is 0.43037974683544306\n"
     ]
    }
   ],
   "source": [
    "for i in range(85, 92):\n",
    "    print(f'{i}% percentile: {anomaliesT_f.quantile(q=i*0.01)[0]:.4f}')\n",
    "limit = anomaliesT_f.quantile(q=0.9)[0]\n",
    "print(f'\\nLimit is {limit}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "As there is a much larger jump from the 89th to 90th percentile (a factor of around 113%) I would say our threshold for the split for the rows should be those which have 43% or higher missing values of the total data in the top category (denoted as anomaliesU), and those that are less than 43% in the bottom category (denoted as anomaliesL)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({0: 623209, 47: 45578, 2: 27926, 43: 27369, 8: 24592, 5: 22515, 3: 17629, 1: 15738, 6: 13771, 7: 13714, 4: 12607, 34: 10816, 10: 5410, 15: 4743, 35: 3911, 13: 3255, 9: 3042, 33: 2985, 16: 2505, 14: 2243, 19: 1180, 11: 1127, 12: 766, 17: 677, 37: 538, 45: 494, 18: 428, 38: 421, 41: 356, 20: 349, 32: 206, 44: 155, 21: 150, 40: 137, 23: 132, 22: 129, 36: 84, 39: 77, 24: 69, 26: 59, 25: 55, 27: 24, 42: 21, 29: 12, 30: 6, 28: 5, 31: 3, 49: 2, 48: 1})\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "print(collections.Counter(null_row_count))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "We will now split the dataframe into 2 categories, and then compare the columns of the original matrix like before"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  0\n",
      "count  89960.000000\n",
      "mean       0.550983\n",
      "std        0.057724\n",
      "min        0.430380\n",
      "25%        0.544304\n",
      "50%        0.594937\n",
      "75%        0.594937\n",
      "max        0.620253\n"
     ]
    }
   ],
   "source": [
    "# Write code to divide the data into two subsets based on the number of missing\n",
    "# values in each row.a\n",
    "anomaliesU = anomaliesT_f[(anomaliesT_f>=limit)]\n",
    "anomaliesU.dropna(inplace=True)\n",
    "print(anomaliesU.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   0\n",
      "count  801261.000000\n",
      "mean        0.017687\n",
      "std         0.046201\n",
      "min         0.000000\n",
      "25%         0.000000\n",
      "50%         0.000000\n",
      "75%         0.000000\n",
      "max         0.417722\n"
     ]
    }
   ],
   "source": [
    "anomaliesL = anomaliesT_f[(anomaliesT_f<limit)]\n",
    "anomaliesL.dropna(inplace=True)\n",
    "print(anomaliesL.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "We check that our dataframes match the original row totals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert ((anomaliesL.shape[0] + anomaliesU.shape[0]) == azdias.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We have droped 10% of rows\n"
     ]
    }
   ],
   "source": [
    "print(f'We have droped {100*anomaliesU.shape[0]/azdias.shape[0]:.0f}% of rows')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "As we took the 90th percentile, this confirms we have dropped the right amount. I am unsure at this stage if we have dropped too much. For the sake of the project I will commit to the values I initially chose, and only revise this later if we see a huge detriment to the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# anomaliesT.iloc[anomaliesU.index]\n",
    "anomaliesU = azdias.iloc[anomaliesU.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(89960, 79)\n",
      "        ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
      "455227                   2.0          2            1.0                  3   \n",
      "7661                     1.0          2            4.0                  1   \n",
      "581535                   4.0          2            5.0                  4   \n",
      "523278                   2.0          1            4.0                  5   \n",
      "109567                   2.0          2            6.0                  3   \n",
      "545797                   3.0          1            5.0                  5   \n",
      "473310                   3.0          2            6.0                  3   \n",
      "246673                   1.0          2            6.0                  3   \n",
      "747422                   3.0          1            4.0                  3   \n",
      "765381                   2.0          2            6.0                  3   \n",
      "\n",
      "        FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
      "455227              4                 3               5   \n",
      "7661                5                 3               5   \n",
      "581535              1                 5               2   \n",
      "523278              3                 4               3   \n",
      "109567              4                 3               5   \n",
      "545797              2                 4               3   \n",
      "473310              4                 3               5   \n",
      "246673              4                 3               5   \n",
      "747422              4                 3               5   \n",
      "765381              4                 3               5   \n",
      "\n",
      "        FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  FINANZTYP    ...     \\\n",
      "455227                      5                 3          4    ...      \n",
      "7661                        5                 3          1    ...      \n",
      "581535                      2                 4          2    ...      \n",
      "523278                      3                 1          3    ...      \n",
      "109567                      5                 3          4    ...      \n",
      "545797                      3                 1          3    ...      \n",
      "473310                      5                 3          4    ...      \n",
      "246673                      5                 3          4    ...      \n",
      "747422                      5                 3          4    ...      \n",
      "765381                      5                 3          4    ...      \n",
      "\n",
      "        PLZ8_ANTG1  PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  \\\n",
      "455227         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "7661           NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "581535         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "523278         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "109567         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "545797         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "473310         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "246673         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "747422         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "765381         NaN         NaN         NaN         NaN          NaN       NaN   \n",
      "\n",
      "        PLZ8_GBZ  ARBEIT  ORTSGR_KLS9  RELAT_AB  \n",
      "455227       NaN     NaN          NaN       NaN  \n",
      "7661         NaN     NaN          NaN       NaN  \n",
      "581535       NaN     NaN          NaN       NaN  \n",
      "523278       NaN     NaN          NaN       NaN  \n",
      "109567       NaN     NaN          NaN       NaN  \n",
      "545797       NaN     NaN          NaN       NaN  \n",
      "473310       NaN     NaN          NaN       NaN  \n",
      "246673       NaN     NaN          NaN       NaN  \n",
      "747422       NaN     NaN          NaN       NaN  \n",
      "765381       NaN     NaN          NaN       NaN  \n",
      "\n",
      "[10 rows x 79 columns]\n"
     ]
    }
   ],
   "source": [
    "print(anomaliesU.shape)\n",
    "print(anomaliesU.sample(frac=True).head(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "anomaliesL = azdias.iloc[anomaliesL.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(801261, 79)\n",
      "        ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
      "553242                   4.0          1            1.0                  3   \n",
      "169994                   2.0          1            3.0                  4   \n",
      "14476                    4.0          1            5.0                  5   \n",
      "686628                   3.0          1            4.0                  4   \n",
      "477351                   3.0          2            4.0                  4   \n",
      "749545                   1.0          2            3.0                  1   \n",
      "559934                   4.0          1            2.0                  4   \n",
      "760686                   3.0          1            4.0                  2   \n",
      "395366                   3.0          2            3.0                  1   \n",
      "568884                   3.0          2            6.0                  5   \n",
      "\n",
      "        FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
      "553242              1                 5               1   \n",
      "169994              2                 3               3   \n",
      "14476               1                 5               1   \n",
      "686628              3                 4               2   \n",
      "477351              2                 3               3   \n",
      "749545              5                 3               5   \n",
      "559934              2                 3               4   \n",
      "760686              4                 1               3   \n",
      "395366              5                 2               5   \n",
      "568884              2                 3               4   \n",
      "\n",
      "        FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  FINANZTYP    ...     \\\n",
      "553242                      1                 5          6    ...      \n",
      "169994                      2                 2          6    ...      \n",
      "14476                       1                 2          6    ...      \n",
      "686628                      4                 3          5    ...      \n",
      "477351                      2                 2          6    ...      \n",
      "749545                      5                 5          1    ...      \n",
      "559934                      1                 3          6    ...      \n",
      "760686                      4                 3          4    ...      \n",
      "395366                      2                 5          1    ...      \n",
      "568884                      2                 1          6    ...      \n",
      "\n",
      "        PLZ8_ANTG1  PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  \\\n",
      "553242         1.0         3.0         3.0         2.0          4.0       4.0   \n",
      "169994         1.0         4.0         3.0         1.0          3.0       4.0   \n",
      "14476          3.0         2.0         1.0         0.0          1.0       4.0   \n",
      "686628         3.0         3.0         1.0         1.0          1.0       4.0   \n",
      "477351         2.0         3.0         1.0         1.0          1.0       4.0   \n",
      "749545         2.0         3.0         1.0         0.0          1.0       5.0   \n",
      "559934         1.0         4.0         3.0         2.0          4.0       4.0   \n",
      "760686         1.0         4.0         2.0         1.0          2.0       5.0   \n",
      "395366         1.0         3.0         3.0         2.0          5.0       5.0   \n",
      "568884         2.0         3.0         1.0         0.0          1.0       3.0   \n",
      "\n",
      "        PLZ8_GBZ  ARBEIT  ORTSGR_KLS9  RELAT_AB  \n",
      "553242       2.0     4.0          9.0       3.0  \n",
      "169994       3.0     4.0          4.0       5.0  \n",
      "14476        5.0     2.0          2.0       1.0  \n",
      "686628       4.0     2.0          5.0       1.0  \n",
      "477351       4.0     1.0          3.0       1.0  \n",
      "749545       5.0     1.0          3.0       1.0  \n",
      "559934       2.0     4.0          7.0       3.0  \n",
      "760686       4.0     4.0          9.0       3.0  \n",
      "395366       3.0     4.0          9.0       3.0  \n",
      "568884       3.0     3.0          5.0       2.0  \n",
      "\n",
      "[10 rows x 79 columns]\n"
     ]
    }
   ],
   "source": [
    "print(anomaliesL.shape)\n",
    "print(anomaliesL.sample(frac=True).head(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "As directed, we will now look at collumns against these two groups. Recall that we have columnPatternIndexes as a list of columns that have a lot of missing values from before, we can drop these columns and sample at random against the remaining columns to see if we can see anything interesting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of cols to drop: 47\n",
      "Number of cols kept: 33\n"
     ]
    },
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>...</th>\n",
       "      <th>SEMIO_RAT</th>\n",
       "      <th>SEMIO_KRIT</th>\n",
       "      <th>SEMIO_DOM</th>\n",
       "      <th>SEMIO_KAEM</th>\n",
       "      <th>SEMIO_PFLICHT</th>\n",
       "      <th>SEMIO_TRADV</th>\n",
       "      <th>ZABEOTYP</th>\n",
       "      <th>HH_EINKOMMEN_SCORE</th>\n",
       "      <th>ANZ_HAUSHALTE_AKTIV</th>\n",
       "      <th>ONLINE_AFFINITAET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>830954</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>829381</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>841875</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>848175</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>818489</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>215572</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83951</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>284735</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>258200</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388735</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
       "830954                   1.0          2            3.0                  1   \n",
       "829381                   3.0          2            1.0                  4   \n",
       "841875                   1.0          1            4.0                  2   \n",
       "848175                   2.0          1            3.0                  4   \n",
       "818489                   1.0          2            4.0                  3   \n",
       "215572                   1.0          1            4.0                  2   \n",
       "83951                    3.0          1            5.0                  5   \n",
       "284735                   2.0          1            4.0                  4   \n",
       "258200                   1.0          2            6.0                  2   \n",
       "388735                   3.0          1            6.0                  5   \n",
       "\n",
       "        FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "830954              5                 3               5   \n",
       "829381              2                 4               4   \n",
       "841875              5                 3               5   \n",
       "848175              4                 2               4   \n",
       "818489              4                 2               5   \n",
       "215572              5                 3               5   \n",
       "83951               3                 4               3   \n",
       "284735              4                 2               4   \n",
       "258200              5                 3               5   \n",
       "388735              2                 5               2   \n",
       "\n",
       "        FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  FINANZTYP        ...          \\\n",
       "830954                      5                 3          1        ...           \n",
       "829381                      3                 1          3        ...           \n",
       "841875                      5                 2          1        ...           \n",
       "848175                      5                 1          3        ...           \n",
       "818489                      5                 2          4        ...           \n",
       "215572                      5                 2          1        ...           \n",
       "83951                       3                 1          3        ...           \n",
       "284735                      5                 1          3        ...           \n",
       "258200                      5                 4          1        ...           \n",
       "388735                      3                 2          2        ...           \n",
       "\n",
       "        SEMIO_RAT  SEMIO_KRIT  SEMIO_DOM  SEMIO_KAEM  SEMIO_PFLICHT  \\\n",
       "830954          6           7          6           6              5   \n",
       "829381          4           7          6           7              4   \n",
       "841875          4           1          2           4              5   \n",
       "848175          5           1          2           2              5   \n",
       "818489          6           7          6           6              5   \n",
       "215572          5           1          2           4              5   \n",
       "83951           5           3          2           3              4   \n",
       "284735          4           1          2           2              5   \n",
       "258200          6           7          6           6              5   \n",
       "388735          5           3          2           3              4   \n",
       "\n",
       "        SEMIO_TRADV  ZABEOTYP  HH_EINKOMMEN_SCORE  ANZ_HAUSHALTE_AKTIV  \\\n",
       "830954            6         5                 NaN                  NaN   \n",
       "829381            3         3                 NaN                  NaN   \n",
       "841875            5         1                 2.0                  NaN   \n",
       "848175            5         5                 2.0                  NaN   \n",
       "818489            6         5                 NaN                  NaN   \n",
       "215572            5         5                 NaN                  NaN   \n",
       "83951             4         6                 NaN                  NaN   \n",
       "284735            5         5                 NaN                  NaN   \n",
       "258200            6         4                 NaN                  NaN   \n",
       "388735            4         3                 NaN                  NaN   \n",
       "\n",
       "        ONLINE_AFFINITAET  \n",
       "830954                3.0  \n",
       "829381                4.0  \n",
       "841875                3.0  \n",
       "848175                2.0  \n",
       "818489                5.0  \n",
       "215572                4.0  \n",
       "83951                 5.0  \n",
       "284735                4.0  \n",
       "258200                3.0  \n",
       "388735                4.0  \n",
       "\n",
       "[10 rows x 33 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Compare the distribution of values for at least five columns where there are\n",
    "# no or few missing values, between the two subsets.\n",
    "print(f'Number of cols to drop: {len(columnPatternIndexes)}')\n",
    "anomaliesL_compare = anomaliesL.drop(anomaliesL.iloc[:,columnPatternIndexes], axis=1)\n",
    "print(f'Number of cols kept: {anomaliesL_compare.shape[1]}')\n",
    "anomaliesL_compare.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "def dataComparison(df):\n",
    "    cols = random.sample(list(df.columns.values), 5)\n",
    "    f, axes = plt.subplots(1, 5, figsize=(25,4))\n",
    "    for i in range(0, 5):\n",
    "        sns.countplot(x=cols[i], data=df.fillna('Missing'), ax=axes[i])  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "Let's look at the data distribution for the Lower group (those that don't have many zero values across the rows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 30812 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35dab8748>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31408 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1a5ace518>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31692 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1abbba940>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 28676 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb26b93edd8>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 31020 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb269cd8160>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import itertools\n",
    "\n",
    "for _ in itertools.repeat(None, 5):\n",
    "    dataComparison(anomaliesL_compare)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "Now the same for the upper group"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of cols to drop: 47\n",
      "Number of cols kept: 33\n"
     ]
    },
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>...</th>\n",
       "      <th>SEMIO_RAT</th>\n",
       "      <th>SEMIO_KRIT</th>\n",
       "      <th>SEMIO_DOM</th>\n",
       "      <th>SEMIO_KAEM</th>\n",
       "      <th>SEMIO_PFLICHT</th>\n",
       "      <th>SEMIO_TRADV</th>\n",
       "      <th>ZABEOTYP</th>\n",
       "      <th>HH_EINKOMMEN_SCORE</th>\n",
       "      <th>ANZ_HAUSHALTE_AKTIV</th>\n",
       "      <th>ONLINE_AFFINITAET</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>643174</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>732775</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>472919</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>183108</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139316</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>691141</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>691142</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>691171</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>691183</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>139332</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
       "643174                   3.0          1            NaN                  2   \n",
       "732775                   3.0          2            NaN                  3   \n",
       "472919                   1.0          1            NaN                  5   \n",
       "183108                   1.0          2            6.0                  3   \n",
       "139316                   3.0          2            6.0                  3   \n",
       "691141                   1.0          1            6.0                  3   \n",
       "691142                   3.0          2            6.0                  3   \n",
       "691171                   3.0          1            6.0                  3   \n",
       "691183                   3.0          1            6.0                  3   \n",
       "139332                   3.0          1            6.0                  3   \n",
       "\n",
       "        FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "643174              5                 3               5   \n",
       "732775              5                 3               5   \n",
       "472919              1                 5               2   \n",
       "183108              4                 3               5   \n",
       "139316              4                 3               5   \n",
       "691141              4                 3               5   \n",
       "691142              4                 3               5   \n",
       "691171              4                 3               5   \n",
       "691183              4                 3               5   \n",
       "139332              4                 3               5   \n",
       "\n",
       "        FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  FINANZTYP        ...          \\\n",
       "643174                      5                 2          4        ...           \n",
       "732775                      5                 2          4        ...           \n",
       "472919                      3                 2          2        ...           \n",
       "183108                      5                 3          4        ...           \n",
       "139316                      5                 3          4        ...           \n",
       "691141                      5                 3          4        ...           \n",
       "691142                      5                 3          4        ...           \n",
       "691171                      5                 3          4        ...           \n",
       "691183                      5                 3          4        ...           \n",
       "139332                      5                 3          4        ...           \n",
       "\n",
       "        SEMIO_RAT  SEMIO_KRIT  SEMIO_DOM  SEMIO_KAEM  SEMIO_PFLICHT  \\\n",
       "643174          5           4          4           4              5   \n",
       "732775          4           7          6           6              5   \n",
       "472919          4           4          4           4              5   \n",
       "183108          4           7          6           6              5   \n",
       "139316          4           7          6           6              5   \n",
       "691141          4           7          6           6              5   \n",
       "691142          4           7          6           6              5   \n",
       "691171          4           7          6           6              5   \n",
       "691183          4           7          6           6              5   \n",
       "139332          4           7          6           6              5   \n",
       "\n",
       "        SEMIO_TRADV  ZABEOTYP  HH_EINKOMMEN_SCORE  ANZ_HAUSHALTE_AKTIV  \\\n",
       "643174            4         4                 NaN                  NaN   \n",
       "732775            3         3                 NaN                  NaN   \n",
       "472919            4         6                 NaN                  NaN   \n",
       "183108            3         3                 2.0                  NaN   \n",
       "139316            3         3                 2.0                  NaN   \n",
       "691141            3         3                 2.0                  NaN   \n",
       "691142            3         3                 2.0                  NaN   \n",
       "691171            3         3                 2.0                  NaN   \n",
       "691183            3         3                 2.0                  NaN   \n",
       "139332            3         3                 2.0                  NaN   \n",
       "\n",
       "        ONLINE_AFFINITAET  \n",
       "643174                NaN  \n",
       "732775                NaN  \n",
       "472919                NaN  \n",
       "183108                2.0  \n",
       "139316                2.0  \n",
       "691141                2.0  \n",
       "691142                2.0  \n",
       "691171                2.0  \n",
       "691183                2.0  \n",
       "139332                2.0  \n",
       "\n",
       "[10 rows x 33 columns]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(f'Number of cols to drop: {len(columnPatternIndexes)}')\n",
    "anomaliesU_compare = anomaliesU.drop(anomaliesU.iloc[:,columnPatternIndexes], axis=1)\n",
    "print(f'Number of cols kept: {anomaliesU_compare.shape[1]}')\n",
    "anomaliesU_compare.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22600 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35db3a898>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 22796 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb194292b70>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 24176 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1a6509400>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 26336 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1a63f4be0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 24956 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb19ea744e0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "for _ in itertools.repeat(None, 5):\n",
    "    dataComparison(anomaliesU_compare)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Discussion 1.1.3: Assess Missing Data in Each Row\n",
    "\n",
    "There is a huge distance that is faily easy to visualise from the above graphs. We can see that the group that is below the threshold looks very reasonable. Without doing any specific statistical analysis we can see that across most columns, the data is fairly evenly spread. With a good mix between the columns - the point to take away is across the columns, the data doesn't look to follow a pattern. \n",
    "\n",
    "\n",
    "Looking at the group which contains the rows above the threshold we can see immediately the data is largely dominated by a single value in each column. Looking at the column ANZ_HAUSHALTE_AKTIV we can see that this column is dominated with missing values exlusively from this rows. This means that all the information we have for this column will come from the other set of rows.\n",
    "\n",
    "This is a very interesting factor in our data. If we think about it, if a row has a large number of missing values, then there must be only a few cells in that row that contain the information. If we take the columns that we know contain mostly data and not many nan values (as we have exlucded both of these earlier on), then we can see that these rows account for the dominating value. \n",
    "\n",
    "We can see this from the above graph where SEMIO_Lust is dominated with the value 5 in these upper rows, whereas in the lower group SEMIO_Lust is fairly evenly spread out, with 6, 7 being the two highest values and 5 actually being second from the bottom."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1.2: Select and Re-Encode Features\n",
    "\n",
    "Checking for missing data isn't the only way in which you can prepare a dataset for analysis. Since the unsupervised learning techniques to be used will only work on data that is encoded numerically, you need to make a few encoding changes or additional assumptions to be able to make progress. In addition, while almost all of the values in the dataset are encoded using numbers, not all of them represent numeric values. Check the third column of the feature summary (`feat_info`) for a summary of types of measurement.\n",
    "- For numeric and interval data, these features can be kept without changes.\n",
    "- Most of the variables in the dataset are ordinal in nature. While ordinal values may technically be non-linear in spacing, make the simplifying assumption that the ordinal variables can be treated as being interval in nature (that is, kept without any changes).\n",
    "- Special handling may be necessary for the remaining two variable types: categorical, and 'mixed'.\n",
    "\n",
    "In the first two parts of this sub-step, you will perform an investigation of the categorical and mixed-type features and make a decision on each of them, whether you will keep, drop, or re-encode each. Then, in the last part, you will create a new data frame with only the selected and engineered columns.\n",
    "\n",
    "Data wrangling is often the trickiest part of the data analysis process, and there's a lot of it to be done here. But stick with it: once you're done with this step, you'll be ready to get to the machine learning parts of the project!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'categorical': 21,\n",
       "         'ordinal': 49,\n",
       "         'numeric': 7,\n",
       "         'mixed': 7,\n",
       "         'interval': 1})"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# How many features are there of each data type?\n",
    "collections.Counter(feat_info['type'].values)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.2.1: Re-Encode Categorical Features\n",
    "\n",
    "For categorical data, you would ordinarily need to encode the levels as dummy variables. Depending on the number of categories, perform one of the following:\n",
    "- For binary (two-level) categoricals that take numeric values, you can keep them without needing to do anything.\n",
    "- There is one binary variable that takes on non-numeric values. For this one, you need to re-encode the values as numbers or create a dummy variable.\n",
    "- For multi-level categoricals (three or more values), you can choose to encode the values using multiple dummy variables (e.g. via [OneHotEncoder](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html)), or (to keep things straightforward) just drop them from the analysis. As always, document your choices in the Discussion section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "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>attribute</th>\n",
       "      <th>information_level</th>\n",
       "      <th>type</th>\n",
       "      <th>missing_or_unknown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AGER_TYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ANREDE_KZ</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CJT_GESAMTTYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>FINANZTYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>GFK_URLAUBERTYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>GREEN_AVANTGARDE</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>LP_FAMILIE_FEIN</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>LP_FAMILIE_GROB</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>LP_STATUS_FEIN</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>LP_STATUS_GROB</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>NATIONALITAET_KZ</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>SHOPPER_TYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>SOHO_KZ</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>TITEL_KZ</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>VERS_TYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>ZABEOTYP</td>\n",
       "      <td>person</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,9]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>KK_KUNDENTYP</td>\n",
       "      <td>household</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>GEBAEUDETYP</td>\n",
       "      <td>building</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>OST_WEST_KZ</td>\n",
       "      <td>building</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>CAMEO_DEUG_2015</td>\n",
       "      <td>microcell_rr4</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[-1,X]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>CAMEO_DEU_2015</td>\n",
       "      <td>microcell_rr4</td>\n",
       "      <td>categorical</td>\n",
       "      <td>[XX]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           attribute information_level         type missing_or_unknown\n",
       "0           AGER_TYP            person  categorical             [-1,0]\n",
       "2          ANREDE_KZ            person  categorical             [-1,0]\n",
       "3      CJT_GESAMTTYP            person  categorical                [0]\n",
       "10         FINANZTYP            person  categorical               [-1]\n",
       "12   GFK_URLAUBERTYP            person  categorical                 []\n",
       "13  GREEN_AVANTGARDE            person  categorical                 []\n",
       "17   LP_FAMILIE_FEIN            person  categorical                [0]\n",
       "18   LP_FAMILIE_GROB            person  categorical                [0]\n",
       "19    LP_STATUS_FEIN            person  categorical                [0]\n",
       "20    LP_STATUS_GROB            person  categorical                [0]\n",
       "21  NATIONALITAET_KZ            person  categorical             [-1,0]\n",
       "38       SHOPPER_TYP            person  categorical               [-1]\n",
       "39           SOHO_KZ            person  categorical               [-1]\n",
       "40          TITEL_KZ            person  categorical             [-1,0]\n",
       "41          VERS_TYP            person  categorical               [-1]\n",
       "42          ZABEOTYP            person  categorical             [-1,9]\n",
       "47      KK_KUNDENTYP         household  categorical               [-1]\n",
       "52       GEBAEUDETYP          building  categorical             [-1,0]\n",
       "55       OST_WEST_KZ          building  categorical               [-1]\n",
       "57   CAMEO_DEUG_2015     microcell_rr4  categorical             [-1,X]\n",
       "58    CAMEO_DEU_2015     microcell_rr4  categorical               [XX]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Assess categorical variables: which are binary, which are multi-level, and\n",
    "# which one needs to be re-encoded?\n",
    "feat_info_cat = feat_info[(feat_info['type']=='categorical')]\n",
    "feat_info_cat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "We pick the attribute column, which gives us the columns for our data.\n",
    "\n",
    "We need to remember to drop the columns from this list that we removed at the start of the project, as they were mostly missing values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ANREDE_KZ', 'CJT_GESAMTTYP', 'FINANZTYP', 'GFK_URLAUBERTYP', 'GREEN_AVANTGARDE', 'LP_FAMILIE_FEIN', 'LP_FAMILIE_GROB', 'LP_STATUS_FEIN', 'LP_STATUS_GROB', 'NATIONALITAET_KZ', 'SHOPPER_TYP', 'SOHO_KZ', 'VERS_TYP', 'ZABEOTYP', 'GEBAEUDETYP', 'OST_WEST_KZ', 'CAMEO_DEUG_2015', 'CAMEO_DEU_2015']\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "categorical_cols = feat_info_cat['attribute'].tolist()\n",
    "\n",
    "drop_cols = ['AGER_TYP', 'TITEL_KZ', 'KK_KUNDENTYP']\n",
    "\n",
    "for i in drop_cols:\n",
    "    categorical_cols.remove(i)\n",
    "    \n",
    "print(categorical_cols)\n",
    "print(len(categorical_cols))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "We will drop from this list the columns that contain just two values. **Note I am dropping them if the length of their unique values is equal to 2. This is because we know that we do not have any columns that have a length of two with non numeric values. This method would not extend to a case where we have 2 non-numeric values**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ANREDE_KZ\n",
      "Values: [1 2]\n",
      "Length: 2\n",
      "FINANZTYP\n",
      "Values: [4 1 6 5 2 3]\n",
      "Length: 6\n",
      "GFK_URLAUBERTYP\n",
      "Values: [ 10.   1.   5.  12.   9.   3.   8.  11.   4.   2.   7.   6.  nan]\n",
      "Length: 13\n",
      "GREEN_AVANTGARDE\n",
      "Values: [0 1]\n",
      "Length: 2\n",
      "LP_FAMILIE_GROB\n",
      "Values: [  2.   3.   1.  nan   5.   4.]\n",
      "Length: 6\n",
      "LP_STATUS_FEIN\n",
      "Values: [  1.   2.   3.   9.   4.  10.   5.   8.   6.   7.  nan]\n",
      "Length: 11\n",
      "LP_STATUS_GROB\n",
      "Values: [  1.   2.   4.   5.   3.  nan]\n",
      "Length: 6\n",
      "NATIONALITAET_KZ\n",
      "Values: [ nan   1.   3.   2.]\n",
      "Length: 4\n",
      "SHOPPER_TYP\n",
      "Values: [ nan   3.   2.   1.   0.]\n",
      "Length: 5\n",
      "SOHO_KZ\n",
      "Values: [ nan   1.   0.]\n",
      "Length: 3\n",
      "VERS_TYP\n",
      "Values: [ nan   2.   1.]\n",
      "Length: 3\n",
      "ZABEOTYP\n",
      "Values: [3 5 4 1 6 2]\n",
      "Length: 6\n",
      "GEBAEUDETYP\n",
      "Values: [ nan   8.   1.   3.   2.   6.   4.   5.]\n",
      "Length: 8\n",
      "OST_WEST_KZ\n",
      "Values: [nan 'W' 'O']\n",
      "Length: 3\n",
      "CAMEO_DEUG_2015\n",
      "Values: [nan '8' '4' '2' '6' '1' '9' '5' '7' '3']\n",
      "Length: 10\n",
      "CAMEO_DEU_2015\n",
      "Values: [nan '8A' '4C' '2A' '6B' '8C' '4A' '2D' '1A' '1E' '9D' '5C' '8B' '7A' '5D'\n",
      " '9E' '9B' '1B' '3D' '4E' '4B' '3C' '5A' '7B' '9A' '6D' '6E' '2C' '7C' '9C'\n",
      " '7D' '5E' '1D' '8D' '6C' '6A' '5B' '4D' '3A' '2B' '7E' '3B' '6F' '5F' '1C']\n",
      "Length: 45\n",
      "\n",
      " Columns to reencode as dummies: ['CJT_GESAMTTYP', 'FINANZTYP', 'GFK_URLAUBERTYP', 'LP_FAMILIE_FEIN', 'LP_FAMILIE_GROB', 'LP_STATUS_FEIN', 'LP_STATUS_GROB', 'NATIONALITAET_KZ', 'SHOPPER_TYP', 'SOHO_KZ', 'VERS_TYP', 'ZABEOTYP', 'GEBAEUDETYP', 'OST_WEST_KZ', 'CAMEO_DEUG_2015', 'CAMEO_DEU_2015']\n"
     ]
    }
   ],
   "source": [
    "# Re-encode categorical variable(s) to be kept in the analysis.\n",
    "for i in categorical_cols:\n",
    "    print(f'{i}\\nValues: {azdias[i].unique()}\\nLength: {len(azdias[i].unique())}')\n",
    "    if len(azdias[i].unique()) == 2:\n",
    "        categorical_cols.remove(i)\n",
    "print('\\n Columns to reencode as dummies:', categorical_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(categorical_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "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>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>GFK_URLAUBERTYP</th>\n",
       "      <th>LP_FAMILIE_FEIN</th>\n",
       "      <th>LP_FAMILIE_GROB</th>\n",
       "      <th>LP_STATUS_FEIN</th>\n",
       "      <th>LP_STATUS_GROB</th>\n",
       "      <th>NATIONALITAET_KZ</th>\n",
       "      <th>SHOPPER_TYP</th>\n",
       "      <th>SOHO_KZ</th>\n",
       "      <th>VERS_TYP</th>\n",
       "      <th>ZABEOTYP</th>\n",
       "      <th>GEBAEUDETYP</th>\n",
       "      <th>OST_WEST_KZ</th>\n",
       "      <th>CAMEO_DEUG_2015</th>\n",
       "      <th>CAMEO_DEU_2015</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>10.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>8.0</td>\n",
       "      <td>W</td>\n",
       "      <td>8</td>\n",
       "      <td>8A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1.0</td>\n",
       "      <td>W</td>\n",
       "      <td>4</td>\n",
       "      <td>4C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>6</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>W</td>\n",
       "      <td>2</td>\n",
       "      <td>2A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>W</td>\n",
       "      <td>6</td>\n",
       "      <td>6B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CJT_GESAMTTYP  FINANZTYP  GFK_URLAUBERTYP  LP_FAMILIE_FEIN  \\\n",
       "0            2.0          4             10.0              2.0   \n",
       "1            5.0          1             10.0              5.0   \n",
       "2            3.0          1             10.0              1.0   \n",
       "3            2.0          6              1.0              NaN   \n",
       "4            5.0          5              5.0             10.0   \n",
       "\n",
       "   LP_FAMILIE_GROB  LP_STATUS_FEIN  LP_STATUS_GROB  NATIONALITAET_KZ  \\\n",
       "0              2.0             1.0             1.0               NaN   \n",
       "1              3.0             2.0             1.0               1.0   \n",
       "2              1.0             3.0             2.0               1.0   \n",
       "3              NaN             9.0             4.0               1.0   \n",
       "4              5.0             3.0             2.0               1.0   \n",
       "\n",
       "   SHOPPER_TYP  SOHO_KZ  VERS_TYP  ZABEOTYP  GEBAEUDETYP OST_WEST_KZ  \\\n",
       "0          NaN      NaN       NaN         3          NaN         NaN   \n",
       "1          3.0      1.0       2.0         5          8.0           W   \n",
       "2          2.0      0.0       1.0         5          1.0           W   \n",
       "3          1.0      0.0       1.0         3          1.0           W   \n",
       "4          2.0      0.0       2.0         4          1.0           W   \n",
       "\n",
       "  CAMEO_DEUG_2015 CAMEO_DEU_2015  \n",
       "0             NaN            NaN  \n",
       "1               8             8A  \n",
       "2               4             4C  \n",
       "3               2             2A  \n",
       "4               6             6B  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias[categorical_cols].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "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>CJT_GESAMTTYP_1.0</th>\n",
       "      <th>CJT_GESAMTTYP_2.0</th>\n",
       "      <th>CJT_GESAMTTYP_3.0</th>\n",
       "      <th>CJT_GESAMTTYP_4.0</th>\n",
       "      <th>CJT_GESAMTTYP_5.0</th>\n",
       "      <th>CJT_GESAMTTYP_6.0</th>\n",
       "      <th>CJT_GESAMTTYP_nan</th>\n",
       "      <th>FINANZTYP_1</th>\n",
       "      <th>FINANZTYP_2</th>\n",
       "      <th>FINANZTYP_3</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_DEU_2015_8A</th>\n",
       "      <th>CAMEO_DEU_2015_8B</th>\n",
       "      <th>CAMEO_DEU_2015_8C</th>\n",
       "      <th>CAMEO_DEU_2015_8D</th>\n",
       "      <th>CAMEO_DEU_2015_9A</th>\n",
       "      <th>CAMEO_DEU_2015_9B</th>\n",
       "      <th>CAMEO_DEU_2015_9C</th>\n",
       "      <th>CAMEO_DEU_2015_9D</th>\n",
       "      <th>CAMEO_DEU_2015_9E</th>\n",
       "      <th>CAMEO_DEU_2015_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 148 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   CJT_GESAMTTYP_1.0  CJT_GESAMTTYP_2.0  CJT_GESAMTTYP_3.0  CJT_GESAMTTYP_4.0  \\\n",
       "0                  0                  1                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  1                  0   \n",
       "3                  0                  1                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CJT_GESAMTTYP_5.0  CJT_GESAMTTYP_6.0  CJT_GESAMTTYP_nan  FINANZTYP_1  \\\n",
       "0                  0                  0                  0            0   \n",
       "1                  1                  0                  0            1   \n",
       "2                  0                  0                  0            1   \n",
       "3                  0                  0                  0            0   \n",
       "4                  1                  0                  0            0   \n",
       "\n",
       "   FINANZTYP_2  FINANZTYP_3         ...          CAMEO_DEU_2015_8A  \\\n",
       "0            0            0         ...                          0   \n",
       "1            0            0         ...                          1   \n",
       "2            0            0         ...                          0   \n",
       "3            0            0         ...                          0   \n",
       "4            0            0         ...                          0   \n",
       "\n",
       "   CAMEO_DEU_2015_8B  CAMEO_DEU_2015_8C  CAMEO_DEU_2015_8D  CAMEO_DEU_2015_9A  \\\n",
       "0                  0                  0                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  0                  0   \n",
       "3                  0                  0                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CAMEO_DEU_2015_9B  CAMEO_DEU_2015_9C  CAMEO_DEU_2015_9D  CAMEO_DEU_2015_9E  \\\n",
       "0                  0                  0                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  0                  0   \n",
       "3                  0                  0                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CAMEO_DEU_2015_nan  \n",
       "0                   1  \n",
       "1                   0  \n",
       "2                   0  \n",
       "3                   0  \n",
       "4                   0  \n",
       "\n",
       "[5 rows x 148 columns]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias_cat_dummies = pd.get_dummies(azdias[categorical_cols].astype(str))\n",
    "azdias_cat_dummies.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Discussion 1.2.1: Re-Encode Categorical Features\n",
    "\n",
    "\n",
    "There were 18 of the type 'categorical' that we needed to work with. They were:\n",
    "\n",
    "['ANREDE_KZ', 'CJT_GESAMTTYP', 'FINANZTYP', 'GFK_URLAUBERTYP', 'GREEN_AVANTGARDE', 'LP_FAMILIE_FEIN', 'LP_FAMILIE_GROB', 'LP_STATUS_FEIN', 'LP_STATUS_GROB', 'NATIONALITAET_KZ', 'SHOPPER_TYP', 'SOHO_KZ', 'VERS_TYP', 'ZABEOTYP', 'GEBAEUDETYP', 'OST_WEST_KZ', 'CAMEO_DEUG_2015', 'CAMEO_DEU_2015']\n",
    "\n",
    "ANREDE_KZ was binary numerical so we were able to drop it.\n",
    "\n",
    "CAMEO_DEU_2015 had multiple strings as its values. We will use dummy variables on this.\n",
    "\n",
    "The remaining 16 columns were multi-level categoricals. I used pd.get_dummies() to convert these to dummy variables.\n",
    "\n",
    "These 16 become 148 columns of dummy variables. All we need to do now is to drop the columns from the original dataframe, and replace them with these.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.2.2: Engineer Mixed-Type Features\n",
    "\n",
    "There are a handful of features that are marked as \"mixed\" in the feature summary that require special treatment in order to be included in the analysis. There are two in particular that deserve attention; the handling of the rest are up to your own choices:\n",
    "- \"PRAEGENDE_JUGENDJAHRE\" combines information on three dimensions: generation by decade, movement (mainstream vs. avantgarde), and nation (east vs. west). While there aren't enough levels to disentangle east from west, you should create two new variables to capture the other two dimensions: an interval-type variable for decade, and a binary variable for movement.\n",
    "- \"CAMEO_INTL_2015\" combines information on two axes: wealth and life stage. Break up the two-digit codes by their 'tens'-place and 'ones'-place digits into two new ordinal variables (which, for the purposes of this project, is equivalent to just treating them as their raw numeric values).\n",
    "- If you decide to keep or engineer new features around the other mixed-type features, make sure you note your steps in the Discussion section.\n",
    "\n",
    "Be sure to check `Data_Dictionary.md` for the details needed to finish these tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "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>attribute</th>\n",
       "      <th>information_level</th>\n",
       "      <th>type</th>\n",
       "      <th>missing_or_unknown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>LP_LEBENSPHASE_FEIN</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>LP_LEBENSPHASE_GROB</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>PRAEGENDE_JUGENDJAHRE</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>WOHNLAGE</td>\n",
       "      <td>building</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>CAMEO_INTL_2015</td>\n",
       "      <td>microcell_rr4</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,XX]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>KBA05_BAUMAX</td>\n",
       "      <td>microcell_rr3</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>PLZ8_BAUMAX</td>\n",
       "      <td>macrocell_plz8</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                attribute information_level   type missing_or_unknown\n",
       "15    LP_LEBENSPHASE_FEIN            person  mixed                [0]\n",
       "16    LP_LEBENSPHASE_GROB            person  mixed                [0]\n",
       "22  PRAEGENDE_JUGENDJAHRE            person  mixed             [-1,0]\n",
       "56               WOHNLAGE          building  mixed               [-1]\n",
       "59        CAMEO_INTL_2015     microcell_rr4  mixed            [-1,XX]\n",
       "64           KBA05_BAUMAX     microcell_rr3  mixed             [-1,0]\n",
       "79            PLZ8_BAUMAX    macrocell_plz8  mixed             [-1,0]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_info_mixed = feat_info[(feat_info['type']=='mixed')]\n",
    "feat_info_mixed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['LP_LEBENSPHASE_FEIN', 'LP_LEBENSPHASE_GROB', 'PRAEGENDE_JUGENDJAHRE', 'WOHNLAGE', 'CAMEO_INTL_2015', 'KBA05_BAUMAX', 'PLZ8_BAUMAX']\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "mixed_cols = feat_info_mixed['attribute'].tolist()\n",
    "\n",
    "# drop_cols = ['AGER_TYP', 'TITEL_KZ', 'KK_KUNDENTYP']\n",
    "\n",
    "# for i in drop_cols:\n",
    "#     mixed_cols.remove(i)\n",
    "    \n",
    "print(mixed_cols)\n",
    "print(len(mixed_cols))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "def PRAEGENDE_JUGENDJAHRE_decade(value, **kwargs):\n",
    "    if (value == 1) or (value == 2):\n",
    "        return 40\n",
    "    elif (value == 3) or (value == 4):\n",
    "        return 50\n",
    "    elif (value == 5) or (value == 6) or (value == 7):\n",
    "        return 60\n",
    "    elif (value == 8) or (value == 9):\n",
    "        return 70\n",
    "    elif (value == 10) or (value == 11):\n",
    "        return 80\n",
    "    elif (value == 12) or (value == 13):\n",
    "        return 80\n",
    "    elif (value == 14) or (value == 15):\n",
    "        return 90\n",
    "    else:\n",
    "        return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     NaN\n",
       "1    90.0\n",
       "2    90.0\n",
       "3    70.0\n",
       "4    70.0\n",
       "Name: PRAEGENDE_JUGENDJAHRE_decade, dtype: float64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias['PRAEGENDE_JUGENDJAHRE_decade'] = azdias.loc[:,'PRAEGENDE_JUGENDJAHRE'].apply(PRAEGENDE_JUGENDJAHRE_decade, axis=1)\n",
    "azdias['PRAEGENDE_JUGENDJAHRE_decade'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def PRAEGENDE_JUGENDJAHRE_movement(value, **kwargs):\n",
    "    if (value == 1) or (value == 3) or (value == 5) or (value == 8) or (value == 10) or (value == 12) or (value == 14):\n",
    "        return 'mainstream'\n",
    "    elif (value == 2) or (value == 4) or (value == 6) or (value == 7) or (value == 9) or (value == 11) or (value == 13) or (value == 15):\n",
    "        return 'avantgarde'\n",
    "    else:\n",
    "        return value\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0           NaN\n",
       "1    mainstream\n",
       "2    avantgarde\n",
       "3    mainstream\n",
       "4    mainstream\n",
       "Name: PRAEGENDE_JUGENDJAHRE_movement, dtype: object"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias['PRAEGENDE_JUGENDJAHRE_movement'] = azdias.loc[:,'PRAEGENDE_JUGENDJAHRE'].apply(PRAEGENDE_JUGENDJAHRE_movement, axis=1)\n",
    "azdias['PRAEGENDE_JUGENDJAHRE_movement'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_decade</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>90.0</td>\n",
       "      <td>mainstream</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>90.0</td>\n",
       "      <td>avantgarde</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>70.0</td>\n",
       "      <td>mainstream</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>mainstream</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PRAEGENDE_JUGENDJAHRE_decade PRAEGENDE_JUGENDJAHRE_movement\n",
       "0                           NaN                            NaN\n",
       "1                          90.0                     mainstream\n",
       "2                          90.0                     avantgarde\n",
       "3                          70.0                     mainstream\n",
       "4                          70.0                     mainstream"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias.iloc[0:5, -2:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Similar process to the one above"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def CAMEO_INTL_2015_wealth(value, **kwargs):\n",
    "    if pd.isnull(value):\n",
    "        return value\n",
    "    else:\n",
    "        return value[0]\n",
    "\n",
    "def CAMEO_INTL_2015_life(value, **kwargs):\n",
    "    if pd.isnull(value):\n",
    "        return value\n",
    "    else:\n",
    "        return value[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    NaN\n",
       "1      5\n",
       "2      2\n",
       "3      1\n",
       "4      4\n",
       "Name: CAMEO_INTL_2015_wealth, dtype: object"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias['CAMEO_INTL_2015_wealth'] = azdias.loc[:,'CAMEO_INTL_2015'].apply(CAMEO_INTL_2015_wealth, axis=1)\n",
    "azdias['CAMEO_INTL_2015_wealth'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    NaN\n",
       "1      1\n",
       "2      4\n",
       "3      2\n",
       "4      3\n",
       "Name: CAMEO_INTL_2015_life, dtype: object"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias['CAMEO_INTL_2015_life'] = azdias.loc[:,'CAMEO_INTL_2015'].apply(CAMEO_INTL_2015_life, axis=1)\n",
    "azdias['CAMEO_INTL_2015_life'].head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Let's verify that we did it correctly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    NaN\n",
       "1     51\n",
       "2     24\n",
       "3     12\n",
       "4     43\n",
       "Name: CAMEO_INTL_2015, dtype: object"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias['CAMEO_INTL_2015'].iloc[0:5][:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Let's create our dummy variables form these new columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_movement</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade</th>\n",
       "      <th>CAMEO_INTL_2015_wealth</th>\n",
       "      <th>CAMEO_INTL_2015_life</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>mainstream</td>\n",
       "      <td>90.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>avantgarde</td>\n",
       "      <td>90.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mainstream</td>\n",
       "      <td>70.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mainstream</td>\n",
       "      <td>70.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  PRAEGENDE_JUGENDJAHRE_movement  PRAEGENDE_JUGENDJAHRE_decade  \\\n",
       "0                            NaN                           NaN   \n",
       "1                     mainstream                          90.0   \n",
       "2                     avantgarde                          90.0   \n",
       "3                     mainstream                          70.0   \n",
       "4                     mainstream                          70.0   \n",
       "\n",
       "  CAMEO_INTL_2015_wealth CAMEO_INTL_2015_life  \n",
       "0                    NaN                  NaN  \n",
       "1                      5                    1  \n",
       "2                      2                    4  \n",
       "3                      1                    2  \n",
       "4                      4                    3  "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newCols = ['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade', 'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life']\n",
    "azdias[newCols].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_movement_avantgarde</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement_mainstream</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement_nan</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_40.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_50.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_60.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_70.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_80.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_90.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_nan</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_3</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_4</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_5</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_nan</th>\n",
       "      <th>CAMEO_INTL_2015_life_1</th>\n",
       "      <th>CAMEO_INTL_2015_life_2</th>\n",
       "      <th>CAMEO_INTL_2015_life_3</th>\n",
       "      <th>CAMEO_INTL_2015_life_4</th>\n",
       "      <th>CAMEO_INTL_2015_life_5</th>\n",
       "      <th>CAMEO_INTL_2015_life_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PRAEGENDE_JUGENDJAHRE_movement_avantgarde  \\\n",
       "0                                          0   \n",
       "1                                          0   \n",
       "2                                          1   \n",
       "3                                          0   \n",
       "4                                          0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_movement_mainstream  \\\n",
       "0                                          0   \n",
       "1                                          1   \n",
       "2                                          0   \n",
       "3                                          1   \n",
       "4                                          1   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_movement_nan  PRAEGENDE_JUGENDJAHRE_decade_40.0  \\\n",
       "0                                   1                                  0   \n",
       "1                                   0                                  0   \n",
       "2                                   0                                  0   \n",
       "3                                   0                                  0   \n",
       "4                                   0                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_50.0  PRAEGENDE_JUGENDJAHRE_decade_60.0  \\\n",
       "0                                  0                                  0   \n",
       "1                                  0                                  0   \n",
       "2                                  0                                  0   \n",
       "3                                  0                                  0   \n",
       "4                                  0                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_70.0  PRAEGENDE_JUGENDJAHRE_decade_80.0  \\\n",
       "0                                  0                                  0   \n",
       "1                                  0                                  0   \n",
       "2                                  0                                  0   \n",
       "3                                  1                                  0   \n",
       "4                                  1                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_90.0  PRAEGENDE_JUGENDJAHRE_decade_nan  \\\n",
       "0                                  0                                 1   \n",
       "1                                  1                                 0   \n",
       "2                                  1                                 0   \n",
       "3                                  0                                 0   \n",
       "4                                  0                                 0   \n",
       "\n",
       "             ...             CAMEO_INTL_2015_wealth_3  \\\n",
       "0            ...                                    0   \n",
       "1            ...                                    0   \n",
       "2            ...                                    0   \n",
       "3            ...                                    0   \n",
       "4            ...                                    0   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_4  CAMEO_INTL_2015_wealth_5  \\\n",
       "0                         0                         0   \n",
       "1                         0                         1   \n",
       "2                         0                         0   \n",
       "3                         0                         0   \n",
       "4                         1                         0   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_nan  CAMEO_INTL_2015_life_1  CAMEO_INTL_2015_life_2  \\\n",
       "0                           1                       0                       0   \n",
       "1                           0                       1                       0   \n",
       "2                           0                       0                       0   \n",
       "3                           0                       0                       1   \n",
       "4                           0                       0                       0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_3  CAMEO_INTL_2015_life_4  CAMEO_INTL_2015_life_5  \\\n",
       "0                       0                       0                       0   \n",
       "1                       0                       0                       0   \n",
       "2                       0                       1                       0   \n",
       "3                       0                       0                       0   \n",
       "4                       1                       0                       0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_nan  \n",
       "0                         1  \n",
       "1                         0  \n",
       "2                         0  \n",
       "3                         0  \n",
       "4                         0  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias_mixed_dummies = pd.get_dummies(azdias[newCols].astype(str))\n",
    "azdias_mixed_dummies.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Discussion 1.2.2: Engineer Mixed-Type Features\n",
    "\n",
    "(Double-click this cell and replace this text with your own text, reporting your findings and decisions regarding mixed-value features. Which ones did you keep, which did you drop, and what engineering steps did you perform?)\n",
    "\n",
    "I performed the following steps:\n",
    "\n",
    "* For the categorical data we ignore any binary numerical columns as they are fine as is.\n",
    "* We converted the remaining categories to dummy variables\n",
    "\n",
    "* For the mixed data \n",
    "    * We looked at the PRAEGENDE_JUGENDJAHRE column\n",
    "        * We extracted the decade as a new column\n",
    "        * We extracted the movement as a new column\n",
    "    * We looked at CAMEO_INTL_2015 column\n",
    "        * We split the value into 2 single digits and put each one in a column\n",
    "    * We did a pd.get_dummies() on all of our new columns\n",
    "    \n",
    "All that remains is to drop the columns in categorical_cols and mixed_cols and replace them with azdias_cat_dummies and azdias_mixed_dummies\n",
    "\n",
    "**For the sake of simplicity I will drop the remaining mixed columns and not do any further work on them**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1.2.3: Complete Feature Selection\n",
    "\n",
    "In order to finish this step up, you need to make sure that your data frame now only has the columns that you want to keep. To summarize, the dataframe should consist of the following:\n",
    "- All numeric, interval, and ordinal type columns from the original dataset.\n",
    "- Binary categorical features (all numerically-encoded).\n",
    "- Engineered features from other multi-level categorical features and mixed features.\n",
    "\n",
    "Make sure that for any new columns that you have engineered, that you've excluded the original columns from the final dataset. Otherwise, their values will interfere with the analysis later on the project. For example, you should not keep \"PRAEGENDE_JUGENDJAHRE\", since its values won't be useful for the algorithm: only the values derived from it in the engineered features you created should be retained. As a reminder, your data should only be from **the subset with few or no missing values**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement</th>\n",
       "      <th>CAMEO_INTL_2015_wealth</th>\n",
       "      <th>CAMEO_INTL_2015_life</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>90.0</td>\n",
       "      <td>mainstream</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 83 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
       "0                   2.0          1            2.0                  3   \n",
       "1                   1.0          2            5.0                  1   \n",
       "\n",
       "   FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  \\\n",
       "0              4                 3               5                      5   \n",
       "1              5                 2               5                      4   \n",
       "\n",
       "   FINANZ_HAUSBAUER  FINANZTYP          ...           PLZ8_BAUMAX  PLZ8_HHZ  \\\n",
       "0                 3          4          ...                   NaN       NaN   \n",
       "1                 5          1          ...                   1.0       5.0   \n",
       "\n",
       "   PLZ8_GBZ  ARBEIT  ORTSGR_KLS9  RELAT_AB  PRAEGENDE_JUGENDJAHRE_decade  \\\n",
       "0       NaN     NaN          NaN       NaN                           NaN   \n",
       "1       4.0     3.0          5.0       4.0                          90.0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_movement  CAMEO_INTL_2015_wealth  \\\n",
       "0                             NaN                     NaN   \n",
       "1                      mainstream                       5   \n",
       "\n",
       "   CAMEO_INTL_2015_life  \n",
       "0                   NaN  \n",
       "1                     1  \n",
       "\n",
       "[2 rows x 83 columns]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# If there are other re-engineering tasks you need to perform, make sure you\n",
    "# take care of them here. (Dealing with missing data will come in step 2.1.)\n",
    "\n",
    "azdias.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891191</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891192</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891193</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891194</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891195</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891196</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891197</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891198</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891199</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891200</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891201</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891202</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891203</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891204</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891205</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891206</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891207</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891208</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891209</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891210</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891211</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891212</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891213</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891214</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891215</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891216</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891217</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891218</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891219</th>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>891220</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>891221 rows × 79 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  FINANZ_MINIMALIST  \\\n",
       "0                        2.0          1            2.0                  3   \n",
       "1                        1.0          2            5.0                  1   \n",
       "2                        3.0          2            3.0                  1   \n",
       "3                        4.0          2            2.0                  4   \n",
       "4                        3.0          1            5.0                  4   \n",
       "5                        1.0          2            2.0                  3   \n",
       "6                        2.0          2            5.0                  1   \n",
       "7                        1.0          1            3.0                  3   \n",
       "8                        3.0          1            3.0                  4   \n",
       "9                        3.0          2            4.0                  2   \n",
       "10                       3.0          2            1.0                  2   \n",
       "11                       2.0          1            6.0                  3   \n",
       "12                       3.0          1            6.0                  5   \n",
       "13                       1.0          2            5.0                  1   \n",
       "14                       3.0          1            6.0                  3   \n",
       "15                       4.0          2            4.0                  4   \n",
       "16                       1.0          2            1.0                  4   \n",
       "17                       2.0          1            6.0                  3   \n",
       "18                       2.0          2            6.0                  2   \n",
       "19                       3.0          1            3.0                  5   \n",
       "20                       2.0          2            4.0                  4   \n",
       "21                       2.0          1            3.0                  3   \n",
       "22                       1.0          1            4.0                  1   \n",
       "23                       3.0          1            3.0                  5   \n",
       "24                       3.0          2            6.0                  3   \n",
       "25                       1.0          1            3.0                  3   \n",
       "26                       3.0          1            3.0                  5   \n",
       "27                       3.0          1            4.0                  3   \n",
       "28                       3.0          1            2.0                  3   \n",
       "29                       4.0          2            1.0                  5   \n",
       "...                      ...        ...            ...                ...   \n",
       "891191                   4.0          2            1.0                  4   \n",
       "891192                   1.0          2            3.0                  1   \n",
       "891193                   4.0          1            3.0                  4   \n",
       "891194                   3.0          1            4.0                  4   \n",
       "891195                   4.0          2            6.0                  3   \n",
       "891196                   2.0          2            6.0                  1   \n",
       "891197                   3.0          2            1.0                  3   \n",
       "891198                   3.0          1            5.0                  2   \n",
       "891199                   2.0          1            3.0                  2   \n",
       "891200                   1.0          2            3.0                  1   \n",
       "891201                   3.0          1            3.0                  4   \n",
       "891202                   2.0          2            5.0                  1   \n",
       "891203                   4.0          2            1.0                  4   \n",
       "891204                   3.0          1            5.0                  4   \n",
       "891205                   4.0          1            2.0                  4   \n",
       "891206                   1.0          2            4.0                  3   \n",
       "891207                   3.0          2            1.0                  5   \n",
       "891208                   4.0          1            2.0                  5   \n",
       "891209                   1.0          2            5.0                  1   \n",
       "891210                   3.0          1            5.0                  3   \n",
       "891211                   3.0          1            2.0                  3   \n",
       "891212                   4.0          1            1.0                  3   \n",
       "891213                   4.0          2            5.0                  3   \n",
       "891214                   1.0          2            4.0                  1   \n",
       "891215                   2.0          2            6.0                  1   \n",
       "891216                   3.0          2            5.0                  1   \n",
       "891217                   2.0          1            4.0                  3   \n",
       "891218                   2.0          2            4.0                  2   \n",
       "891219                   1.0          1            3.0                  1   \n",
       "891220                   4.0          1            1.0                  4   \n",
       "\n",
       "        FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "0                   4                 3               5   \n",
       "1                   5                 2               5   \n",
       "2                   4                 1               2   \n",
       "3                   2                 5               2   \n",
       "4                   3                 4               1   \n",
       "5                   1                 5               2   \n",
       "6                   5                 1               5   \n",
       "7                   3                 4               1   \n",
       "8                   4                 2               4   \n",
       "9                   4                 2               3   \n",
       "10                  2                 5               3   \n",
       "11                  4                 3               5   \n",
       "12                  3                 4               2   \n",
       "13                  4                 3               5   \n",
       "14                  4                 3               5   \n",
       "15                  1                 5               1   \n",
       "16                  3                 1               4   \n",
       "17                  4                 3               5   \n",
       "18                  4                 1               5   \n",
       "19                  2                 3               1   \n",
       "20                  3                 1               4   \n",
       "21                  4                 1               2   \n",
       "22                  5                 3               5   \n",
       "23                  3                 3               2   \n",
       "24                  4                 3               5   \n",
       "25                  5                 3               5   \n",
       "26                  2                 4               2   \n",
       "27                  3                 4               1   \n",
       "28                  2                 4               3   \n",
       "29                  1                 5               2   \n",
       "...               ...               ...             ...   \n",
       "891191              1                 5               1   \n",
       "891192              5                 2               5   \n",
       "891193              1                 5               1   \n",
       "891194              3                 4               2   \n",
       "891195              1                 5               1   \n",
       "891196              5                 2               5   \n",
       "891197              2                 4               1   \n",
       "891198              5                 3               4   \n",
       "891199              5                 2               3   \n",
       "891200              5                 3               5   \n",
       "891201              2                 3               2   \n",
       "891202              4                 1               2   \n",
       "891203              1                 5               1   \n",
       "891204              3                 4               4   \n",
       "891205              2                 4               3   \n",
       "891206              4                 2               4   \n",
       "891207              1                 5               1   \n",
       "891208              2                 4               2   \n",
       "891209              4                 2               3   \n",
       "891210              3                 4               2   \n",
       "891211              2                 4               3   \n",
       "891212              1                 5               1   \n",
       "891213              3                 3               5   \n",
       "891214              5                 2               3   \n",
       "891215              5                 2               4   \n",
       "891216              4                 2               5   \n",
       "891217              3                 3               2   \n",
       "891218              4                 2               5   \n",
       "891219              5                 3               5   \n",
       "891220              2                 5               2   \n",
       "\n",
       "        FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  FINANZTYP    ...     \\\n",
       "0                           5                 3          4    ...      \n",
       "1                           4                 5          1    ...      \n",
       "2                           3                 5          1    ...      \n",
       "3                           1                 2          6    ...      \n",
       "4                           3                 2          5    ...      \n",
       "5                           2                 5          2    ...      \n",
       "6                           4                 3          4    ...      \n",
       "7                           3                 2          5    ...      \n",
       "8                           2                 2          6    ...      \n",
       "9                           5                 4          1    ...      \n",
       "10                          1                 5          6    ...      \n",
       "11                          5                 3          4    ...      \n",
       "12                          4                 1          3    ...      \n",
       "13                          5                 2          1    ...      \n",
       "14                          5                 3          4    ...      \n",
       "15                          1                 4          2    ...      \n",
       "16                          5                 1          3    ...      \n",
       "17                          5                 3          4    ...      \n",
       "18                          4                 1          1    ...      \n",
       "19                          3                 1          5    ...      \n",
       "20                          5                 1          3    ...      \n",
       "21                          5                 1          3    ...      \n",
       "22                          5                 5          1    ...      \n",
       "23                          2                 1          6    ...      \n",
       "24                          5                 3          4    ...      \n",
       "25                          4                 3          4    ...      \n",
       "26                          3                 1          3    ...      \n",
       "27                          2                 2          5    ...      \n",
       "28                          3                 2          3    ...      \n",
       "29                          1                 3          6    ...      \n",
       "...                       ...               ...        ...    ...      \n",
       "891191                      1                 4          2    ...      \n",
       "891192                      3                 5          1    ...      \n",
       "891193                      2                 5          5    ...      \n",
       "891194                      3                 1          3    ...      \n",
       "891195                      1                 5          2    ...      \n",
       "891196                      4                 3          1    ...      \n",
       "891197                      3                 3          5    ...      \n",
       "891198                      5                 3          1    ...      \n",
       "891199                      3                 4          1    ...      \n",
       "891200                      5                 5          1    ...      \n",
       "891201                      2                 3          6    ...      \n",
       "891202                      5                 4          1    ...      \n",
       "891203                      3                 4          5    ...      \n",
       "891204                      2                 1          6    ...      \n",
       "891205                      3                 4          2    ...      \n",
       "891206                      4                 2          4    ...      \n",
       "891207                      2                 4          2    ...      \n",
       "891208                      3                 1          6    ...      \n",
       "891209                      4                 4          1    ...      \n",
       "891210                      2                 2          6    ...      \n",
       "891211                      3                 2          2    ...      \n",
       "891212                      1                 5          5    ...      \n",
       "891213                      3                 2          6    ...      \n",
       "891214                      3                 4          1    ...      \n",
       "891215                      5                 4          1    ...      \n",
       "891216                      4                 4          1    ...      \n",
       "891217                      2                 3          6    ...      \n",
       "891218                      4                 3          1    ...      \n",
       "891219                      5                 5          1    ...      \n",
       "891220                      1                 5          6    ...      \n",
       "\n",
       "        PLZ8_ANTG1  PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  \\\n",
       "0              NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "1              2.0         3.0         2.0         1.0          1.0       5.0   \n",
       "2              3.0         3.0         1.0         0.0          1.0       4.0   \n",
       "3              2.0         2.0         2.0         0.0          1.0       3.0   \n",
       "4              2.0         4.0         2.0         1.0          2.0       3.0   \n",
       "5              2.0         3.0         1.0         1.0          1.0       5.0   \n",
       "6              3.0         3.0         1.0         0.0          1.0       5.0   \n",
       "7              3.0         3.0         1.0         0.0          1.0       4.0   \n",
       "8              2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "9              2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "10             2.0         4.0         2.0         0.0          2.0       3.0   \n",
       "11             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "12             3.0         3.0         1.0         0.0          1.0       5.0   \n",
       "13             2.0         1.0         1.0         1.0          1.0       3.0   \n",
       "14             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "15             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "16             3.0         3.0         1.0         0.0          1.0       3.0   \n",
       "17             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "18             2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "19             2.0         4.0         2.0         1.0          2.0       5.0   \n",
       "20             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "21             1.0         4.0         3.0         2.0          4.0       4.0   \n",
       "22             2.0         4.0         2.0         1.0          2.0       4.0   \n",
       "23             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "24             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "25             2.0         2.0         0.0         0.0          1.0       4.0   \n",
       "26             NaN         NaN         NaN         NaN          NaN       NaN   \n",
       "27             2.0         3.0         3.0         2.0          4.0       5.0   \n",
       "28             3.0         3.0         1.0         0.0          1.0       5.0   \n",
       "29             3.0         2.0         2.0         0.0          1.0       3.0   \n",
       "...            ...         ...         ...         ...          ...       ...   \n",
       "891191         3.0         3.0         1.0         0.0          1.0       4.0   \n",
       "891192         4.0         2.0         1.0         0.0          1.0       3.0   \n",
       "891193         2.0         4.0         1.0         0.0          2.0       2.0   \n",
       "891194         3.0         2.0         2.0         0.0          1.0       4.0   \n",
       "891195         2.0         3.0         2.0         1.0          1.0       4.0   \n",
       "891196         2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "891197         2.0         3.0         2.0         1.0          1.0       4.0   \n",
       "891198         4.0         2.0         1.0         0.0          1.0       4.0   \n",
       "891199         1.0         4.0         3.0         2.0          5.0       5.0   \n",
       "891200         1.0         4.0         2.0         1.0          2.0       3.0   \n",
       "891201         1.0         4.0         3.0         1.0          3.0       4.0   \n",
       "891202         1.0         4.0         3.0         2.0          5.0       4.0   \n",
       "891203         2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "891204         2.0         3.0         1.0         0.0          1.0       3.0   \n",
       "891205         1.0         4.0         3.0         1.0          3.0       3.0   \n",
       "891206         3.0         2.0         1.0         0.0          1.0       4.0   \n",
       "891207         2.0         3.0         2.0         2.0          4.0       4.0   \n",
       "891208         3.0         2.0         0.0         0.0          1.0       3.0   \n",
       "891209         1.0         4.0         2.0         1.0          5.0       5.0   \n",
       "891210         2.0         3.0         2.0         1.0          1.0       3.0   \n",
       "891211         1.0         4.0         2.0         0.0          2.0       3.0   \n",
       "891212         1.0         3.0         3.0         2.0          5.0       4.0   \n",
       "891213         2.0         4.0         2.0         1.0          2.0       5.0   \n",
       "891214         1.0         4.0         3.0         2.0          5.0       5.0   \n",
       "891215         2.0         4.0         2.0         1.0          2.0       4.0   \n",
       "891216         3.0         2.0         0.0         0.0          1.0       2.0   \n",
       "891217         1.0         3.0         3.0         2.0          4.0       5.0   \n",
       "891218         4.0         2.0         0.0         0.0          1.0       3.0   \n",
       "891219         1.0         4.0         3.0         1.0          5.0       1.0   \n",
       "891220         3.0         3.0         1.0         0.0          1.0       4.0   \n",
       "\n",
       "        PLZ8_GBZ  ARBEIT  ORTSGR_KLS9  RELAT_AB  \n",
       "0            NaN     NaN          NaN       NaN  \n",
       "1            4.0     3.0          5.0       4.0  \n",
       "2            4.0     3.0          5.0       2.0  \n",
       "3            4.0     2.0          3.0       3.0  \n",
       "4            3.0     4.0          6.0       5.0  \n",
       "5            5.0     2.0          3.0       3.0  \n",
       "6            5.0     4.0          6.0       3.0  \n",
       "7            4.0     2.0          5.0       2.0  \n",
       "8            3.0     2.0          4.0       3.0  \n",
       "9            3.0     2.0          3.0       1.0  \n",
       "10           3.0     4.0          6.0       5.0  \n",
       "11           NaN     NaN          NaN       NaN  \n",
       "12           5.0     3.0          6.0       4.0  \n",
       "13           3.0     3.0          6.0       4.0  \n",
       "14           NaN     NaN          NaN       NaN  \n",
       "15           NaN     4.0          8.0       5.0  \n",
       "16           4.0     1.0          2.0       1.0  \n",
       "17           NaN     NaN          NaN       NaN  \n",
       "18           3.0     3.0          4.0       3.0  \n",
       "19           4.0     4.0          6.0       3.0  \n",
       "20           NaN     3.0          4.0       1.0  \n",
       "21           3.0     5.0          7.0       5.0  \n",
       "22           3.0     4.0          5.0       5.0  \n",
       "23           NaN     3.0          6.0       2.0  \n",
       "24           NaN     NaN          NaN       NaN  \n",
       "25           5.0     3.0          2.0       5.0  \n",
       "26           NaN     4.0          3.0       5.0  \n",
       "27           3.0     4.0          7.0       5.0  \n",
       "28           5.0     3.0          4.0       1.0  \n",
       "29           4.0     2.0          4.0       3.0  \n",
       "...          ...     ...          ...       ...  \n",
       "891191       5.0     1.0          3.0       1.0  \n",
       "891192       3.0     3.0          5.0       1.0  \n",
       "891193       2.0     4.0          6.0       4.0  \n",
       "891194       4.0     4.0          8.0       5.0  \n",
       "891195       3.0     4.0          7.0       5.0  \n",
       "891196       3.0     3.0          4.0       5.0  \n",
       "891197       4.0     3.0          9.0       5.0  \n",
       "891198       5.0     3.0          9.0       5.0  \n",
       "891199       3.0     3.0          7.0       3.0  \n",
       "891200       3.0     4.0          6.0       5.0  \n",
       "891201       3.0     3.0          7.0       5.0  \n",
       "891202       2.0     4.0          8.0       5.0  \n",
       "891203       3.0     4.0          8.0       5.0  \n",
       "891204       4.0     4.0          7.0       3.0  \n",
       "891205       2.0     3.0          7.0       5.0  \n",
       "891206       5.0     3.0          4.0       3.0  \n",
       "891207       2.0     1.0          5.0       1.0  \n",
       "891208       3.0     4.0          2.0       5.0  \n",
       "891209       4.0     4.0          5.0       5.0  \n",
       "891210       3.0     3.0          9.0       5.0  \n",
       "891211       2.0     3.0          5.0       5.0  \n",
       "891212       1.0     3.0          9.0       5.0  \n",
       "891213       5.0     3.0          4.0       4.0  \n",
       "891214       2.0     3.0          7.0       3.0  \n",
       "891215       4.0     2.0          5.0       2.0  \n",
       "891216       3.0     NaN          NaN       NaN  \n",
       "891217       3.0     4.0          6.0       5.0  \n",
       "891218       4.0     2.0          2.0       3.0  \n",
       "891219       1.0     4.0          7.0       5.0  \n",
       "891220       4.0     3.0          4.0       5.0  \n",
       "\n",
       "[891221 rows x 79 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdias.drop(['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade', 'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life'], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We need to drop ['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade',\n",
    "       'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdias.drop(['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade',\n",
    "       'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "79"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(azdias.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "7\n",
      "\n",
      "148\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "print(len(categorical_cols))\n",
    "print(len(mixed_cols))\n",
    "print()\n",
    "print(len(azdias_cat_dummies.columns))\n",
    "print(len(azdias_mixed_dummies.columns))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have 79 cols which is what we started with after we dropped the mostly empty cols.\n",
    "\n",
    "We need to do the following:\n",
    "\n",
    "* Drop categorical_cols and replace with adzias_cat_dummies\n",
    "* Drop mixed_cols and replace with azdias_mixed_dummies\n",
    "\n",
    "We know we need to drop 16+6 = 22 columns from azdias (It's 6 because we dropped one of these right at the start)\n",
    "\n",
    "We know we need to add 148+22 = 170 columns to azdias\n",
    "\n",
    "We know we will end up with 79-22+170=227 columns at the end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "mixed_cols.remove('KBA05_BAUMAX')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LP_LEBENSPHASE_FEIN',\n",
       " 'LP_LEBENSPHASE_GROB',\n",
       " 'PRAEGENDE_JUGENDJAHRE',\n",
       " 'WOHNLAGE',\n",
       " 'CAMEO_INTL_2015',\n",
       " 'PLZ8_BAUMAX']"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mixed_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdias.drop(categorical_cols, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdias.drop(mixed_cols, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>GREEN_AVANTGARDE</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>963.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 57 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "0                   2.0          1                  3              4   \n",
       "1                   1.0          2                  1              5   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "0                 3               5                      5                 3   \n",
       "1                 2               5                      4                 5   \n",
       "\n",
       "   GREEN_AVANTGARDE  HEALTH_TYP    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "0                 0         NaN    ...                  NaN         NaN   \n",
       "1                 0         3.0    ...                963.0         2.0   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         NaN         NaN         NaN       NaN       NaN     NaN   \n",
       "1         3.0         2.0         1.0       5.0       4.0     3.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          NaN       NaN  \n",
       "1          5.0       4.0  \n",
       "\n",
       "[2 rows x 57 columns]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp = azdias\n",
    "azdiastemp.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "azdiastemp = azdiastemp.join(azdias_cat_dummies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp = azdiastemp.join(azdias_mixed_dummies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>GREEN_AVANTGARDE</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_3</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_4</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_5</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_nan</th>\n",
       "      <th>CAMEO_INTL_2015_life_1</th>\n",
       "      <th>CAMEO_INTL_2015_life_2</th>\n",
       "      <th>CAMEO_INTL_2015_life_3</th>\n",
       "      <th>CAMEO_INTL_2015_life_4</th>\n",
       "      <th>CAMEO_INTL_2015_life_5</th>\n",
       "      <th>CAMEO_INTL_2015_life_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 227 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "0                   2.0          1                  3              4   \n",
       "1                   1.0          2                  1              5   \n",
       "2                   3.0          2                  1              4   \n",
       "3                   4.0          2                  4              2   \n",
       "4                   3.0          1                  4              3   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "0                 3               5                      5                 3   \n",
       "1                 2               5                      4                 5   \n",
       "2                 1               2                      3                 5   \n",
       "3                 5               2                      1                 2   \n",
       "4                 4               1                      3                 2   \n",
       "\n",
       "   GREEN_AVANTGARDE  HEALTH_TYP            ...             \\\n",
       "0                 0         NaN            ...              \n",
       "1                 0         3.0            ...              \n",
       "2                 1         3.0            ...              \n",
       "3                 0         2.0            ...              \n",
       "4                 0         3.0            ...              \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_3  CAMEO_INTL_2015_wealth_4  \\\n",
       "0                         0                         0   \n",
       "1                         0                         0   \n",
       "2                         0                         0   \n",
       "3                         0                         0   \n",
       "4                         0                         1   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_5  CAMEO_INTL_2015_wealth_nan  \\\n",
       "0                         0                           1   \n",
       "1                         1                           0   \n",
       "2                         0                           0   \n",
       "3                         0                           0   \n",
       "4                         0                           0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_1  CAMEO_INTL_2015_life_2  CAMEO_INTL_2015_life_3  \\\n",
       "0                       0                       0                       0   \n",
       "1                       1                       0                       0   \n",
       "2                       0                       0                       0   \n",
       "3                       0                       1                       0   \n",
       "4                       0                       0                       1   \n",
       "\n",
       "   CAMEO_INTL_2015_life_4  CAMEO_INTL_2015_life_5  CAMEO_INTL_2015_life_nan  \n",
       "0                       0                       0                         1  \n",
       "1                       0                       0                         0  \n",
       "2                       1                       0                         0  \n",
       "3                       0                       0                         0  \n",
       "4                       0                       0                         0  \n",
       "\n",
       "[5 rows x 227 columns]"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "We have 227 cols as expected"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1.3: Create a Cleaning Function\n",
    "\n",
    "Even though you've finished cleaning up the general population demographics data, it's important to look ahead to the future and realize that you'll need to perform the same cleaning steps on the customer demographics data. In this substep, complete the function below to execute the main feature selection, encoding, and re-engineering steps you performed above. Then, when it comes to looking at the customer data in Step 3, you can just run this function on that DataFrame to get the trimmed dataset in a single step."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_data(df):\n",
    "    \"\"\"\n",
    "    Perform feature trimming, re-encoding, and engineering for demographics\n",
    "    data\n",
    "    \n",
    "    INPUT: Demographics DataFrame\n",
    "    OUTPUT: Trimmed and cleaned demographics DataFrame\n",
    "    \"\"\"\n",
    "    \n",
    "    # Put in code here to execute all main cleaning steps:\n",
    "    # convert missing value codes into NaNs, ...\n",
    "    \n",
    "    \n",
    "    # remove selected columns and rows, ...\n",
    "\n",
    "    \n",
    "    # select, re-encode, and engineer column values.\n",
    "\n",
    "    \n",
    "    # Return the cleaned dataframe.\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Feature Transformation\n",
    "\n",
    "### Step 2.1: Apply Feature Scaling\n",
    "\n",
    "Before we apply dimensionality reduction techniques to the data, we need to perform feature scaling so that the principal component vectors are not influenced by the natural differences in scale for features. Starting from this part of the project, you'll want to keep an eye on the [API reference page for sklearn](http://scikit-learn.org/stable/modules/classes.html) to help you navigate to all of the classes and functions that you'll need. In this substep, you'll need to check the following:\n",
    "\n",
    "- sklearn requires that data not have missing values in order for its estimators to work properly. So, before applying the scaler to your data, make sure that you've cleaned the DataFrame of the remaining missing values. This can be as simple as just removing all data points with missing data, or applying an [Imputer](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Imputer.html) to replace all missing values. You might also try a more complicated procedure where you temporarily remove missing values in order to compute the scaling parameters before re-introducing those missing values and applying imputation. Think about how much missing data you have and what possible effects each approach might have on your analysis, and justify your decision in the discussion section below.\n",
    "- For the actual scaling function, a [StandardScaler](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html) instance is suggested, scaling each feature to mean 0 and standard deviation 1.\n",
    "- For these classes, you can make use of the `.fit_transform()` method to both fit a procedure to the data as well as apply the transformation to the data at the same time. Don't forget to keep the fit sklearn objects handy, since you'll be applying them to the customer demographics data towards the end of the project."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you've not yet cleaned the dataset of all NaN values, then investigate and\n",
    "# do that now.\n",
    "\n",
    "# from sklearn.impute import SimpleImputer\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "To keep things simple I will drop any row with a null value. As long as we know that our final result may suffer because of the lack of information I am happy to do this for the sake of the project. In reality I would spend more time using an Imputer (which isn't a simple thing to do if you want to do it properly), or using another ML algorithm to predict the values we are missing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "259095"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp.shape[0] - azdiastemp.dropna().shape[0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp_noNa = azdiastemp.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(632126, 227)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will lose 259,095 (30%) of our rows by dropping null values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "As we encoded our features from before into dummy variables, we do not need to scale these now. These are contained in the lists azdias_cat_dummies and azdias_mixed_dummies. In addition we don't need to scale GREEN_AVANTGARDE as it is a binary column with 1,0. We do need to scale ANREDE_KZ, as it is not a binary numbered value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(891221, 227)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "azdiastemp_noNa.drop(azdias_cat_dummies.columns, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "azdiastemp_noNa.drop(azdias_mixed_dummies.columns, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "azdiastemp_noNa.drop('GREEN_AVANTGARDE', axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>RETOURTYP_BK_S</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>963.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>712.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>435.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1300.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 56 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "1                   1.0          2                  1              5   \n",
       "2                   3.0          2                  1              4   \n",
       "4                   3.0          1                  4              3   \n",
       "5                   1.0          2                  3              1   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "1                 2               5                      4                 5   \n",
       "2                 1               2                      3                 5   \n",
       "4                 4               1                      3                 2   \n",
       "5                 5               2                      2                 5   \n",
       "\n",
       "   HEALTH_TYP  RETOURTYP_BK_S    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "1         3.0             1.0    ...                963.0         2.0   \n",
       "2         3.0             3.0    ...                712.0         3.0   \n",
       "4         3.0             5.0    ...                435.0         2.0   \n",
       "5         3.0             3.0    ...               1300.0         2.0   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "1         3.0         2.0         1.0       5.0       4.0     3.0   \n",
       "2         3.0         1.0         0.0       4.0       4.0     3.0   \n",
       "4         4.0         2.0         1.0       3.0       3.0     4.0   \n",
       "5         3.0         1.0         1.0       5.0       5.0     2.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "1          5.0       4.0  \n",
       "2          5.0       2.0  \n",
       "4          6.0       5.0  \n",
       "5          3.0       3.0  \n",
       "\n",
       "[4 rows x 56 columns]"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.head(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "scaler = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StandardScaler(copy=True, with_mean=True, with_std=True)"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scaler.fit(azdiastemp_noNa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp_noNa_scaled = pd.DataFrame(scaler.transform(azdiastemp_noNa), columns=azdiastemp_noNa.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa_scaled.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(632126, 56)"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa_scaled.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(632126, 56)"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp_noNa = azdiastemp.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(632126, 227)"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "azdiastemp_noNa.drop(azdiastemp_noNa_scaled.columns, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([     1,      2,      4,      5,      6,      7,      8,      9,\n",
       "                10,     19,\n",
       "            ...\n",
       "            891207, 891209, 891210, 891211, 891212, 891213, 891214, 891215,\n",
       "            891219, 891220],\n",
       "           dtype='int64', length=632126)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(632126, 171)\n",
      "(632126, 56)\n"
     ]
    }
   ],
   "source": [
    "print(azdiastemp_noNa.shape)\n",
    "print(azdiastemp_noNa_scaled.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp_noNa_final = pd.merge(azdiastemp_noNa.reset_index(), azdiastemp_noNa_scaled.reset_index(), right_index=True, left_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa_final.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "azdiastemp_noNa_final.drop(['index_x', 'index_y'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(632126, 227)\n"
     ]
    },
    {
     "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>GREEN_AVANTGARDE</th>\n",
       "      <th>CJT_GESAMTTYP_1.0</th>\n",
       "      <th>CJT_GESAMTTYP_2.0</th>\n",
       "      <th>CJT_GESAMTTYP_3.0</th>\n",
       "      <th>CJT_GESAMTTYP_4.0</th>\n",
       "      <th>CJT_GESAMTTYP_5.0</th>\n",
       "      <th>CJT_GESAMTTYP_6.0</th>\n",
       "      <th>CJT_GESAMTTYP_nan</th>\n",
       "      <th>FINANZTYP_1</th>\n",
       "      <th>FINANZTYP_2</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.057812</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>0.586011</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>0.655880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.302898</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>0.400863</td>\n",
       "      <td>0.586011</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>-0.824992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.530214</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>1.270764</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>0.807528</td>\n",
       "      <td>0.263572</td>\n",
       "      <td>1.396317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.071381</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-1.043069</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.769080</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>0.807528</td>\n",
       "      <td>0.263572</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.441249</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>0.400863</td>\n",
       "      <td>0.586011</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>-0.824992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.301635</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-0.607522</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.244490</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-1.043069</td>\n",
       "      <td>-1.565428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.563298</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>1.270764</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>0.807528</td>\n",
       "      <td>0.263572</td>\n",
       "      <td>1.396317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.029204</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>1.270764</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>0.586011</td>\n",
       "      <td>0.807528</td>\n",
       "      <td>0.263572</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 227 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   GREEN_AVANTGARDE  CJT_GESAMTTYP_1.0  CJT_GESAMTTYP_2.0  CJT_GESAMTTYP_3.0  \\\n",
       "0                 0                  0                  0                  0   \n",
       "1                 1                  0                  0                  1   \n",
       "2                 0                  0                  0                  0   \n",
       "3                 0                  0                  1                  0   \n",
       "4                 0                  0                  0                  0   \n",
       "5                 0                  0                  0                  1   \n",
       "6                 1                  0                  0                  1   \n",
       "7                 1                  0                  0                  0   \n",
       "8                 0                  1                  0                  0   \n",
       "9                 1                  0                  0                  1   \n",
       "\n",
       "   CJT_GESAMTTYP_4.0  CJT_GESAMTTYP_5.0  CJT_GESAMTTYP_6.0  CJT_GESAMTTYP_nan  \\\n",
       "0                  0                  1                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  1                  0                  0   \n",
       "3                  0                  0                  0                  0   \n",
       "4                  0                  1                  0                  0   \n",
       "5                  0                  0                  0                  0   \n",
       "6                  0                  0                  0                  0   \n",
       "7                  1                  0                  0                  0   \n",
       "8                  0                  0                  0                  0   \n",
       "9                  0                  0                  0                  0   \n",
       "\n",
       "   FINANZTYP_1  FINANZTYP_2    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "0            1            0    ...             1.057812   -0.231061   \n",
       "1            1            0    ...             0.302898    0.791700   \n",
       "2            0            0    ...            -0.530214   -0.231061   \n",
       "3            0            1    ...             2.071381   -0.231061   \n",
       "4            0            0    ...             0.769080    0.791700   \n",
       "5            0            0    ...             0.441249    0.791700   \n",
       "6            0            0    ...            -0.301635   -0.231061   \n",
       "7            1            0    ...            -0.244490   -0.231061   \n",
       "8            0            0    ...            -0.563298   -0.231061   \n",
       "9            0            0    ...             0.029204   -0.231061   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ    ARBEIT  \\\n",
       "0    0.181378     0.37203    0.381459  1.440275  0.586011 -0.203820   \n",
       "1    0.181378    -0.64373   -0.984345  0.400863  0.586011 -0.203820   \n",
       "2    1.270764     0.37203    0.381459 -0.638548 -0.311834  0.807528   \n",
       "3    0.181378    -0.64373    0.381459  1.440275  1.483855 -1.215167   \n",
       "4    0.181378    -0.64373   -0.984345  1.440275  1.483855  0.807528   \n",
       "5    0.181378    -0.64373   -0.984345  0.400863  0.586011 -1.215167   \n",
       "6    0.181378     0.37203    0.381459 -0.638548 -0.311834 -1.215167   \n",
       "7    0.181378     0.37203    0.381459 -0.638548 -0.311834 -1.215167   \n",
       "8    1.270764     0.37203   -0.984345 -0.638548 -0.311834  0.807528   \n",
       "9    1.270764     0.37203    0.381459  1.440275  0.586011  0.807528   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0    -0.171975  0.655880  \n",
       "1    -0.171975 -0.824992  \n",
       "2     0.263572  1.396317  \n",
       "3    -1.043069 -0.084556  \n",
       "4     0.263572 -0.084556  \n",
       "5    -0.171975 -0.824992  \n",
       "6    -0.607522 -0.084556  \n",
       "7    -1.043069 -1.565428  \n",
       "8     0.263572  1.396317  \n",
       "9     0.263572 -0.084556  \n",
       "\n",
       "[10 rows x 227 columns]"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(azdiastemp_noNa_final.shape)\n",
    "azdiastemp_noNa_final.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Discussion 2.1: Apply Feature Scaling\n",
    "\n",
    "My process has been explained above but we can recap here\n",
    "\n",
    "The idea was:\n",
    "\n",
    "* Filter out columns that had been transformed into dummy variables\n",
    "* Apply scaling to the remaining columns\n",
    "* Join everything together under azdiastemp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2.2: Perform Dimensionality Reduction\n",
    "\n",
    "On your scaled data, you are now ready to apply dimensionality reduction techniques.\n",
    "\n",
    "- Use sklearn's [PCA](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html) class to apply principal component analysis on the data, thus finding the vectors of maximal variance in the data. To start, you should not set any parameters (so all components are computed) or set a number of components that is at least half the number of features (so there's enough features to see the general trend in variability).\n",
    "- Check out the ratio of variance explained by each principal component as well as the cumulative variance explained. Try plotting the cumulative or sequential values using matplotlib's [`plot()`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html) function. Based on what you find, select a value for the number of transformed features you'll retain for the clustering part of the project.\n",
    "- Once you've made a choice for the number of components to keep, make sure you re-fit a PCA instance to perform the decided-on transformation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "azdiastemp_noNa_final.isnull().values.any()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Reusing some of the code from the videos here, really good way to plot the pca variance!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply PCA to the data.\n",
    "from sklearn.decomposition import PCA\n",
    "\n",
    "def do_pca(n_components, data):\n",
    "    '''\n",
    "    Transforms data using PCA to create n_components, and provides back the results of the\n",
    "    transformation.\n",
    "\n",
    "    INPUT: n_components - int - the number of principal components to create\n",
    "           data - the data you would like to transform\n",
    "\n",
    "    OUTPUT: pca - the pca object created after fitting the data\n",
    "            X_pca - the transformed X matrix with new number of components\n",
    "    '''\n",
    "#     X = StandardScaler().fit_transform(data)\n",
    "    pca = PCA(n_components)\n",
    "#     X_pca = pca.fit_transform(X)\n",
    "    X_pca = pca.fit_transform(data)\n",
    "    return pca, X_pca\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "pca, df_pca = do_pca(50, azdiastemp_noNa_final)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "def scree_plot(pca):\n",
    "    '''\n",
    "    Creates a scree plot associated with the principal components\n",
    "\n",
    "    INPUT: pca - the result of instantian of PCA in scikit learn\n",
    "\n",
    "    OUTPUT:\n",
    "            None\n",
    "    '''\n",
    "    num_components=len(pca.explained_variance_ratio_)\n",
    "    ind = np.arange(num_components)\n",
    "    vals = pca.explained_variance_ratio_\n",
    "\n",
    "    plt.figure(figsize=(10, 6))\n",
    "    ax = plt.subplot(111)\n",
    "    cumvals = np.cumsum(vals)\n",
    "    ax.bar(ind, vals)\n",
    "    ax.plot(ind, cumvals)\n",
    "    for i in range(num_components):\n",
    "        ax.annotate(r\"%s%%\" % ((str(vals[i]*100)[:4])), (ind[i]+0.2, vals[i]), va=\"bottom\", ha=\"center\", fontsize=12)\n",
    "\n",
    "    ax.xaxis.set_tick_params(width=0)\n",
    "    ax.yaxis.set_tick_params(width=2, length=12)\n",
    "\n",
    "    ax.set_xlabel(\"Principal Component\")\n",
    "    ax.set_ylabel(\"Variance Explained (%)\")\n",
    "    plt.title('Explained Variance Per Principal Component')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43460 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb35db21b00>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "scree_plot(pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Re-apply PCA to the data while selecting for number of components to retain.\n",
    "import copy\n",
    "\n",
    "pca, df_pca = do_pca(30, azdiastemp_noNa_final)\n",
    "\n",
    "df_pca_orig = copy.deepcopy(df_pca)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Discussion 2.2: Perform Dimensionality Reduction\n",
    "\n",
    "(Double-click this cell and replace this text with your own text, reporting your findings and decisions regarding dimensionality reduction. How many principal components / transformed features are you retaining for the next step of the analysis?)\n",
    "\n",
    "Reusing the code from the videos, we can see that the first 3 features account for around 35% of the variance of the data. We see that this variance quickly falls off as we have more and more features. From the above graph I think 30 features seems a fair number of features to retain, as this should account for around 80% of the variance in the data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2.3: Interpret Principal Components\n",
    "\n",
    "Now that we have our transformed principal components, it's a nice idea to check out the weight of each variable on the first few components to see if they can be interpreted in some fashion.\n",
    "\n",
    "As a reminder, each principal component is a unit vector that points in the direction of highest variance (after accounting for the variance captured by earlier principal components). The further a weight is from zero, the more the principal component is in the direction of the corresponding feature. If two features have large weights of the same sign (both positive or both negative), then increases in one tend expect to be associated with increases in the other. To contrast, features with different signs can be expected to show a negative correlation: increases in one variable should result in a decrease in the other.\n",
    "\n",
    "- To investigate the features, you should map each weight to their corresponding feature name, then sort the features according to weight. The most interesting features for each principal component, then, will be those at the beginning and end of the sorted list. Use the data dictionary document to help you understand these most prominent features, their relationships, and what a positive or negative value on the principal component might indicate.\n",
    "- You should investigate and interpret feature associations from the first three principal components in this substep. To help facilitate this, you should write a function that you can call at any time to print the sorted list of feature weights, for the *i*-th principal component. This might come in handy in the next step of the project, when you interpret the tendencies of the discovered clusters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getWeights(df, pca, weightIndex, N):\n",
    "    weights = pd.DataFrame(pca.components_, columns=df.columns)\n",
    "    weights = weights.iloc[weightIndex:weightIndex+1, :].transpose()\n",
    "    posWeights = weights.sort_values(weights.columns[0], ascending=False).head(N)\n",
    "    negWeights = weights.sort_values(weights.columns[0], ascending=True).head(N)\n",
    "    return posWeights, negWeights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "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>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <td>0.222606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <td>0.215934</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <td>0.198526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>EWDICHTE</th>\n",
       "      <td>0.195974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>HH_EINKOMMEN_SCORE</th>\n",
       "      <td>0.190977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <td>0.155443</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <td>0.152013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KBA05_ANTG4</th>\n",
       "      <td>0.151090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <td>0.148548</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ARBEIT</th>\n",
       "      <td>0.141607</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           0\n",
       "PLZ8_ANTG3          0.222606\n",
       "PLZ8_ANTG4          0.215934\n",
       "ORTSGR_KLS9         0.198526\n",
       "EWDICHTE            0.195974\n",
       "HH_EINKOMMEN_SCORE  0.190977\n",
       "FINANZ_SPARER       0.155443\n",
       "FINANZ_HAUSBAUER    0.152013\n",
       "KBA05_ANTG4         0.151090\n",
       "PLZ8_ANTG2          0.148548\n",
       "ARBEIT              0.141607"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Map weights for the first principal component to corresponding feature names\n",
    "# and then print the linked values, sorted by weight.\n",
    "# HINT: Try defining a function here or in a new cell that you can reuse in the\n",
    "# other cells.\n",
    "# and then print the linked values, sorted by weight.\n",
    "p, n = getWeights(azdiastemp_noNa_final, pca, 0, 10)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "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>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>MOBI_REGIO</th>\n",
       "      <td>-0.239428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KBA05_ANTG1</th>\n",
       "      <td>-0.222922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <td>-0.222529</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <td>-0.221995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KBA05_GBZ</th>\n",
       "      <td>-0.214801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <td>-0.166337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INNENSTADT</th>\n",
       "      <td>-0.165851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KONSUMNAEHE</th>\n",
       "      <td>-0.165035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <td>-0.137991</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BALLRAUM</th>\n",
       "      <td>-0.128487</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             0\n",
       "MOBI_REGIO           -0.239428\n",
       "KBA05_ANTG1          -0.222922\n",
       "FINANZ_MINIMALIST    -0.222529\n",
       "PLZ8_ANTG1           -0.221995\n",
       "KBA05_GBZ            -0.214801\n",
       "PLZ8_GBZ             -0.166337\n",
       "INNENSTADT           -0.165851\n",
       "KONSUMNAEHE          -0.165035\n",
       "ALTERSKATEGORIE_GROB -0.137991\n",
       "BALLRAUM             -0.128487"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Map weights for the second principal component to corresponding feature names\n",
    "# and then print the linked values, sorted by weight.\n",
    "p, n = getWeights(azdiastemp_noNa_final, pca, 1, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "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>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <td>0.252765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_ERL</th>\n",
       "      <td>0.235743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <td>0.217776</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_LUST</th>\n",
       "      <td>0.175344</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RETOURTYP_BK_S</th>\n",
       "      <td>0.161684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KRIT</th>\n",
       "      <td>0.130653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KAEM</th>\n",
       "      <td>0.122912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <td>0.122652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>W_KEIT_KIND_HH</th>\n",
       "      <td>0.118632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <td>0.104754</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             1\n",
       "ALTERSKATEGORIE_GROB  0.252765\n",
       "SEMIO_ERL             0.235743\n",
       "FINANZ_VORSORGER      0.217776\n",
       "SEMIO_LUST            0.175344\n",
       "RETOURTYP_BK_S        0.161684\n",
       "SEMIO_KRIT            0.130653\n",
       "SEMIO_KAEM            0.122912\n",
       "FINANZ_HAUSBAUER      0.122652\n",
       "W_KEIT_KIND_HH        0.118632\n",
       "PLZ8_ANTG3            0.104754"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "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>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>SEMIO_REL</th>\n",
       "      <td>-0.262923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_PFLICHT</th>\n",
       "      <td>-0.231305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KULT</th>\n",
       "      <td>-0.225357</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_TRADV</th>\n",
       "      <td>-0.225101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <td>-0.223603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <td>-0.217697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <td>-0.195253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_FAM</th>\n",
       "      <td>-0.184628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ONLINE_AFFINITAET</th>\n",
       "      <td>-0.164147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_RAT</th>\n",
       "      <td>-0.163079</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              1\n",
       "SEMIO_REL             -0.262923\n",
       "SEMIO_PFLICHT         -0.231305\n",
       "SEMIO_KULT            -0.225357\n",
       "SEMIO_TRADV           -0.225101\n",
       "FINANZ_SPARER         -0.223603\n",
       "FINANZ_UNAUFFAELLIGER -0.217697\n",
       "FINANZ_ANLEGER        -0.195253\n",
       "SEMIO_FAM             -0.184628\n",
       "ONLINE_AFFINITAET     -0.164147\n",
       "SEMIO_RAT             -0.163079"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Map weights for the third principal component to corresponding feature names\n",
    "# and then print the linked values, sorted by weight.\n",
    "p, n = getWeights(azdiastemp_noNa_final, pca, 2, 10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "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>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>SEMIO_VERT</th>\n",
       "      <td>0.345306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_SOZ</th>\n",
       "      <td>0.256961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_FAM</th>\n",
       "      <td>0.242939</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KULT</th>\n",
       "      <td>0.220989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <td>0.162318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RETOURTYP_BK_S</th>\n",
       "      <td>0.123113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <td>0.107471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>W_KEIT_KIND_HH</th>\n",
       "      <td>0.099203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <td>0.095271</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_LUST</th>\n",
       "      <td>0.081573</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             2\n",
       "SEMIO_VERT            0.345306\n",
       "SEMIO_SOZ             0.256961\n",
       "SEMIO_FAM             0.242939\n",
       "SEMIO_KULT            0.220989\n",
       "FINANZ_MINIMALIST     0.162318\n",
       "RETOURTYP_BK_S        0.123113\n",
       "FINANZ_VORSORGER      0.107471\n",
       "W_KEIT_KIND_HH        0.099203\n",
       "ALTERSKATEGORIE_GROB  0.095271\n",
       "SEMIO_LUST            0.081573"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "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>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <td>-0.364546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KAEM</th>\n",
       "      <td>-0.334261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_DOM</th>\n",
       "      <td>-0.311411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_KRIT</th>\n",
       "      <td>-0.264220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_RAT</th>\n",
       "      <td>-0.227216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <td>-0.195261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_ERL</th>\n",
       "      <td>-0.158497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <td>-0.111787</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <td>-0.103077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SEMIO_TRADV</th>\n",
       "      <td>-0.097060</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                              2\n",
       "ANREDE_KZ             -0.364546\n",
       "SEMIO_KAEM            -0.334261\n",
       "SEMIO_DOM             -0.311411\n",
       "SEMIO_KRIT            -0.264220\n",
       "SEMIO_RAT             -0.227216\n",
       "FINANZ_ANLEGER        -0.195261\n",
       "SEMIO_ERL             -0.158497\n",
       "FINANZ_SPARER         -0.111787\n",
       "FINANZ_UNAUFFAELLIGER -0.103077\n",
       "SEMIO_TRADV           -0.097060"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Discussion 2.3: Interpret Principal Components\n",
    "\n",
    "(Double-click this cell and replace this text with your own text, reporting your observations from detailed investigation of the first few principal components generated. Can we interpret positive and negative values from them in a meaningful way?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Clustering\n",
    "\n",
    "### Step 3.1: Apply Clustering to General Population\n",
    "\n",
    "You've assessed and cleaned the demographics data, then scaled and transformed them. Now, it's time to see how the data clusters in the principal components space. In this substep, you will apply k-means clustering to the dataset and use the average within-cluster distances from each point to their assigned cluster's centroid to decide on a number of clusters to keep.\n",
    "\n",
    "- Use sklearn's [KMeans](http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn.cluster.KMeans) class to perform k-means clustering on the PCA-transformed data.\n",
    "- Then, compute the average difference from each point to its assigned cluster's center. **Hint**: The KMeans object's `.score()` method might be useful here, but note that in sklearn, scores tend to be defined so that larger is better. Try applying it to a small, toy dataset, or use an internet search to help your understanding.\n",
    "- Perform the above two steps for a number of different cluster counts. You can then see how the average distance decreases with an increasing number of clusters. However, each additional cluster provides a smaller net benefit. Use this fact to select a final number of clusters in which to group the data. **Warning**: because of the large size of the dataset, it can take a long time for the algorithm to resolve. The more clusters to fit, the longer the algorithm will take. You should test for cluster counts through at least 10 clusters to get the full picture, but you shouldn't need to test for a number of clusters above about 30.\n",
    "- Once you've selected a final number of clusters to use, re-fit a KMeans instance to perform the clustering operation. Make sure that you also obtain the cluster assignments for the general demographics data, since you'll be using them in the final Step 3.3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster0 = KMeans(10)\n",
    "# result0 = cluster0.fit_transform(df_pca)\n",
    "# distance0 = np.min(result0, axis=1)\n",
    "# dump(cluster0, 'cluster0')\n",
    "# print(distance0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster1 = KMeans(11)\n",
    "# result1 = cluster1.fit_transform(df_pca)\n",
    "# distance1 = np.min(result1, axis=1)\n",
    "# dump(cluster1, 'cluster1')\n",
    "# print(distance1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster2 = KMeans(12)\n",
    "# result2 = cluster2.fit_transform(df_pca)\n",
    "# distance2 = np.min(result2, axis=1)\n",
    "# dump(cluster2, 'cluster2')\n",
    "# print(distance2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster3 = KMeans(13)\n",
    "# result3 = cluster3.fit_transform(df_pca)\n",
    "# distance3 = np.min(result3, axis=1)\n",
    "# dump(cluster3, 'cluster3')\n",
    "# print(distance3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster4 = KMeans(14)\n",
    "# result4 = cluster4.fit_transform(df_pca)\n",
    "# distance4 = np.min(result4, axis=1)\n",
    "# dump(cluster4, 'cluster4')\n",
    "# print(distance4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster5 = KMeans(15)\n",
    "# result5 = cluster5.fit_transform(df_pca)\n",
    "# distance5 = np.min(result5, axis=1)\n",
    "# dump(cluster5, 'cluster5')\n",
    "# print(distance5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster6 = KMeans(16)\n",
    "# result6 = cluster6.fit_transform(df_pca)\n",
    "# distance6 = np.min(result6, axis=1)\n",
    "# dump(cluster6, 'cluster6')\n",
    "# print(distance6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster7 = KMeans(17)\n",
    "# result7 = cluster7.fit_transform(df_pca)\n",
    "# distance7 = np.min(result7, axis=1)\n",
    "# dump(cluster7, 'cluster7')\n",
    "# print(distance7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster8 = KMeans(18)\n",
    "# result8 = cluster8.fit_transform(df_pca)\n",
    "# distance8 = np.min(result8, axis=1)\n",
    "# dump(cluster8, 'cluster8')\n",
    "# print(distance8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster9 = KMeans(19)\n",
    "# result9 = cluster9.fit_transform(df_pca)\n",
    "# distance9 = np.min(result9, axis=1)\n",
    "# dump(cluster9, 'cluster9')\n",
    "# print(distance9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster0 = load('cluster0')\n",
    "cluster1 = load('cluster1')\n",
    "cluster2 = load('cluster2')\n",
    "cluster3 = load('cluster3')\n",
    "cluster4 = load('cluster4')\n",
    "cluster5 = load('cluster5')\n",
    "cluster6 = load('cluster6')\n",
    "cluster7 = load('cluster7')\n",
    "cluster8 = load('cluster8')\n",
    "cluster9 = load('cluster9')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5.99920356  5.25322915  6.26760652 ...,  4.92803458  5.6262381\n",
      "  6.55423197]\n",
      "[ 5.99729642  5.19941681  5.80185611 ...,  4.91543697  5.49068444\n",
      "  6.23846466]\n",
      "[ 6.00321557  5.38155623  5.71584121 ...,  4.93191586  5.4907225\n",
      "  6.14040449]\n",
      "[ 6.00392766  5.37278726  5.7805062  ...,  4.93259741  5.48580282\n",
      "  6.21427945]\n",
      "[ 5.86017994  5.66220384  5.77913626 ...,  4.60380248  5.48047593\n",
      "  6.21966435]\n",
      "[ 5.99451958  5.32246457  5.88282276 ...,  4.92354011  5.46000209\n",
      "  5.49646623]\n",
      "[ 5.92187825  5.0573057   5.77767642 ...,  4.4821897   5.48267078\n",
      "  6.21543802]\n",
      "[ 6.0017645   5.51875852  6.02450863 ...,  4.92603213  5.42898455\n",
      "  5.90105521]\n",
      "[ 5.92034543  5.03588893  5.815679   ...,  4.48228893  5.49018974\n",
      "  6.33747584]\n",
      "[ 5.92737572  5.04822523  5.88351874 ...,  4.48506609  5.1921062\n",
      "  5.66267788]\n"
     ]
    }
   ],
   "source": [
    "result0 = cluster0.transform(df_pca)\n",
    "distance0 = np.min(result0, axis=1)\n",
    "print(distance0)\n",
    "result1 = cluster1.transform(df_pca)\n",
    "distance1 = np.min(result1, axis=1)\n",
    "print(distance1)\n",
    "result2 = cluster2.transform(df_pca)\n",
    "distance2 = np.min(result2, axis=1)\n",
    "print(distance2)\n",
    "result3 = cluster3.transform(df_pca)\n",
    "distance3 = np.min(result3, axis=1)\n",
    "print(distance3)\n",
    "result4 = cluster4.transform(df_pca)\n",
    "distance4 = np.min(result4, axis=1)\n",
    "print(distance4)\n",
    "result5 = cluster5.transform(df_pca)\n",
    "distance5 = np.min(result5, axis=1)\n",
    "print(distance5)\n",
    "result6 = cluster6.transform(df_pca)\n",
    "distance6 = np.min(result6, axis=1)\n",
    "print(distance6)\n",
    "result7 = cluster7.transform(df_pca)\n",
    "distance7 = np.min(result7, axis=1)\n",
    "print(distance7)\n",
    "result8 = cluster8.transform(df_pca)\n",
    "distance8 = np.min(result8, axis=1)\n",
    "print(distance8)\n",
    "result9 = cluster9.transform(df_pca)\n",
    "distance9 = np.min(result9, axis=1)\n",
    "print(distance9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultList = [distance0, distance1, distance2, distance3, distance4, distance5, distance6, distance7, distance8, distance9]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average of 0 is: 5.491091752413104\n",
      "Average of 1 is: 5.432722028994438\n",
      "Average of 2 is: 5.3953931307323435\n",
      "Average of 3 is: 5.347826619987956\n",
      "Average of 4 is: 5.317350436694105\n",
      "Average of 5 is: 5.286875239748684\n",
      "Average of 6 is: 5.249543196370822\n",
      "Average of 7 is: 5.242796093423207\n",
      "Average of 8 is: 5.207815480022285\n",
      "Average of 9 is: 5.191018052005388\n"
     ]
    }
   ],
   "source": [
    "resultListAvg = []\n",
    "for i, j in zip(resultList, range(0, len(resultList))):\n",
    "    resultListAvg.append(np.mean(i))\n",
    "    print(f'Average of {j} is: {resultListAvg[j]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0,0.5,'Distance to nearest cluster centre')"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 16536 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1a64b7cf8>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = range(10, 20)\n",
    "plt.plot(x, resultListAvg)\n",
    "plt.xlabel('Num of Clusters')\n",
    "plt.ylabel('Distance to nearest cluster centre')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Unfortunately I ran into memory issues on this VM I couldn't get any further even with refreshing the workspace. If this were a real piece of work - I would go back and optimise my code (I suspect I am wasting a lot of memory here) however I don't want to go back and change things as it currently works for this project.\n",
    "\n",
    "I am on my work laptop which is not powerful and I am unable to access my desktop at home. I hope you can see that the average distance to the centres is decreasing for increasing clusters!. \n",
    "\n",
    "I suspect that we will soon see this decrease slow down and eventually reach an optimum number of clusters.\n",
    "\n",
    "As such I am choosing **19 clusters** for the model.\n",
    "\n",
    "We have already got the results for this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  8.89676907,  11.18238063,   9.44552392, ...,   7.78952809,\n",
       "          8.01786661,  23.62727942],\n",
       "       [  7.98911824,   8.06703566,   7.30671633, ...,   8.28362807,\n",
       "          6.64715697,  23.42522223],\n",
       "       [  9.89697642,   6.97460371,   5.88351874, ...,   8.04631183,\n",
       "          9.80914733,  24.07789033],\n",
       "       ..., \n",
       "       [  9.49078454,  10.88099939,   8.75271193, ...,   7.85142571,\n",
       "          7.23220293,  23.94232482],\n",
       "       [ 11.30937033,  11.41794303,   9.05660614, ...,   7.33669112,\n",
       "         10.55966618,  24.50789177],\n",
       "       [  9.35820758,   7.75844345,   5.66267788, ...,   7.50301775,\n",
       "         10.07965876,  23.58627624]])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result9\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Discussion 3.1: Apply Clustering to General Population\n",
    "\n",
    "I have explained above what I've done but I will recap.\n",
    "\n",
    "We fitted KMeans to our final dataset starting at k=5 to k=14. Unfortunately I had to stop at k=14 due to memory problems but we can see that as we are increasing k the distance to the nearest cluster centre is decreasing. For higher k we will eventually find a sweet spot for our data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3.2: Apply All Steps to the Customer Data\n",
    "\n",
    "Now that you have clusters and cluster centers for the general population, it's time to see how the customer data maps on to those clusters. Take care to not confuse this for re-fitting all of the models to the customer data. Instead, you're going to use the fits from the general population to clean, transform, and cluster the customer data. In the last step of the project, you will interpret how the general population fits apply to the customer data.\n",
    "\n",
    "- Don't forget when loading in the customers data, that it is semicolon (`;`) delimited.\n",
    "- Apply the same feature wrangling, selection, and engineering steps to the customer demographics using the `clean_data()` function you created earlier. (You can assume that the customer demographics data has similar meaning behind missing data patterns as the general demographics data.)\n",
    "- Use the sklearn objects from the general demographics data, and apply their transformations to the customers data. That is, you should not be using a `.fit()` or `.fit_transform()` method to re-fit the old objects, nor should you be creating new sklearn objects! Carry the data through the feature scaling, PCA, and clustering steps, obtaining cluster assignments for all of the data in the customer demographics data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(191652, 85)\n"
     ]
    },
    {
     "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>AGER_TYP</th>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGER_TYP  ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  \\\n",
       "0         2                     4          1            5.0   \n",
       "1        -1                     4          1            NaN   \n",
       "2        -1                     4          2            2.0   \n",
       "\n",
       "   FINANZ_MINIMALIST  FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "0                  5              1                 5               1   \n",
       "1                  5              1                 5               1   \n",
       "2                  5              1                 5               1   \n",
       "\n",
       "   FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER    ...     PLZ8_ANTG1  PLZ8_ANTG2  \\\n",
       "0                      2                 2    ...            3.0         3.0   \n",
       "1                      3                 2    ...            NaN         NaN   \n",
       "2                      4                 4    ...            2.0         3.0   \n",
       "\n",
       "   PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         1.0         0.0          1.0       5.0       5.0     1.0   \n",
       "1         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "2         3.0         1.0          3.0       3.0       2.0     3.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          2.0       1.0  \n",
       "1          NaN       NaN  \n",
       "2          5.0       3.0  \n",
       "\n",
       "[3 rows x 85 columns]"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Load in the customer demographics data.\n",
    "customers = pd.read_csv('Udacity_CUSTOMERS_Subset.csv', sep=';')\n",
    "print(customers.shape)\n",
    "customers.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Apply preprocessing, feature transformation, and clustering from the general\n",
    "# demographics onto the customer data, obtaining cluster predictions for the\n",
    "# customer demographics data.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AGER_TYP 0\n",
      "ALTERSKATEGORIE_GROB 1\n",
      "ANREDE_KZ 2\n",
      "CJT_GESAMTTYP 3\n",
      "FINANZ_MINIMALIST 4\n",
      "FINANZ_SPARER 5\n",
      "FINANZ_VORSORGER 6\n",
      "FINANZ_ANLEGER 7\n",
      "FINANZ_UNAUFFAELLIGER 8\n",
      "FINANZ_HAUSBAUER 9\n",
      "FINANZTYP 10\n",
      "GEBURTSJAHR 11\n",
      "GFK_URLAUBERTYP 12\n",
      "GREEN_AVANTGARDE 13\n",
      "HEALTH_TYP 14\n",
      "LP_LEBENSPHASE_FEIN 15\n",
      "LP_LEBENSPHASE_GROB 16\n",
      "LP_FAMILIE_FEIN 17\n",
      "LP_FAMILIE_GROB 18\n",
      "LP_STATUS_FEIN 19\n",
      "LP_STATUS_GROB 20\n",
      "NATIONALITAET_KZ 21\n",
      "PRAEGENDE_JUGENDJAHRE 22\n",
      "RETOURTYP_BK_S 23\n",
      "SEMIO_SOZ 24\n",
      "SEMIO_FAM 25\n",
      "SEMIO_REL 26\n",
      "SEMIO_MAT 27\n",
      "SEMIO_VERT 28\n",
      "SEMIO_LUST 29\n",
      "SEMIO_ERL 30\n",
      "SEMIO_KULT 31\n",
      "SEMIO_RAT 32\n",
      "SEMIO_KRIT 33\n",
      "SEMIO_DOM 34\n",
      "SEMIO_KAEM 35\n",
      "SEMIO_PFLICHT 36\n",
      "SEMIO_TRADV 37\n",
      "SHOPPER_TYP 38\n",
      "SOHO_KZ 39\n",
      "TITEL_KZ 40\n",
      "VERS_TYP 41\n",
      "ZABEOTYP 42\n",
      "ALTER_HH 43\n",
      "ANZ_PERSONEN 44\n",
      "ANZ_TITEL 45\n",
      "HH_EINKOMMEN_SCORE 46\n",
      "KK_KUNDENTYP 47\n",
      "W_KEIT_KIND_HH 48\n",
      "WOHNDAUER_2008 49\n",
      "ANZ_HAUSHALTE_AKTIV 50\n",
      "ANZ_HH_TITEL 51\n",
      "GEBAEUDETYP 52\n",
      "KONSUMNAEHE 53\n",
      "MIN_GEBAEUDEJAHR 54\n",
      "OST_WEST_KZ 55\n",
      "WOHNLAGE 56\n",
      "CAMEO_DEUG_2015 57\n",
      "CAMEO_DEU_2015 58\n",
      "CAMEO_INTL_2015 59\n",
      "KBA05_ANTG1 60\n",
      "KBA05_ANTG2 61\n",
      "KBA05_ANTG3 62\n",
      "KBA05_ANTG4 63\n",
      "KBA05_BAUMAX 64\n",
      "KBA05_GBZ 65\n",
      "BALLRAUM 66\n",
      "EWDICHTE 67\n",
      "INNENSTADT 68\n",
      "GEBAEUDETYP_RASTER 69\n",
      "KKK 70\n",
      "MOBI_REGIO 71\n",
      "ONLINE_AFFINITAET 72\n",
      "REGIOTYP 73\n",
      "KBA13_ANZAHL_PKW 74\n",
      "PLZ8_ANTG1 75\n",
      "PLZ8_ANTG2 76\n",
      "PLZ8_ANTG3 77\n",
      "PLZ8_ANTG4 78\n",
      "PLZ8_BAUMAX 79\n",
      "PLZ8_HHZ 80\n",
      "PLZ8_GBZ 81\n",
      "ARBEIT 82\n",
      "ORTSGR_KLS9 83\n",
      "RELAT_AB 84\n"
     ]
    }
   ],
   "source": [
    "# Fill in missing values:\n",
    "\n",
    "for col, index in zip(customers, range(len(missing_list))):\n",
    "    print(col, index)\n",
    "    customers.iloc[:,index] = customers.iloc[:,index].apply(replace, items=missing_list[index], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "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>AGER_TYP</th>\n",
       "      <th>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>...</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_BAUMAX</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>6.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 85 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   AGER_TYP  ALTERSKATEGORIE_GROB  ANREDE_KZ  CJT_GESAMTTYP  \\\n",
       "0       2.0                   4.0          1            5.0   \n",
       "1       NaN                   4.0          1            NaN   \n",
       "2       NaN                   4.0          2            2.0   \n",
       "3       1.0                   4.0          1            2.0   \n",
       "4       NaN                   3.0          1            6.0   \n",
       "5       1.0                   3.0          1            4.0   \n",
       "6       2.0                   4.0          1            2.0   \n",
       "7       1.0                   4.0          1            2.0   \n",
       "8       2.0                   4.0          2            1.0   \n",
       "9       1.0                   3.0          1            3.0   \n",
       "\n",
       "   FINANZ_MINIMALIST  FINANZ_SPARER  FINANZ_VORSORGER  FINANZ_ANLEGER  \\\n",
       "0                  5              1                 5               1   \n",
       "1                  5              1                 5               1   \n",
       "2                  5              1                 5               1   \n",
       "3                  5              1                 5               2   \n",
       "4                  3              1                 4               4   \n",
       "5                  5              1                 5               1   \n",
       "6                  5              1                 5               1   \n",
       "7                  5              1                 5               1   \n",
       "8                  2              2                 5               1   \n",
       "9                  5              2                 4               1   \n",
       "\n",
       "   FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER    ...     PLZ8_ANTG1  PLZ8_ANTG2  \\\n",
       "0                      2                 2    ...            3.0         3.0   \n",
       "1                      3                 2    ...            NaN         NaN   \n",
       "2                      4                 4    ...            2.0         3.0   \n",
       "3                      1                 2    ...            3.0         2.0   \n",
       "4                      5                 2    ...            2.0         4.0   \n",
       "5                      2                 3    ...            2.0         3.0   \n",
       "6                      1                 2    ...            3.0         2.0   \n",
       "7                      2                 2    ...            3.0         3.0   \n",
       "8                      1                 5    ...            1.0         4.0   \n",
       "9                      3                 1    ...            3.0         3.0   \n",
       "\n",
       "   PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         1.0         0.0          1.0       5.0       5.0     1.0   \n",
       "1         NaN         NaN          NaN       NaN       NaN     NaN   \n",
       "2         3.0         1.0          3.0       3.0       2.0     3.0   \n",
       "3         1.0         0.0          1.0       3.0       4.0     1.0   \n",
       "4         2.0         1.0          2.0       3.0       3.0     3.0   \n",
       "5         2.0         1.0          1.0       5.0       5.0     3.0   \n",
       "6         1.0         0.0          1.0       5.0       5.0     2.0   \n",
       "7         1.0         1.0          1.0       3.0       3.0     3.0   \n",
       "8         2.0         1.0          5.0       5.0       4.0     3.0   \n",
       "9         1.0         0.0          1.0       5.0       5.0     3.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          2.0       1.0  \n",
       "1          NaN       NaN  \n",
       "2          5.0       3.0  \n",
       "3          3.0       1.0  \n",
       "4          5.0       1.0  \n",
       "5          7.0       5.0  \n",
       "6          3.0       2.0  \n",
       "7          4.0       3.0  \n",
       "8          8.0       3.0  \n",
       "9          6.0       4.0  \n",
       "\n",
       "[10 rows x 85 columns]"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TITEL_KZ          0.987936\n",
       "KK_KUNDENTYP      0.584064\n",
       "KBA05_BAUMAX      0.571531\n",
       "AGER_TYP          0.504759\n",
       "GEBURTSJAHR       0.485380\n",
       "ALTER_HH          0.358707\n",
       "KKK               0.313401\n",
       "REGIOTYP          0.313401\n",
       "W_KEIT_KIND_HH    0.297085\n",
       "KBA05_GBZ         0.292102\n",
       "dtype: float64"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Find missing data by col\n",
    "\n",
    "null_col_count_cust = customers.isnull().sum(axis=0)\n",
    "ax_rows_cust = customers.shape[0]\n",
    "anomalies_cust = null_col_count_cust.apply(findTotal, total=ax_rows_cust).sort_values(ascending=False)\n",
    "anomalies_cust[0:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "In the original dataset we dropped anything below 34.8%, I will do the same here so we will drop the first six of the above list\n",
    "\n",
    "As we did above, I am keeping all rows regardless of how much is missing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [],
   "source": [
    "customers.drop(['TITEL_KZ', 'KK_KUNDENTYP', 'KBA05_BAUMAX', 'AGER_TYP', 'GEBURTSJAHR', 'ALTER_HH'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert customers.shape[1]+6 == 85"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now look at reencoding our categorical features "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ANREDE_KZ', 'CJT_GESAMTTYP', 'FINANZTYP', 'GFK_URLAUBERTYP', 'GREEN_AVANTGARDE', 'LP_FAMILIE_FEIN', 'LP_FAMILIE_GROB', 'LP_STATUS_FEIN', 'LP_STATUS_GROB', 'NATIONALITAET_KZ', 'SHOPPER_TYP', 'SOHO_KZ', 'VERS_TYP', 'ZABEOTYP', 'GEBAEUDETYP', 'OST_WEST_KZ', 'CAMEO_DEUG_2015', 'CAMEO_DEU_2015']\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "categorical_cols = feat_info_cat['attribute'].tolist()\n",
    "\n",
    "drop_cols = ['AGER_TYP', 'TITEL_KZ', 'KK_KUNDENTYP']\n",
    "\n",
    "for i in drop_cols:\n",
    "    categorical_cols.remove(i)\n",
    "    \n",
    "print(categorical_cols)\n",
    "print(len(categorical_cols))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ANREDE_KZ\n",
      "Values: [1 2]\n",
      "Length: 2\n",
      "FINANZTYP\n",
      "Values: [2 6 5 3 1 4]\n",
      "Length: 6\n",
      "GFK_URLAUBERTYP\n",
      "Values: [  4.  nan   3.  10.   2.  11.   8.   1.   5.   9.  12.   7.   6.]\n",
      "Length: 13\n",
      "GREEN_AVANTGARDE\n",
      "Values: [1 0]\n",
      "Length: 2\n",
      "LP_FAMILIE_GROB\n",
      "Values: [  2.  nan   1.   5.   4.   3.]\n",
      "Length: 6\n",
      "LP_STATUS_FEIN\n",
      "Values: [ 10.  nan   9.   1.   7.   6.   3.   5.   8.   4.   2.]\n",
      "Length: 11\n",
      "LP_STATUS_GROB\n",
      "Values: [  5.  nan   4.   1.   3.   2.]\n",
      "Length: 6\n",
      "NATIONALITAET_KZ\n",
      "Values: [  1.   2.  nan   3.]\n",
      "Length: 4\n",
      "SHOPPER_TYP\n",
      "Values: [  3.   1.   0.   2.  nan]\n",
      "Length: 5\n",
      "SOHO_KZ\n",
      "Values: [  0.  nan   1.]\n",
      "Length: 3\n",
      "VERS_TYP\n",
      "Values: [  1.   2.  nan]\n",
      "Length: 3\n",
      "ZABEOTYP\n",
      "Values: [3 1 2 4 6 5]\n",
      "Length: 6\n",
      "GEBAEUDETYP\n",
      "Values: [  1.  nan   8.   2.   3.   4.   6.]\n",
      "Length: 7\n",
      "OST_WEST_KZ\n",
      "Values: ['W' nan 'O']\n",
      "Length: 3\n",
      "CAMEO_DEUG_2015\n",
      "Values: ['1' nan '5' '4' '7' '3' '9' '2' '6' '8']\n",
      "Length: 10\n",
      "CAMEO_DEU_2015\n",
      "Values: ['1A' nan '5D' '4C' '7B' '3B' '1D' '9E' '2D' '4A' '6B' '9D' '8B' '5C' '9C'\n",
      " '4E' '6C' '8C' '8A' '5B' '9B' '3D' '2A' '3C' '5F' '7A' '1E' '2C' '7C' '5A'\n",
      " '2B' '6D' '7E' '5E' '6E' '3A' '9A' '4B' '1C' '1B' '6A' '8D' '7D' '6F' '4D']\n",
      "Length: 45\n",
      "\n",
      " Columns to reencode as dummies: ['CJT_GESAMTTYP', 'FINANZTYP', 'GFK_URLAUBERTYP', 'LP_FAMILIE_FEIN', 'LP_FAMILIE_GROB', 'LP_STATUS_FEIN', 'LP_STATUS_GROB', 'NATIONALITAET_KZ', 'SHOPPER_TYP', 'SOHO_KZ', 'VERS_TYP', 'ZABEOTYP', 'GEBAEUDETYP', 'OST_WEST_KZ', 'CAMEO_DEUG_2015', 'CAMEO_DEU_2015']\n"
     ]
    }
   ],
   "source": [
    "# Re-encode categorical variable(s) to be kept in the analysis.\n",
    "for i in categorical_cols:\n",
    "    print(f'{i}\\nValues: {customers[i].unique()}\\nLength: {len(customers[i].unique())}')\n",
    "    if len(customers[i].unique()) == 2:\n",
    "        categorical_cols.remove(i)\n",
    "print('\\n Columns to reencode as dummies:', categorical_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(categorical_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "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>CJT_GESAMTTYP</th>\n",
       "      <th>FINANZTYP</th>\n",
       "      <th>GFK_URLAUBERTYP</th>\n",
       "      <th>LP_FAMILIE_FEIN</th>\n",
       "      <th>LP_FAMILIE_GROB</th>\n",
       "      <th>LP_STATUS_FEIN</th>\n",
       "      <th>LP_STATUS_GROB</th>\n",
       "      <th>NATIONALITAET_KZ</th>\n",
       "      <th>SHOPPER_TYP</th>\n",
       "      <th>SOHO_KZ</th>\n",
       "      <th>VERS_TYP</th>\n",
       "      <th>ZABEOTYP</th>\n",
       "      <th>GEBAEUDETYP</th>\n",
       "      <th>OST_WEST_KZ</th>\n",
       "      <th>CAMEO_DEUG_2015</th>\n",
       "      <th>CAMEO_DEU_2015</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1.0</td>\n",
       "      <td>W</td>\n",
       "      <td>1</td>\n",
       "      <td>1A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>8.0</td>\n",
       "      <td>W</td>\n",
       "      <td>5</td>\n",
       "      <td>5D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>W</td>\n",
       "      <td>4</td>\n",
       "      <td>4C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>W</td>\n",
       "      <td>7</td>\n",
       "      <td>7B</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   CJT_GESAMTTYP  FINANZTYP  GFK_URLAUBERTYP  LP_FAMILIE_FEIN  \\\n",
       "0            5.0          2              4.0              2.0   \n",
       "1            NaN          2              NaN              NaN   \n",
       "2            2.0          2              3.0              1.0   \n",
       "3            2.0          6             10.0              NaN   \n",
       "4            6.0          2              2.0             10.0   \n",
       "\n",
       "   LP_FAMILIE_GROB  LP_STATUS_FEIN  LP_STATUS_GROB  NATIONALITAET_KZ  \\\n",
       "0              2.0            10.0             5.0               1.0   \n",
       "1              NaN             NaN             NaN               1.0   \n",
       "2              1.0            10.0             5.0               1.0   \n",
       "3              NaN             9.0             4.0               1.0   \n",
       "4              5.0             1.0             1.0               1.0   \n",
       "\n",
       "   SHOPPER_TYP  SOHO_KZ  VERS_TYP  ZABEOTYP  GEBAEUDETYP OST_WEST_KZ  \\\n",
       "0          3.0      0.0       1.0         3          1.0           W   \n",
       "1          3.0      0.0       1.0         3          NaN         NaN   \n",
       "2          1.0      0.0       2.0         3          8.0           W   \n",
       "3          0.0      0.0       1.0         1          2.0           W   \n",
       "4          1.0      0.0       2.0         1          3.0           W   \n",
       "\n",
       "  CAMEO_DEUG_2015 CAMEO_DEU_2015  \n",
       "0               1             1A  \n",
       "1             NaN            NaN  \n",
       "2               5             5D  \n",
       "3               4             4C  \n",
       "4               7             7B  "
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers[categorical_cols].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "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>CJT_GESAMTTYP_1.0</th>\n",
       "      <th>CJT_GESAMTTYP_2.0</th>\n",
       "      <th>CJT_GESAMTTYP_3.0</th>\n",
       "      <th>CJT_GESAMTTYP_4.0</th>\n",
       "      <th>CJT_GESAMTTYP_5.0</th>\n",
       "      <th>CJT_GESAMTTYP_6.0</th>\n",
       "      <th>CJT_GESAMTTYP_nan</th>\n",
       "      <th>FINANZTYP_1</th>\n",
       "      <th>FINANZTYP_2</th>\n",
       "      <th>FINANZTYP_3</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_DEU_2015_8A</th>\n",
       "      <th>CAMEO_DEU_2015_8B</th>\n",
       "      <th>CAMEO_DEU_2015_8C</th>\n",
       "      <th>CAMEO_DEU_2015_8D</th>\n",
       "      <th>CAMEO_DEU_2015_9A</th>\n",
       "      <th>CAMEO_DEU_2015_9B</th>\n",
       "      <th>CAMEO_DEU_2015_9C</th>\n",
       "      <th>CAMEO_DEU_2015_9D</th>\n",
       "      <th>CAMEO_DEU_2015_9E</th>\n",
       "      <th>CAMEO_DEU_2015_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 147 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   CJT_GESAMTTYP_1.0  CJT_GESAMTTYP_2.0  CJT_GESAMTTYP_3.0  CJT_GESAMTTYP_4.0  \\\n",
       "0                  0                  0                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  1                  0                  0   \n",
       "3                  0                  1                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CJT_GESAMTTYP_5.0  CJT_GESAMTTYP_6.0  CJT_GESAMTTYP_nan  FINANZTYP_1  \\\n",
       "0                  1                  0                  0            0   \n",
       "1                  0                  0                  1            0   \n",
       "2                  0                  0                  0            0   \n",
       "3                  0                  0                  0            0   \n",
       "4                  0                  1                  0            0   \n",
       "\n",
       "   FINANZTYP_2  FINANZTYP_3         ...          CAMEO_DEU_2015_8A  \\\n",
       "0            1            0         ...                          0   \n",
       "1            1            0         ...                          0   \n",
       "2            1            0         ...                          0   \n",
       "3            0            0         ...                          0   \n",
       "4            1            0         ...                          0   \n",
       "\n",
       "   CAMEO_DEU_2015_8B  CAMEO_DEU_2015_8C  CAMEO_DEU_2015_8D  CAMEO_DEU_2015_9A  \\\n",
       "0                  0                  0                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  0                  0   \n",
       "3                  0                  0                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CAMEO_DEU_2015_9B  CAMEO_DEU_2015_9C  CAMEO_DEU_2015_9D  CAMEO_DEU_2015_9E  \\\n",
       "0                  0                  0                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  0                  0   \n",
       "3                  0                  0                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "\n",
       "   CAMEO_DEU_2015_nan  \n",
       "0                   0  \n",
       "1                   1  \n",
       "2                   0  \n",
       "3                   0  \n",
       "4                   0  \n",
       "\n",
       "[5 rows x 147 columns]"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers_cat_dummies = pd.get_dummies(customers[categorical_cols].astype(str))\n",
    "customers_cat_dummies.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "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>attribute</th>\n",
       "      <th>information_level</th>\n",
       "      <th>type</th>\n",
       "      <th>missing_or_unknown</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>LP_LEBENSPHASE_FEIN</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>LP_LEBENSPHASE_GROB</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>PRAEGENDE_JUGENDJAHRE</td>\n",
       "      <td>person</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>WOHNLAGE</td>\n",
       "      <td>building</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>CAMEO_INTL_2015</td>\n",
       "      <td>microcell_rr4</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,XX]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>KBA05_BAUMAX</td>\n",
       "      <td>microcell_rr3</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>79</th>\n",
       "      <td>PLZ8_BAUMAX</td>\n",
       "      <td>macrocell_plz8</td>\n",
       "      <td>mixed</td>\n",
       "      <td>[-1,0]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                attribute information_level   type missing_or_unknown\n",
       "15    LP_LEBENSPHASE_FEIN            person  mixed                [0]\n",
       "16    LP_LEBENSPHASE_GROB            person  mixed                [0]\n",
       "22  PRAEGENDE_JUGENDJAHRE            person  mixed             [-1,0]\n",
       "56               WOHNLAGE          building  mixed               [-1]\n",
       "59        CAMEO_INTL_2015     microcell_rr4  mixed            [-1,XX]\n",
       "64           KBA05_BAUMAX     microcell_rr3  mixed             [-1,0]\n",
       "79            PLZ8_BAUMAX    macrocell_plz8  mixed             [-1,0]"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat_info_mixed = feat_info[(feat_info['type']=='mixed')]\n",
    "feat_info_mixed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['LP_LEBENSPHASE_FEIN', 'LP_LEBENSPHASE_GROB', 'PRAEGENDE_JUGENDJAHRE', 'WOHNLAGE', 'CAMEO_INTL_2015', 'KBA05_BAUMAX', 'PLZ8_BAUMAX']\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "mixed_cols = feat_info_mixed['attribute'].tolist()\n",
    "\n",
    "# drop_cols = ['AGER_TYP', 'TITEL_KZ', 'KK_KUNDENTYP']\n",
    "\n",
    "# for i in drop_cols:\n",
    "#     mixed_cols.remove(i)\n",
    "    \n",
    "print(mixed_cols)\n",
    "print(len(mixed_cols))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    50.0\n",
       "1     NaN\n",
       "2    50.0\n",
       "3    40.0\n",
       "4    70.0\n",
       "Name: PRAEGENDE_JUGENDJAHRE_decade, dtype: float64"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers['PRAEGENDE_JUGENDJAHRE_decade'] = customers.loc[:,'PRAEGENDE_JUGENDJAHRE'].apply(PRAEGENDE_JUGENDJAHRE_decade, axis=1)\n",
    "customers['PRAEGENDE_JUGENDJAHRE_decade'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    avantgarde\n",
       "1           NaN\n",
       "2    avantgarde\n",
       "3    mainstream\n",
       "4    mainstream\n",
       "Name: PRAEGENDE_JUGENDJAHRE_movement, dtype: object"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers['PRAEGENDE_JUGENDJAHRE_movement'] = customers.loc[:,'PRAEGENDE_JUGENDJAHRE'].apply(PRAEGENDE_JUGENDJAHRE_movement, axis=1)\n",
    "customers['PRAEGENDE_JUGENDJAHRE_movement'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_decade</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50.0</td>\n",
       "      <td>avantgarde</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>50.0</td>\n",
       "      <td>avantgarde</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40.0</td>\n",
       "      <td>mainstream</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>mainstream</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PRAEGENDE_JUGENDJAHRE_decade PRAEGENDE_JUGENDJAHRE_movement\n",
       "0                          50.0                     avantgarde\n",
       "1                           NaN                            NaN\n",
       "2                          50.0                     avantgarde\n",
       "3                          40.0                     mainstream\n",
       "4                          70.0                     mainstream"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers.iloc[0:5, -2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      1\n",
       "1    NaN\n",
       "2      3\n",
       "3      2\n",
       "4      4\n",
       "Name: CAMEO_INTL_2015_wealth, dtype: object"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers['CAMEO_INTL_2015_wealth'] = customers.loc[:,'CAMEO_INTL_2015'].apply(CAMEO_INTL_2015_wealth, axis=1)\n",
    "customers['CAMEO_INTL_2015_wealth'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      3\n",
       "1    NaN\n",
       "2      4\n",
       "3      4\n",
       "4      1\n",
       "Name: CAMEO_INTL_2015_life, dtype: object"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers['CAMEO_INTL_2015_life'] = customers.loc[:,'CAMEO_INTL_2015'].apply(CAMEO_INTL_2015_life, axis=1)\n",
    "customers['CAMEO_INTL_2015_life'].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     13\n",
       "1    NaN\n",
       "2     34\n",
       "3     24\n",
       "4     41\n",
       "Name: CAMEO_INTL_2015, dtype: object"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers['CAMEO_INTL_2015'].iloc[0:5][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_movement</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade</th>\n",
       "      <th>CAMEO_INTL_2015_wealth</th>\n",
       "      <th>CAMEO_INTL_2015_life</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>avantgarde</td>\n",
       "      <td>50.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>avantgarde</td>\n",
       "      <td>50.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mainstream</td>\n",
       "      <td>40.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mainstream</td>\n",
       "      <td>70.0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  PRAEGENDE_JUGENDJAHRE_movement  PRAEGENDE_JUGENDJAHRE_decade  \\\n",
       "0                     avantgarde                          50.0   \n",
       "1                            NaN                           NaN   \n",
       "2                     avantgarde                          50.0   \n",
       "3                     mainstream                          40.0   \n",
       "4                     mainstream                          70.0   \n",
       "\n",
       "  CAMEO_INTL_2015_wealth CAMEO_INTL_2015_life  \n",
       "0                      1                    3  \n",
       "1                    NaN                  NaN  \n",
       "2                      3                    4  \n",
       "3                      2                    4  \n",
       "4                      4                    1  "
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newCols = ['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade', 'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life']\n",
    "customers[newCols].head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "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>PRAEGENDE_JUGENDJAHRE_movement_avantgarde</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement_mainstream</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_movement_nan</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_40.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_50.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_60.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_70.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_80.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_90.0</th>\n",
       "      <th>PRAEGENDE_JUGENDJAHRE_decade_nan</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_3</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_4</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_5</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_nan</th>\n",
       "      <th>CAMEO_INTL_2015_life_1</th>\n",
       "      <th>CAMEO_INTL_2015_life_2</th>\n",
       "      <th>CAMEO_INTL_2015_life_3</th>\n",
       "      <th>CAMEO_INTL_2015_life_4</th>\n",
       "      <th>CAMEO_INTL_2015_life_5</th>\n",
       "      <th>CAMEO_INTL_2015_life_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   PRAEGENDE_JUGENDJAHRE_movement_avantgarde  \\\n",
       "0                                          1   \n",
       "1                                          0   \n",
       "2                                          1   \n",
       "3                                          0   \n",
       "4                                          0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_movement_mainstream  \\\n",
       "0                                          0   \n",
       "1                                          0   \n",
       "2                                          0   \n",
       "3                                          1   \n",
       "4                                          1   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_movement_nan  PRAEGENDE_JUGENDJAHRE_decade_40.0  \\\n",
       "0                                   0                                  0   \n",
       "1                                   1                                  0   \n",
       "2                                   0                                  0   \n",
       "3                                   0                                  1   \n",
       "4                                   0                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_50.0  PRAEGENDE_JUGENDJAHRE_decade_60.0  \\\n",
       "0                                  1                                  0   \n",
       "1                                  0                                  0   \n",
       "2                                  1                                  0   \n",
       "3                                  0                                  0   \n",
       "4                                  0                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_70.0  PRAEGENDE_JUGENDJAHRE_decade_80.0  \\\n",
       "0                                  0                                  0   \n",
       "1                                  0                                  0   \n",
       "2                                  0                                  0   \n",
       "3                                  0                                  0   \n",
       "4                                  1                                  0   \n",
       "\n",
       "   PRAEGENDE_JUGENDJAHRE_decade_90.0  PRAEGENDE_JUGENDJAHRE_decade_nan  \\\n",
       "0                                  0                                 0   \n",
       "1                                  0                                 1   \n",
       "2                                  0                                 0   \n",
       "3                                  0                                 0   \n",
       "4                                  0                                 0   \n",
       "\n",
       "             ...             CAMEO_INTL_2015_wealth_3  \\\n",
       "0            ...                                    0   \n",
       "1            ...                                    0   \n",
       "2            ...                                    1   \n",
       "3            ...                                    0   \n",
       "4            ...                                    0   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_4  CAMEO_INTL_2015_wealth_5  \\\n",
       "0                         0                         0   \n",
       "1                         0                         0   \n",
       "2                         0                         0   \n",
       "3                         0                         0   \n",
       "4                         1                         0   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_nan  CAMEO_INTL_2015_life_1  CAMEO_INTL_2015_life_2  \\\n",
       "0                           0                       0                       0   \n",
       "1                           1                       0                       0   \n",
       "2                           0                       0                       0   \n",
       "3                           0                       0                       0   \n",
       "4                           0                       1                       0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_3  CAMEO_INTL_2015_life_4  CAMEO_INTL_2015_life_5  \\\n",
       "0                       1                       0                       0   \n",
       "1                       0                       0                       0   \n",
       "2                       0                       1                       0   \n",
       "3                       0                       1                       0   \n",
       "4                       0                       0                       0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_nan  \n",
       "0                         0  \n",
       "1                         1  \n",
       "2                         0  \n",
       "3                         0  \n",
       "4                         0  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers_mixed_dummies = pd.get_dummies(customers[newCols].astype(str))\n",
    "customers_mixed_dummies.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "customers.drop(['PRAEGENDE_JUGENDJAHRE_movement', 'PRAEGENDE_JUGENDJAHRE_decade',\n",
    "       'CAMEO_INTL_2015_wealth', 'CAMEO_INTL_2015_life'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "79"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(customers.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16\n",
      "7\n",
      "\n",
      "147\n",
      "22\n"
     ]
    }
   ],
   "source": [
    "print(len(categorical_cols))\n",
    "print(len(mixed_cols))\n",
    "print()\n",
    "print(len(customers_cat_dummies.columns))\n",
    "print(len(customers_mixed_dummies.columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [],
   "source": [
    "mixed_cols.remove('KBA05_BAUMAX')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['LP_LEBENSPHASE_FEIN',\n",
       " 'LP_LEBENSPHASE_GROB',\n",
       " 'PRAEGENDE_JUGENDJAHRE',\n",
       " 'WOHNLAGE',\n",
       " 'CAMEO_INTL_2015',\n",
       " 'PLZ8_BAUMAX']"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mixed_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [],
   "source": [
    "customers.drop(categorical_cols, axis=1, inplace=True)\n",
    "customers.drop(mixed_cols, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>GREEN_AVANTGARDE</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1201.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 57 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "0                   4.0          1                  5              1   \n",
       "1                   4.0          1                  5              1   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "0                 5               1                      2                 2   \n",
       "1                 5               1                      3                 2   \n",
       "\n",
       "   GREEN_AVANTGARDE  HEALTH_TYP    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "0                 1         1.0    ...               1201.0         3.0   \n",
       "1                 0         1.0    ...                  NaN         NaN   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         3.0         1.0         0.0       5.0       5.0     1.0   \n",
       "1         NaN         NaN         NaN       NaN       NaN     NaN   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          2.0       1.0  \n",
       "1          NaN       NaN  \n",
       "\n",
       "[2 rows x 57 columns]"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerstemp = customers\n",
    "customerstemp.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {},
   "outputs": [],
   "source": [
    "customerstemp = customerstemp.join(customers_cat_dummies)\n",
    "customerstemp = customerstemp.join(customers_mixed_dummies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>GREEN_AVANTGARDE</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>...</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_3</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_4</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_5</th>\n",
       "      <th>CAMEO_INTL_2015_wealth_nan</th>\n",
       "      <th>CAMEO_INTL_2015_life_1</th>\n",
       "      <th>CAMEO_INTL_2015_life_2</th>\n",
       "      <th>CAMEO_INTL_2015_life_3</th>\n",
       "      <th>CAMEO_INTL_2015_life_4</th>\n",
       "      <th>CAMEO_INTL_2015_life_5</th>\n",
       "      <th>CAMEO_INTL_2015_life_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 226 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "0                   4.0          1                  5              1   \n",
       "1                   4.0          1                  5              1   \n",
       "2                   4.0          2                  5              1   \n",
       "3                   4.0          1                  5              1   \n",
       "4                   3.0          1                  3              1   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "0                 5               1                      2                 2   \n",
       "1                 5               1                      3                 2   \n",
       "2                 5               1                      4                 4   \n",
       "3                 5               2                      1                 2   \n",
       "4                 4               4                      5                 2   \n",
       "\n",
       "   GREEN_AVANTGARDE  HEALTH_TYP            ...             \\\n",
       "0                 1         1.0            ...              \n",
       "1                 0         1.0            ...              \n",
       "2                 1         2.0            ...              \n",
       "3                 0         2.0            ...              \n",
       "4                 0         3.0            ...              \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_3  CAMEO_INTL_2015_wealth_4  \\\n",
       "0                         0                         0   \n",
       "1                         0                         0   \n",
       "2                         1                         0   \n",
       "3                         0                         0   \n",
       "4                         0                         1   \n",
       "\n",
       "   CAMEO_INTL_2015_wealth_5  CAMEO_INTL_2015_wealth_nan  \\\n",
       "0                         0                           0   \n",
       "1                         0                           1   \n",
       "2                         0                           0   \n",
       "3                         0                           0   \n",
       "4                         0                           0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_1  CAMEO_INTL_2015_life_2  CAMEO_INTL_2015_life_3  \\\n",
       "0                       0                       0                       1   \n",
       "1                       0                       0                       0   \n",
       "2                       0                       0                       0   \n",
       "3                       0                       0                       0   \n",
       "4                       1                       0                       0   \n",
       "\n",
       "   CAMEO_INTL_2015_life_4  CAMEO_INTL_2015_life_5  CAMEO_INTL_2015_life_nan  \n",
       "0                       0                       0                         0  \n",
       "1                       0                       0                         1  \n",
       "2                       1                       0                         0  \n",
       "3                       1                       0                         0  \n",
       "4                       0                       0                         0  \n",
       "\n",
       "[5 rows x 226 columns]"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerstemp.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will now apply feature scaling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "75520\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(116132, 226)"
      ]
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(customerstemp.shape[0] - customerstemp.dropna().shape[0])\n",
    "customerstemp_noNa = customerstemp.dropna()\n",
    "customerstemp_noNa.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    },
    {
     "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>ALTERSKATEGORIE_GROB</th>\n",
       "      <th>ANREDE_KZ</th>\n",
       "      <th>FINANZ_MINIMALIST</th>\n",
       "      <th>FINANZ_SPARER</th>\n",
       "      <th>FINANZ_VORSORGER</th>\n",
       "      <th>FINANZ_ANLEGER</th>\n",
       "      <th>FINANZ_UNAUFFAELLIGER</th>\n",
       "      <th>FINANZ_HAUSBAUER</th>\n",
       "      <th>HEALTH_TYP</th>\n",
       "      <th>RETOURTYP_BK_S</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1201.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.0</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>2.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>433.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>513.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1167.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4 rows × 56 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   ALTERSKATEGORIE_GROB  ANREDE_KZ  FINANZ_MINIMALIST  FINANZ_SPARER  \\\n",
       "0                   4.0          1                  5              1   \n",
       "2                   4.0          2                  5              1   \n",
       "4                   3.0          1                  3              1   \n",
       "5                   3.0          1                  5              1   \n",
       "\n",
       "   FINANZ_VORSORGER  FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER  FINANZ_HAUSBAUER  \\\n",
       "0                 5               1                      2                 2   \n",
       "2                 5               1                      4                 4   \n",
       "4                 4               4                      5                 2   \n",
       "5                 5               1                      2                 3   \n",
       "\n",
       "   HEALTH_TYP  RETOURTYP_BK_S    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "0         1.0             5.0    ...               1201.0         3.0   \n",
       "2         2.0             5.0    ...                433.0         2.0   \n",
       "4         3.0             5.0    ...                513.0         2.0   \n",
       "5         3.0             3.0    ...               1167.0         2.0   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ  ARBEIT  \\\n",
       "0         3.0         1.0         0.0       5.0       5.0     1.0   \n",
       "2         3.0         3.0         1.0       3.0       2.0     3.0   \n",
       "4         4.0         2.0         1.0       3.0       3.0     3.0   \n",
       "5         3.0         2.0         1.0       5.0       5.0     3.0   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0          2.0       1.0  \n",
       "2          5.0       3.0  \n",
       "4          5.0       1.0  \n",
       "5          7.0       5.0  \n",
       "\n",
       "[4 rows x 56 columns]"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerstemp_noNa.drop(customers_cat_dummies.columns, axis=1, inplace=True)\n",
    "customerstemp_noNa.drop(customers_mixed_dummies.columns, axis=1, inplace=True)\n",
    "customerstemp_noNa.drop('GREEN_AVANTGARDE', axis=1, inplace=True)\n",
    "customerstemp_noNa.head(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StandardScaler(copy=True, with_mean=True, with_std=True)"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scalerCust = StandardScaler()\n",
    "scalerCust.fit(customerstemp_noNa)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "customers_noNa_scaled = pd.DataFrame(scaler.transform(customerstemp_noNa), columns=customerstemp_noNa.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customers_noNa_scaled.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  errors=errors)\n"
     ]
    }
   ],
   "source": [
    "customerstemp_noNa = customerstemp.dropna()\n",
    "customerstemp_noNa.drop(customers_noNa_scaled.columns, axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerstemp_noNa.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [],
   "source": [
    "customerstemp_noNa_final = pd.merge(customerstemp_noNa.reset_index(), customers_noNa_scaled.reset_index(), right_index=True, left_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerstemp_noNa_final.isnull().values.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [],
   "source": [
    "customerstemp_noNa_final.drop(['index_x', 'index_y'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(116132, 226)\n"
     ]
    },
    {
     "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>GREEN_AVANTGARDE</th>\n",
       "      <th>CJT_GESAMTTYP_1.0</th>\n",
       "      <th>CJT_GESAMTTYP_2.0</th>\n",
       "      <th>CJT_GESAMTTYP_3.0</th>\n",
       "      <th>CJT_GESAMTTYP_4.0</th>\n",
       "      <th>CJT_GESAMTTYP_5.0</th>\n",
       "      <th>CJT_GESAMTTYP_6.0</th>\n",
       "      <th>CJT_GESAMTTYP_nan</th>\n",
       "      <th>FINANZTYP_1</th>\n",
       "      <th>FINANZTYP_2</th>\n",
       "      <th>...</th>\n",
       "      <th>KBA13_ANZAHL_PKW</th>\n",
       "      <th>PLZ8_ANTG1</th>\n",
       "      <th>PLZ8_ANTG2</th>\n",
       "      <th>PLZ8_ANTG3</th>\n",
       "      <th>PLZ8_ANTG4</th>\n",
       "      <th>PLZ8_HHZ</th>\n",
       "      <th>PLZ8_GBZ</th>\n",
       "      <th>ARBEIT</th>\n",
       "      <th>ORTSGR_KLS9</th>\n",
       "      <th>RELAT_AB</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.773626</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-2.226515</td>\n",
       "      <td>-1.478616</td>\n",
       "      <td>-1.565428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.536230</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>1.38779</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-1.209678</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.295620</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>1.270764</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>-1.565428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.671367</td>\n",
       "      <td>-0.231061</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>0.37203</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>0.699119</td>\n",
       "      <td>1.396317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2.071381</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>-0.908009</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-1.043069</td>\n",
       "      <td>-0.824992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.391864</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>-0.607522</td>\n",
       "      <td>-0.084556</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1.487902</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>0.181378</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>0.263572</td>\n",
       "      <td>0.655880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0.035219</td>\n",
       "      <td>1.814461</td>\n",
       "      <td>-0.908009</td>\n",
       "      <td>-0.64373</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>-0.638548</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-1.215167</td>\n",
       "      <td>-0.171975</td>\n",
       "      <td>-1.565428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.782649</td>\n",
       "      <td>0.791700</td>\n",
       "      <td>-1.997396</td>\n",
       "      <td>-1.65949</td>\n",
       "      <td>-0.984345</td>\n",
       "      <td>0.400863</td>\n",
       "      <td>1.483855</td>\n",
       "      <td>-2.226515</td>\n",
       "      <td>-1.914163</td>\n",
       "      <td>-1.565428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.091101</td>\n",
       "      <td>-1.253822</td>\n",
       "      <td>1.270764</td>\n",
       "      <td>1.38779</td>\n",
       "      <td>0.381459</td>\n",
       "      <td>1.440275</td>\n",
       "      <td>-0.311834</td>\n",
       "      <td>-0.203820</td>\n",
       "      <td>1.134666</td>\n",
       "      <td>1.396317</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 226 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   GREEN_AVANTGARDE  CJT_GESAMTTYP_1.0  CJT_GESAMTTYP_2.0  CJT_GESAMTTYP_3.0  \\\n",
       "0                 1                  0                  0                  0   \n",
       "1                 1                  0                  1                  0   \n",
       "2                 0                  0                  0                  0   \n",
       "3                 1                  0                  0                  0   \n",
       "4                 1                  0                  1                  0   \n",
       "5                 1                  0                  1                  0   \n",
       "6                 1                  0                  0                  1   \n",
       "7                 1                  0                  0                  1   \n",
       "8                 0                  0                  0                  0   \n",
       "9                 0                  0                  1                  0   \n",
       "\n",
       "   CJT_GESAMTTYP_4.0  CJT_GESAMTTYP_5.0  CJT_GESAMTTYP_6.0  CJT_GESAMTTYP_nan  \\\n",
       "0                  0                  1                  0                  0   \n",
       "1                  0                  0                  0                  0   \n",
       "2                  0                  0                  1                  0   \n",
       "3                  1                  0                  0                  0   \n",
       "4                  0                  0                  0                  0   \n",
       "5                  0                  0                  0                  0   \n",
       "6                  0                  0                  0                  0   \n",
       "7                  0                  0                  0                  0   \n",
       "8                  0                  1                  0                  0   \n",
       "9                  0                  0                  0                  0   \n",
       "\n",
       "   FINANZTYP_1  FINANZTYP_2    ...     KBA13_ANZAHL_PKW  PLZ8_ANTG1  \\\n",
       "0            0            1    ...             1.773626    0.791700   \n",
       "1            0            1    ...            -0.536230   -0.231061   \n",
       "2            0            1    ...            -0.295620   -0.231061   \n",
       "3            0            0    ...             1.671367   -0.231061   \n",
       "4            0            1    ...             2.071381    0.791700   \n",
       "5            0            0    ...            -0.391864    0.791700   \n",
       "6            0            1    ...             1.487902    0.791700   \n",
       "7            0            1    ...             0.035219    1.814461   \n",
       "8            0            0    ...             1.782649    0.791700   \n",
       "9            0            0    ...            -0.091101   -1.253822   \n",
       "\n",
       "   PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_HHZ  PLZ8_GBZ    ARBEIT  \\\n",
       "0    0.181378    -0.64373   -0.984345  1.440275  1.483855 -2.226515   \n",
       "1    0.181378     1.38779    0.381459 -0.638548 -1.209678 -0.203820   \n",
       "2    1.270764     0.37203    0.381459 -0.638548 -0.311834 -0.203820   \n",
       "3    0.181378     0.37203    0.381459  1.440275  1.483855 -0.203820   \n",
       "4   -0.908009    -0.64373   -0.984345  1.440275  1.483855 -1.215167   \n",
       "5    0.181378    -0.64373    0.381459 -0.638548 -0.311834 -0.203820   \n",
       "6    0.181378    -0.64373   -0.984345  1.440275  1.483855 -0.203820   \n",
       "7   -0.908009    -0.64373   -0.984345 -0.638548 -0.311834 -1.215167   \n",
       "8   -1.997396    -1.65949   -0.984345  0.400863  1.483855 -2.226515   \n",
       "9    1.270764     1.38779    0.381459  1.440275 -0.311834 -0.203820   \n",
       "\n",
       "   ORTSGR_KLS9  RELAT_AB  \n",
       "0    -1.478616 -1.565428  \n",
       "1    -0.171975 -0.084556  \n",
       "2    -0.171975 -1.565428  \n",
       "3     0.699119  1.396317  \n",
       "4    -1.043069 -0.824992  \n",
       "5    -0.607522 -0.084556  \n",
       "6     0.263572  0.655880  \n",
       "7    -0.171975 -1.565428  \n",
       "8    -1.914163 -1.565428  \n",
       "9     1.134666  1.396317  \n",
       "\n",
       "[10 rows x 226 columns]"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(customerstemp_noNa_final.shape)\n",
    "customerstemp_noNa_final.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [],
   "source": [
    "pca, df_pca = do_pca(30, customerstemp_noNa_final)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(116132, 30)"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pca.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "Now we can apply kmeans on our fitted model from before!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,\n",
       "    n_clusters=19, n_init=10, n_jobs=1, precompute_distances='auto',\n",
       "    random_state=None, tol=0.0001, verbose=0)"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cluster9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultCust = cluster9.transform(df_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": [
    "customerClusters = cluster9.predict(df_pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [],
   "source": [
    "popClusters = cluster9.labels_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "This plot shows us the number of points in each cluster. It is not very useful as we need to scale them **so they represent a percentage which we will do below**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cluster    18\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "14    17464\n",
       "15    13584\n",
       "11    12668\n",
       "2      9874\n",
       "1      9647\n",
       "4      8844\n",
       "10     8623\n",
       "13     8363\n",
       "5      8040\n",
       "16     6109\n",
       "8      5274\n",
       "6      2956\n",
       "7      1510\n",
       "9      1175\n",
       "0       885\n",
       "3       771\n",
       "17      320\n",
       "18       25\n",
       "Name: Cluster, dtype: int64"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "testDf = pd.DataFrame(customerClusters, columns=['Cluster'])\n",
    "print(testDf.nunique())\n",
    "testDf[testDf['Cluster'] == 18]\n",
    "pd.value_counts(testDf['Cluster'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fb1a648e198>"
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19476 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb1a6492630>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, axes = plt.subplots(1, 2, figsize=(25, 10))\n",
    "sns.countplot(x=customerClusters, ax=axes[0])\n",
    "sns.countplot(popClusters, ax=axes[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_cust=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "116132\n",
      "116132\n"
     ]
    }
   ],
   "source": [
    "print(len(customerClusters))\n",
    "print(len(df_pca))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [],
   "source": [
    "def findClusterPerc(clusterSeries, df_pca, index=False):\n",
    "    testDf = pd.DataFrame(clusterSeries, columns=['Cluster'])\n",
    "    if index is False:\n",
    "        return pd.DataFrame((pd.DataFrame(pd.value_counts((testDf['Cluster'].values))).values/len(df_pca)), columns=['Cluster'])\n",
    "    else:\n",
    "        return pd.DataFrame((pd.DataFrame(pd.value_counts((testDf['Cluster'].values))).values/len(df_pca)), columns=['Cluster'], index=index)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "customerClustersPerc = findClusterPerc(customerClusters, df_pca, index=index_cust)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [],
   "source": [
    "popClustersPerc = findClusterPerc(popClusters, df_pca_orig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "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>Cluster</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.150381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.116970</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.109083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.085024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.083069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.076155</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.074252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.072013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.069232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.052604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.045414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.025454</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.013002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.010118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.007621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.006639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.002755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.000215</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Cluster\n",
       "0   0.150381\n",
       "1   0.116970\n",
       "2   0.109083\n",
       "3   0.085024\n",
       "4   0.083069\n",
       "5   0.076155\n",
       "6   0.074252\n",
       "7   0.072013\n",
       "8   0.069232\n",
       "9   0.052604\n",
       "10  0.045414\n",
       "11  0.025454\n",
       "13  0.013002\n",
       "14  0.010118\n",
       "15  0.007621\n",
       "16  0.006639\n",
       "17  0.002755\n",
       "18  0.000215"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "customerClustersPerc\n",
    "# popClustersPerc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7fb19eab32e8>"
      ]
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 17972 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fb27aca12b0>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, axes = plt.subplots(1, 2, figsize=(25, 10))\n",
    "sns.barplot(y=customerClustersPerc['Cluster'], x=customerClustersPerc.index, ax=axes[0])\n",
    "sns.barplot(y=popClustersPerc['Cluster'], x=popClustersPerc.index, ax=axes[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Answer\n",
    "\n",
    "We can see that in the left graph which represents the customer data, there are relatively more people towards the first clusters. On the right which represents our population data, it is more evenly spread out between the clusters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "632125\n",
      "891187\n"
     ]
    }
   ],
   "source": [
    "print(pd.DataFrame(df_pca_orig).index.max())\n",
    "print(anomaliesU_compare.index.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3.3: Compare Customer Data to Demographics Data\n",
    "\n",
    "At this point, you have clustered data based on demographics of the general population of Germany, and seen how the customer data for a mail-order sales company maps onto those demographic clusters. In this final substep, you will compare the two cluster distributions to see where the strongest customer base for the company is.\n",
    "\n",
    "Consider the proportion of persons in each cluster for the general population, and the proportions for the customers. If we think the company's customer base to be universal, then the cluster assignment proportions should be fairly similar between the two. If there are only particular segments of the population that are interested in the company's products, then we should see a mismatch from one to the other. If there is a higher proportion of persons in a cluster for the customer data compared to the general population (e.g. 5% of persons are assigned to a cluster for the general population, but 15% of the customer data is closest to that cluster's centroid) then that suggests the people in that cluster to be a target audience for the company. On the other hand, the proportion of the data in a cluster being larger in the general population than the customer data (e.g. only 2% of customers closest to a population centroid that captures 6% of the data) suggests that group of persons to be outside of the target demographics.\n",
    "\n",
    "Take a look at the following points in this step:\n",
    "\n",
    "- Compute the proportion of data points in each cluster for the general population and the customer data. Visualizations will be useful here: both for the individual dataset proportions, but also to visualize the ratios in cluster representation between groups. Seaborn's [`countplot()`](https://seaborn.pydata.org/generated/seaborn.countplot.html) or [`barplot()`](https://seaborn.pydata.org/generated/seaborn.barplot.html) function could be handy.\n",
    "  - Recall the analysis you performed in step 1.1.3 of the project, where you separated out certain data points from the dataset if they had more than a specified threshold of missing values. If you found that this group was qualitatively different from the main bulk of the data, you should treat this as an additional data cluster in this analysis. Make sure that you account for the number of data points in this subset, for both the general population and customer datasets, when making your computations!\n",
    "- Which cluster or clusters are overrepresented in the customer dataset compared to the general population? Select at least one such cluster and infer what kind of people might be represented by that cluster. Use the principal component interpretations from step 2.3 or look at additional components to help you make this inference. Alternatively, you can use the `.inverse_transform()` method of the PCA and StandardScaler objects to transform centroids back to the original data space and interpret the retrieved values directly.\n",
    "- Perform a similar investigation for the underrepresented clusters. Which cluster or clusters are underrepresented in the customer dataset compared to the general population, and what kinds of people are typified by these clusters?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compare the proportion of data in each cluster for the customer data to the\n",
    "# proportion of data in each cluster for the general population.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# What kinds of people are part of a cluster that is overrepresented in the\n",
    "# customer data compared to the general population?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# What kinds of people are part of a cluster that is underrepresented in the\n",
    "# customer data compared to the general population?\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Discussion 3.3: Compare Customer Data to Demographics Data\n",
    "\n",
    "(Double-click this cell and replace this text with your own text, reporting findings and conclusions from the clustering analysis. Can we describe segments of the population that are relatively popular with the mail-order company, or relatively unpopular with the company?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Congratulations on making it this far in the project! Before you finish, make sure to check through the entire notebook from top to bottom to make sure that your analysis follows a logical flow and all of your findings are documented in **Discussion** cells. Once you've checked over all of your work, you should export the notebook as an HTML document to submit for evaluation. You can do this from the menu, navigating to **File -> Download as -> HTML (.html)**. You will submit both that document and this notebook for your project submission."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
