{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true,
      "collapsed_sections": [
        "-exRqkmq5HgP"
      ],
      "authorship_tag": "ABX9TyPZyaxF3GFbQ2TY4HytxQlr",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Sahel-Eskandar/Project-Notebooks/blob/main/2_Preprocessing_Classification.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "SaveName = '2-Preprocessing-Classification'\n",
        "Version = 'Dec05'\n",
        "\n",
        "Input = 'Interview/Data/'\n",
        "Output = '/content/Interview/Model/'"
      ],
      "metadata": {
        "id": "ApyG7mXf_HTC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Import Libraries"
      ],
      "metadata": {
        "id": "WBmH97kS84Ux"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%load_ext autoreload\n",
        "%autoreload 2"
      ],
      "metadata": {
        "id": "7Cf3xGDv-GYt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install --upgrade pyod"
      ],
      "metadata": {
        "id": "90v9l4vdC0uY",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "52dec60e-a3e2-430c-d334-0fea3732e3b3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
            "Collecting pyod\n",
            "  Downloading pyod-1.0.6.tar.gz (141 kB)\n",
            "\u001b[K     |████████████████████████████████| 141 kB 3.9 MB/s \n",
            "\u001b[?25hRequirement already satisfied: joblib in /usr/local/lib/python3.8/dist-packages (from pyod) (1.2.0)\n",
            "Requirement already satisfied: matplotlib in /usr/local/lib/python3.8/dist-packages (from pyod) (3.2.2)\n",
            "Requirement already satisfied: numpy>=1.19 in /usr/local/lib/python3.8/dist-packages (from pyod) (1.21.6)\n",
            "Requirement already satisfied: numba>=0.51 in /usr/local/lib/python3.8/dist-packages (from pyod) (0.56.4)\n",
            "Requirement already satisfied: scipy>=1.5.1 in /usr/local/lib/python3.8/dist-packages (from pyod) (1.7.3)\n",
            "Requirement already satisfied: scikit_learn>=0.20.0 in /usr/local/lib/python3.8/dist-packages (from pyod) (1.0.2)\n",
            "Requirement already satisfied: six in /usr/local/lib/python3.8/dist-packages (from pyod) (1.15.0)\n",
            "Requirement already satisfied: statsmodels in /usr/local/lib/python3.8/dist-packages (from pyod) (0.12.2)\n",
            "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.8/dist-packages (from numba>=0.51->pyod) (4.13.0)\n",
            "Requirement already satisfied: llvmlite<0.40,>=0.39.0dev0 in /usr/local/lib/python3.8/dist-packages (from numba>=0.51->pyod) (0.39.1)\n",
            "Requirement already satisfied: setuptools in /usr/local/lib/python3.8/dist-packages (from numba>=0.51->pyod) (57.4.0)\n",
            "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from scikit_learn>=0.20.0->pyod) (3.1.0)\n",
            "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.8/dist-packages (from importlib-metadata->numba>=0.51->pyod) (3.10.0)\n",
            "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->pyod) (2.8.2)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib->pyod) (0.11.0)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->pyod) (3.0.9)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib->pyod) (1.4.4)\n",
            "Requirement already satisfied: pandas>=0.21 in /usr/local/lib/python3.8/dist-packages (from statsmodels->pyod) (1.3.5)\n",
            "Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.8/dist-packages (from statsmodels->pyod) (0.5.3)\n",
            "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.8/dist-packages (from pandas>=0.21->statsmodels->pyod) (2022.6)\n",
            "Building wheels for collected packages: pyod\n",
            "  Building wheel for pyod (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
            "  Created wheel for pyod: filename=pyod-1.0.6-py3-none-any.whl size=175098 sha256=df0811e4b09fba873e87e0404721f994d770751effff4292173460e7f92eecc1\n",
            "  Stored in directory: /root/.cache/pip/wheels/98/93/e6/6d40410d9635ecde42d06041a1ba7f2ee7396e036fcf702e73\n",
            "Successfully built pyod\n",
            "Installing collected packages: pyod\n",
            "Successfully installed pyod-1.0.6\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os, sys\n",
        "import os.path\n",
        "\n",
        "import math\n",
        "import time\n",
        "from datetime import datetime as dt\n",
        "from collections import OrderedDict\n",
        "import matplotlib.pyplot as plt\n",
        "from matplotlib.colors import cnames\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import seaborn as sns\n",
        "import random\n",
        "\n",
        "import warnings\n",
        "warnings.simplefilter('ignore')\n",
        "sns.set_style('whitegrid') # style\n",
        "# render plots inline\n",
        "%matplotlib inline"
      ],
      "metadata": {
        "id": "C5wTKM8f_-Bk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def ensure_dir(file_path):\n",
        "    directory = os.path.dirname(file_path)\n",
        "    if not os.path.exists(directory):\n",
        "        os.makedirs(directory)"
      ],
      "metadata": {
        "id": "17KFg-AeCayN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Data Extraction"
      ],
      "metadata": {
        "id": "UXjhL-lZ4Rna"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "path = Input + 'term_deposit_data.csv'\n",
        "df = pd.read_csv(path, sep= ';')"
      ],
      "metadata": {
        "id": "F26OpePm88xl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "1. age (numeric)\n",
        "2. job : type of job (categorical: 'admin.','blue-collar', 'entrepreneur','housemaid','management', 'retired','self-employed','services','student','technician','unemployed','unknown')\n",
        "3. marital : marital status (categorical: 'divorced','married','single','unknown'; note: 'divorced' means divorced or widowed)\n",
        "4. education (categorical: 'basic.4y','basic.6y','basic.9y','high.school','illiterate','professional.course', 'university.degree', 'unknown')\n",
        "5. default: has credit in default? (categorical: 'no','yes','unknown')\n",
        "6. housing: has housing loan? (categorical: 'no','yes','unknown')\n",
        "7. loan: has personal loan? (categorical: 'no','yes','unknown') # related with the last contact of the current campaign:\n",
        "8. contact: contact communication type (categorical: 'cellular','telephone')\n",
        "9. month: last contact month of year (categorical: 'jan', 'feb', 'mar', ..., 'nov', 'dec')\n",
        "10. day_of_week: last contact day of the week (categorical: 'mon','tue','wed','thu','fri')\n",
        "11. duration: last contact duration, in seconds (numeric). Important note: this attribute highly affects the output target (e.g., if duration=0 then y='no')\n",
        "12. campaign: number of contacts performed during this campaign and for this client (numeric, includes last contact)\n",
        "13. pdays: number of days that passed by after the client was last contacted from a previous campaign (numeric; 999 means client was not previously contacted)\n",
        "14. previous: number of contacts performed before this campaign and for this client (numeric)\n",
        "15. poutcome: outcome of the previous marketing campaign (categorical: 'failure','nonexistent','success') # social and economic context attributes\n",
        "16. emp.var.rate: employment variation rate - quarterly indicator (numeric)\n",
        "17. cons.price.idx: consumer price index - monthly indicator (numeric)\n",
        "18. cons.conf.idx: consumer confidence index - monthly indicator (numeric)\n",
        "19. euribor3m: euribor 3 month rate - daily indicator (numeric)\n",
        "20. nr.employed: number of employees - quarterly indicator (numeric) Output variable (desired target):\n",
        "21. y - has the client subscribed a term deposit? (binary: 'yes','no') "
      ],
      "metadata": {
        "id": "FfX6GrgDiaxD"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df.info()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yaWEG-Ro9Qm5",
        "outputId": "62f7085b-f019-4f87-93f2-a5833fbf5361"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<class 'pandas.core.frame.DataFrame'>\n",
            "RangeIndex: 41188 entries, 0 to 41187\n",
            "Data columns (total 21 columns):\n",
            " #   Column          Non-Null Count  Dtype  \n",
            "---  ------          --------------  -----  \n",
            " 0   age             41188 non-null  int64  \n",
            " 1   job             41188 non-null  object \n",
            " 2   marital         41188 non-null  object \n",
            " 3   education       41188 non-null  object \n",
            " 4   default         41188 non-null  object \n",
            " 5   housing         41188 non-null  object \n",
            " 6   loan            41188 non-null  object \n",
            " 7   contact         41188 non-null  object \n",
            " 8   month           41188 non-null  object \n",
            " 9   day_of_week     41188 non-null  object \n",
            " 10  duration        41188 non-null  int64  \n",
            " 11  campaign        41188 non-null  int64  \n",
            " 12  pdays           41188 non-null  int64  \n",
            " 13  previous        41188 non-null  int64  \n",
            " 14  poutcome        41188 non-null  object \n",
            " 15  emp.var.rate    41188 non-null  float64\n",
            " 16  cons.price.idx  41188 non-null  float64\n",
            " 17  cons.conf.idx   41188 non-null  float64\n",
            " 18  euribor3m       41188 non-null  float64\n",
            " 19  nr.employed     41188 non-null  float64\n",
            " 20  y               41188 non-null  object \n",
            "dtypes: float64(5), int64(5), object(11)\n",
            "memory usage: 6.6+ MB\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df.head(5)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 300
        },
        "id": "5nKJ5P4S-9zk",
        "outputId": "744375a2-f83f-4da2-de1d-9fbfb50505e9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   age        job  marital    education  default housing loan    contact  \\\n",
              "0   56  housemaid  married     basic.4y       no      no   no  telephone   \n",
              "1   57   services  married  high.school  unknown      no   no  telephone   \n",
              "2   37   services  married  high.school       no     yes   no  telephone   \n",
              "3   40     admin.  married     basic.6y       no      no   no  telephone   \n",
              "4   56   services  married  high.school       no      no  yes  telephone   \n",
              "\n",
              "  month day_of_week  ...  campaign  pdays  previous     poutcome emp.var.rate  \\\n",
              "0   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "1   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "2   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "3   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "4   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "\n",
              "   cons.price.idx  cons.conf.idx  euribor3m  nr.employed   y  \n",
              "0          93.994          -36.4      4.857       5191.0  no  \n",
              "1          93.994          -36.4      4.857       5191.0  no  \n",
              "2          93.994          -36.4      4.857       5191.0  no  \n",
              "3          93.994          -36.4      4.857       5191.0  no  \n",
              "4          93.994          -36.4      4.857       5191.0  no  \n",
              "\n",
              "[5 rows x 21 columns]"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-943eebee-1030-4f01-ad1c-1bd2d3bd44fd\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>job</th>\n",
              "      <th>marital</th>\n",
              "      <th>education</th>\n",
              "      <th>default</th>\n",
              "      <th>housing</th>\n",
              "      <th>loan</th>\n",
              "      <th>contact</th>\n",
              "      <th>month</th>\n",
              "      <th>day_of_week</th>\n",
              "      <th>...</th>\n",
              "      <th>campaign</th>\n",
              "      <th>pdays</th>\n",
              "      <th>previous</th>\n",
              "      <th>poutcome</th>\n",
              "      <th>emp.var.rate</th>\n",
              "      <th>cons.price.idx</th>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <th>euribor3m</th>\n",
              "      <th>nr.employed</th>\n",
              "      <th>y</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>56</td>\n",
              "      <td>housemaid</td>\n",
              "      <td>married</td>\n",
              "      <td>basic.4y</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>no</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>57</td>\n",
              "      <td>services</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>unknown</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>no</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>37</td>\n",
              "      <td>services</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>no</td>\n",
              "      <td>yes</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>no</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>40</td>\n",
              "      <td>admin.</td>\n",
              "      <td>married</td>\n",
              "      <td>basic.6y</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>no</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>56</td>\n",
              "      <td>services</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>yes</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>no</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-943eebee-1030-4f01-ad1c-1bd2d3bd44fd')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-943eebee-1030-4f01-ad1c-1bd2d3bd44fd button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-943eebee-1030-4f01-ad1c-1bd2d3bd44fd');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df.describe()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 364
        },
        "id": "Lw6uwa2O_Tzu",
        "outputId": "5f99e91e-68df-4a4f-aafa-e753d1c6be12"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "               age      duration      campaign         pdays      previous  \\\n",
              "count  41188.00000  41188.000000  41188.000000  41188.000000  41188.000000   \n",
              "mean      40.02406    258.285010      2.567593    962.475454      0.172963   \n",
              "std       10.42125    259.279249      2.770014    186.910907      0.494901   \n",
              "min       17.00000      0.000000      1.000000      0.000000      0.000000   \n",
              "25%       32.00000    102.000000      1.000000    999.000000      0.000000   \n",
              "50%       38.00000    180.000000      2.000000    999.000000      0.000000   \n",
              "75%       47.00000    319.000000      3.000000    999.000000      0.000000   \n",
              "max       98.00000   4918.000000     56.000000    999.000000      7.000000   \n",
              "\n",
              "       emp.var.rate  cons.price.idx  cons.conf.idx     euribor3m   nr.employed  \n",
              "count  41188.000000    41188.000000   41188.000000  41188.000000  41188.000000  \n",
              "mean       0.081886       93.575664     -40.502600      3.621291   5167.035911  \n",
              "std        1.570960        0.578840       4.628198      1.734447     72.251528  \n",
              "min       -3.400000       92.201000     -50.800000      0.634000   4963.600000  \n",
              "25%       -1.800000       93.075000     -42.700000      1.344000   5099.100000  \n",
              "50%        1.100000       93.749000     -41.800000      4.857000   5191.000000  \n",
              "75%        1.400000       93.994000     -36.400000      4.961000   5228.100000  \n",
              "max        1.400000       94.767000     -26.900000      5.045000   5228.100000  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-d42307b6-e4b1-4bab-a3ce-37f8382274e6\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>duration</th>\n",
              "      <th>campaign</th>\n",
              "      <th>pdays</th>\n",
              "      <th>previous</th>\n",
              "      <th>emp.var.rate</th>\n",
              "      <th>cons.price.idx</th>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <th>euribor3m</th>\n",
              "      <th>nr.employed</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>count</th>\n",
              "      <td>41188.00000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "      <td>41188.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mean</th>\n",
              "      <td>40.02406</td>\n",
              "      <td>258.285010</td>\n",
              "      <td>2.567593</td>\n",
              "      <td>962.475454</td>\n",
              "      <td>0.172963</td>\n",
              "      <td>0.081886</td>\n",
              "      <td>93.575664</td>\n",
              "      <td>-40.502600</td>\n",
              "      <td>3.621291</td>\n",
              "      <td>5167.035911</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>std</th>\n",
              "      <td>10.42125</td>\n",
              "      <td>259.279249</td>\n",
              "      <td>2.770014</td>\n",
              "      <td>186.910907</td>\n",
              "      <td>0.494901</td>\n",
              "      <td>1.570960</td>\n",
              "      <td>0.578840</td>\n",
              "      <td>4.628198</td>\n",
              "      <td>1.734447</td>\n",
              "      <td>72.251528</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>min</th>\n",
              "      <td>17.00000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>-3.400000</td>\n",
              "      <td>92.201000</td>\n",
              "      <td>-50.800000</td>\n",
              "      <td>0.634000</td>\n",
              "      <td>4963.600000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>25%</th>\n",
              "      <td>32.00000</td>\n",
              "      <td>102.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>999.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>-1.800000</td>\n",
              "      <td>93.075000</td>\n",
              "      <td>-42.700000</td>\n",
              "      <td>1.344000</td>\n",
              "      <td>5099.100000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>50%</th>\n",
              "      <td>38.00000</td>\n",
              "      <td>180.000000</td>\n",
              "      <td>2.000000</td>\n",
              "      <td>999.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>1.100000</td>\n",
              "      <td>93.749000</td>\n",
              "      <td>-41.800000</td>\n",
              "      <td>4.857000</td>\n",
              "      <td>5191.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>75%</th>\n",
              "      <td>47.00000</td>\n",
              "      <td>319.000000</td>\n",
              "      <td>3.000000</td>\n",
              "      <td>999.000000</td>\n",
              "      <td>0.000000</td>\n",
              "      <td>1.400000</td>\n",
              "      <td>93.994000</td>\n",
              "      <td>-36.400000</td>\n",
              "      <td>4.961000</td>\n",
              "      <td>5228.100000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>max</th>\n",
              "      <td>98.00000</td>\n",
              "      <td>4918.000000</td>\n",
              "      <td>56.000000</td>\n",
              "      <td>999.000000</td>\n",
              "      <td>7.000000</td>\n",
              "      <td>1.400000</td>\n",
              "      <td>94.767000</td>\n",
              "      <td>-26.900000</td>\n",
              "      <td>5.045000</td>\n",
              "      <td>5228.100000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d42307b6-e4b1-4bab-a3ce-37f8382274e6')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-d42307b6-e4b1-4bab-a3ce-37f8382274e6 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-d42307b6-e4b1-4bab-a3ce-37f8382274e6');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 10
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Data Visualization"
      ],
      "metadata": {
        "id": "1NEybxe7iiMH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df.hist(bins= 30, figsize=(20, 10))\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 456
        },
        "id": "PhJ1lLia_ZAK",
        "outputId": "25e068dc-5717-4af7-f19a-805a7a4de864"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 1440x720 with 12 Axes>"
            ],
            "image/png": "[encoded data, 42292 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Pair-plot\n",
        "\n",
        "Pair plots are a combination of scatter plots and histograms.\n",
        "\n",
        "They are done for each pair of features (e.g. age vs. job)\n",
        "\n",
        "https://seaborn.pydata.org/generated/seaborn.pairplot.html"
      ],
      "metadata": {
        "id": "NSiJ0Cus7asn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#sns.set_theme(style=\"darkgrid\")\n",
        "#sns.set(rc={'figure.figsize':(30,30)})\n",
        "#sns.pairplot(df,hue=\"y\")\n",
        "#plt.show()"
      ],
      "metadata": {
        "id": "wguv7iLy6gWf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#df.drop('pdays',axis=1)"
      ],
      "metadata": {
        "id": "KqdHG-aTIDrD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Correlation\n",
        "\n",
        "Correlations provide a metric to indicate whether two variables are strongly dependent.\n",
        "\n",
        "https://www.statisticssolutions.com/correlation-pearson-kendall-spearman/"
      ],
      "metadata": {
        "id": "16YWe-oRRkPc"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df.corr(method='pearson')\n",
        "# a graphical way to viewing the correlation matrix\n",
        "fig, ax = plt.subplots(figsize=(10, 7))\n",
        "sns.heatmap(df.corr(method='pearson'), annot=True, fmt='.2g',\n",
        "            annot_kws=None, linewidths=1, linecolor='black')\n",
        "plt.xticks(fontsize=12)\n",
        "plt.yticks(fontsize=10)\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 506
        },
        "id": "5KiNk4HZRjUm",
        "outputId": "3fedce90-36f9-483a-806b-4afd8b597ad1"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 720x504 with 2 Axes>"
            ],
            "image/png": "[encoded data, 83524 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Drop one of the features of those highly correlated. Normally with high correlation (>0.8) and almost zero correlation there are issues.\n",
        "\n",
        "features 'emp.var.rate' and 'euribor3m' were selected based on the correlation matrix and kbest feature analysis"
      ],
      "metadata": {
        "id": "Ha4brnMFio6Q"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "remove_cols = ['emp.var.rate','euribor3m']\n",
        "df2 = df.drop(remove_cols, axis= 1)\n",
        "#euribor3m nr.employed emp.var.rate"
      ],
      "metadata": {
        "id": "PkCAg6HVLiJG"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df2.corr(method='pearson')\n",
        "# a graphical way to viewing the correlation matrix\n",
        "fig, ax = plt.subplots(figsize=(10, 7))\n",
        "sns.heatmap(df2.corr(method='pearson'), annot=True, fmt='.2g',\n",
        "            annot_kws=None, linewidths=1, linecolor='black')\n",
        "plt.xticks(fontsize=12)\n",
        "plt.yticks(fontsize=10)\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 506
        },
        "id": "CCjUrcNqMGAJ",
        "outputId": "9c5441ff-1d92-4b46-9730-9d32c0108044"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 720x504 with 2 Axes>"
            ],
            "image/png": "[encoded data, 60688 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "marital_percent = pd.crosstab(df['marital'], df['y']).apply(lambda x: x/x.sum() * 100)\n",
        "marital_percent.sort_values(by='yes', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "JyMSEolStB7z",
        "outputId": "56a1145c-1d4d-430c-b121-c8583992e3ba"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y                no        yes\n",
              "marital                       \n",
              "married   61.278319  54.568966\n",
              "single    27.219000  34.913793\n",
              "divorced  11.316625  10.258621\n",
              "unknown    0.186057   0.258621"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-ea7f6341-be5b-4230-a3ab-37cfd58cfb6e\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "      <th>no</th>\n",
              "      <th>yes</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>marital</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>married</th>\n",
              "      <td>61.278319</td>\n",
              "      <td>54.568966</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>single</th>\n",
              "      <td>27.219000</td>\n",
              "      <td>34.913793</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>divorced</th>\n",
              "      <td>11.316625</td>\n",
              "      <td>10.258621</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>unknown</th>\n",
              "      <td>0.186057</td>\n",
              "      <td>0.258621</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-ea7f6341-be5b-4230-a3ab-37cfd58cfb6e')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-ea7f6341-be5b-4230-a3ab-37cfd58cfb6e button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-ea7f6341-be5b-4230-a3ab-37cfd58cfb6e');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "contact_percent = pd.crosstab(df['contact'], df['y']).apply(lambda x: x/x.sum() * 100)\n",
        "contact_percent.sort_values(by='yes', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 143
        },
        "outputId": "5703ecef-a753-4855-be69-ec5511ba102c",
        "id": "3bhWZ-zfSIMV"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y                 no        yes\n",
              "contact                        \n",
              "cellular   60.991026  83.038793\n",
              "telephone  39.008974  16.961207"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-e69b8ce6-75f6-455a-ad69-5e97741e963d\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "      <th>no</th>\n",
              "      <th>yes</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>contact</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>cellular</th>\n",
              "      <td>60.991026</td>\n",
              "      <td>83.038793</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>telephone</th>\n",
              "      <td>39.008974</td>\n",
              "      <td>16.961207</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-e69b8ce6-75f6-455a-ad69-5e97741e963d')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-e69b8ce6-75f6-455a-ad69-5e97741e963d button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-e69b8ce6-75f6-455a-ad69-5e97741e963d');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df.contact.value_counts()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QrsFd5ceRfCN",
        "outputId": "f862f4a9-1d6f-4127-90be-86b4bdd1b49e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "cellular     26144\n",
              "telephone    15044\n",
              "Name: contact, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "month_percent = pd.crosstab(df['month'], df['y']).apply(lambda x: x/x.sum() * 100)\n",
        "month_percent.sort_values(by='yes', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 394
        },
        "id": "ge3SdmCNB6HH",
        "outputId": "cdac0f99-f881-4557-b50f-12fe814c9b3c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y             no        yes\n",
              "month                      \n",
              "may    35.249535  19.094828\n",
              "aug    15.111634  14.116379\n",
              "jul    17.853234  13.987069\n",
              "jun    13.021232  12.047414\n",
              "apr     5.726716  11.616379\n",
              "nov    10.082631   8.965517\n",
              "oct     1.102660   6.788793\n",
              "mar     0.738755   5.948276\n",
              "sep     0.859144   5.517241\n",
              "dec     0.254460   1.918103"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-671cf085-62eb-4829-9d1f-075b690f0bea\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "      <th>no</th>\n",
              "      <th>yes</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>month</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>may</th>\n",
              "      <td>35.249535</td>\n",
              "      <td>19.094828</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>aug</th>\n",
              "      <td>15.111634</td>\n",
              "      <td>14.116379</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jul</th>\n",
              "      <td>17.853234</td>\n",
              "      <td>13.987069</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>jun</th>\n",
              "      <td>13.021232</td>\n",
              "      <td>12.047414</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>apr</th>\n",
              "      <td>5.726716</td>\n",
              "      <td>11.616379</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>nov</th>\n",
              "      <td>10.082631</td>\n",
              "      <td>8.965517</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>oct</th>\n",
              "      <td>1.102660</td>\n",
              "      <td>6.788793</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>mar</th>\n",
              "      <td>0.738755</td>\n",
              "      <td>5.948276</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>sep</th>\n",
              "      <td>0.859144</td>\n",
              "      <td>5.517241</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>dec</th>\n",
              "      <td>0.254460</td>\n",
              "      <td>1.918103</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-671cf085-62eb-4829-9d1f-075b690f0bea')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-671cf085-62eb-4829-9d1f-075b690f0bea button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-671cf085-62eb-4829-9d1f-075b690f0bea');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "job_percent = pd.crosstab(df['job'], df['y']).apply(lambda x: x/x.sum() * 100)\n",
        "job_percent.sort_values(by='yes', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 457
        },
        "id": "KuzQNGXrtBxR",
        "outputId": "8d244a54-f8f0-4b83-8120-9ddb024f28b9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y                     no        yes\n",
              "job                                \n",
              "admin.         24.816679  29.137931\n",
              "technician     16.452337  15.732759\n",
              "blue-collar    23.574477  13.750000\n",
              "retired         3.518660   9.353448\n",
              "management      7.102988   7.068966\n",
              "services        9.975922   6.961207\n",
              "student         1.641677   5.926724\n",
              "self-employed   3.480355   3.211207\n",
              "unemployed      2.380431   3.103448\n",
              "entrepreneur    3.644522   2.672414\n",
              "housemaid       2.610266   2.284483\n",
              "unknown         0.801685   0.797414"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-fc081caa-b800-47d9-9b27-2e9786f8d437\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "      <th>no</th>\n",
              "      <th>yes</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>job</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>admin.</th>\n",
              "      <td>24.816679</td>\n",
              "      <td>29.137931</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>technician</th>\n",
              "      <td>16.452337</td>\n",
              "      <td>15.732759</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>blue-collar</th>\n",
              "      <td>23.574477</td>\n",
              "      <td>13.750000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>retired</th>\n",
              "      <td>3.518660</td>\n",
              "      <td>9.353448</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>management</th>\n",
              "      <td>7.102988</td>\n",
              "      <td>7.068966</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>services</th>\n",
              "      <td>9.975922</td>\n",
              "      <td>6.961207</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>student</th>\n",
              "      <td>1.641677</td>\n",
              "      <td>5.926724</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>self-employed</th>\n",
              "      <td>3.480355</td>\n",
              "      <td>3.211207</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>unemployed</th>\n",
              "      <td>2.380431</td>\n",
              "      <td>3.103448</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>entrepreneur</th>\n",
              "      <td>3.644522</td>\n",
              "      <td>2.672414</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>housemaid</th>\n",
              "      <td>2.610266</td>\n",
              "      <td>2.284483</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>unknown</th>\n",
              "      <td>0.801685</td>\n",
              "      <td>0.797414</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-fc081caa-b800-47d9-9b27-2e9786f8d437')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-fc081caa-b800-47d9-9b27-2e9786f8d437 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-fc081caa-b800-47d9-9b27-2e9786f8d437');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df['job'].replace({'entrepreneur': 'self-employed', 'technician': 'blue-collar',\n",
        "                   'admin.': 'white-collar', 'management': 'white-collar',\n",
        "                  'services': 'pink-collar', 'housemaid': 'pink-collar'}, inplace=True)"
      ],
      "metadata": {
        "id": "939a1XI6KgDe"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df['job'].value_counts()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "DLPTzq7vKxes",
        "outputId": "8bd929bf-82e2-4554-a36b-0523235c51d2"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "blue-collar      15997\n",
              "white-collar     13346\n",
              "pink-collar       5029\n",
              "self-employed     2877\n",
              "retired           1720\n",
              "unemployed        1014\n",
              "student            875\n",
              "unknown            330\n",
              "Name: job, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "education_percent = pd.crosstab(df['education'], df['y']).apply(lambda x: x/x.sum() * 100)\n",
        "education_percent.sort_values(by='yes', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 332
        },
        "id": "bcMAJksJtCCe",
        "outputId": "03d1687f-c9ef-4da5-c69b-27b529990586"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y                           no        yes\n",
              "education                                \n",
              "university.degree    28.723870  35.991379\n",
              "high.school          23.213309  22.219828\n",
              "professional.course  12.717522  12.823276\n",
              "basic.9y             15.245704  10.193966\n",
              "basic.4y             10.255007   9.224138\n",
              "unknown               4.049469   5.409483\n",
              "basic.6y              5.756813   4.051724\n",
              "illiterate            0.038306   0.086207"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-d1cf13a9-65d3-4717-8663-b7eddcf18567\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "      <th>no</th>\n",
              "      <th>yes</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>education</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>university.degree</th>\n",
              "      <td>28.723870</td>\n",
              "      <td>35.991379</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>high.school</th>\n",
              "      <td>23.213309</td>\n",
              "      <td>22.219828</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>professional.course</th>\n",
              "      <td>12.717522</td>\n",
              "      <td>12.823276</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>basic.9y</th>\n",
              "      <td>15.245704</td>\n",
              "      <td>10.193966</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>basic.4y</th>\n",
              "      <td>10.255007</td>\n",
              "      <td>9.224138</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>unknown</th>\n",
              "      <td>4.049469</td>\n",
              "      <td>5.409483</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>basic.6y</th>\n",
              "      <td>5.756813</td>\n",
              "      <td>4.051724</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>illiterate</th>\n",
              "      <td>0.038306</td>\n",
              "      <td>0.086207</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d1cf13a9-65d3-4717-8663-b7eddcf18567')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-d1cf13a9-65d3-4717-8663-b7eddcf18567 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-d1cf13a9-65d3-4717-8663-b7eddcf18567');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df['education'].replace({'basic.9y': 'basic','basic.4y': 'basic','basic.6y':'basic'},inplace=True)"
      ],
      "metadata": {
        "id": "YxvBYwPXKrJZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df['education'].value_counts()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-4x0uXLwKwWz",
        "outputId": "92ff02eb-bd5b-4b6c-be3e-3126326fe1c3"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "basic                  12513\n",
              "university.degree      12168\n",
              "high.school             9515\n",
              "professional.course     5243\n",
              "unknown                 1731\n",
              "illiterate                18\n",
              "Name: education, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df.y.value_counts()"
      ],
      "metadata": {
        "id": "qGihvUFxDty2",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "7eadaa79-9a38-4a77-eb69-69765f964a93"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "no     36548\n",
              "yes     4640\n",
              "Name: y, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "X = df.y.value_counts()\n",
        "f,ax=plt.subplots(1,1, figsize=(6,4))\n",
        "X.plot.pie(explode=[0,0.1],autopct='%1.1f%%')\n",
        "plt.show()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 248
        },
        "id": "ZIGVQKL-0HVj",
        "outputId": "37d01e63-4df1-44d7-dec5-086a2a03b128"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 13772 characters]\n"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Shows impalance data."
      ],
      "metadata": {
        "id": "VRe4vvEv3qWd"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "cd '/content/Interview/'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "M1MtYwp3zdjF",
        "outputId": "7f50937d-4577-4775-bba1-c082a2a2c2fc"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "/content/Interview\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!git add '/content/Interview/Data'\n",
        "!git commit -m 'Aviva'  # commit in Colab\n",
        "!git push origin main"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yPj72VO-6WDq",
        "outputId": "d572ce25-dde5-4ece-a20c-af24fda8da9e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "On branch main\n",
            "Your branch is up to date with 'origin/main'.\n",
            "\n",
            "nothing to commit, working tree clean\n",
            "Everything up-to-date\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "#Data Prepration"
      ],
      "metadata": {
        "id": "SPUyd7cQ3zXv"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import warnings\n",
        "warnings.filterwarnings('ignore')\n",
        "\n",
        "from sklearn.preprocessing import OneHotEncoder,LabelEncoder, StandardScaler, MinMaxScaler\n",
        "from sklearn.pipeline import Pipeline\n",
        "from sklearn.naive_bayes import GaussianNB\n",
        "from sklearn.ensemble import RandomForestClassifier, IsolationForest\n",
        "from sklearn.feature_selection import SelectKBest, f_classif\n",
        "from sklearn.model_selection import train_test_split, GridSearchCV,cross_val_score\n",
        "from sklearn.metrics import classification_report, confusion_matrix\n",
        "from sklearn.decomposition import PCA\n",
        "from keras.utils.np_utils import to_categorical\n",
        "from pyod.models.knn import KNN\n",
        "from imblearn.over_sampling import RandomOverSampler, SMOTE\n",
        "from imblearn.under_sampling import RandomUnderSampler\n",
        "import xgboost as xgb\n",
        "from statsmodels.graphics.tsaplots import plot_acf\n",
        "from statsmodels.tsa.statespace.sarimax import SARIMAX\n",
        "from statsmodels.tsa.vector_ar.var_model import VAR"
      ],
      "metadata": {
        "id": "wxuEG9MM-MhI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc = df.copy()"
      ],
      "metadata": {
        "id": "eUziE_mYmcm-"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "numerical_cols = df_enc.select_dtypes(include=['float64', 'int64']).columns"
      ],
      "metadata": {
        "id": "oN1BbAJqBKNy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc['y'] = np.where(df_enc.y == 'yes', 1, 0)"
      ],
      "metadata": {
        "id": "kRAAJy6_7wst"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "corr = df_enc.corr()['y'].abs().sort_values(ascending=False)\n",
        "cols = corr[corr < 1].index.tolist()\n",
        "sns.set_style('darkgrid')\n",
        "fig , ax = plt.subplots(5,2,figsize = (12,20))\n",
        "ax = ax.ravel()\n",
        "for i,col in enumerate(cols):\n",
        "    sns.boxplot(df_enc[col], ax = ax[i])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "jJf8Mw4-86pH",
        "outputId": "efa576c3-2c76-48c5-ae71-46b47b48a8eb"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 864x1440 with 10 Axes>"
            ],
            "image/png": "[encoded data, 44440 characters]\n"
          },
          "metadata": {}
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 386
        },
        "id": "LeFmLeZkkPWa",
        "outputId": "553e1ceb-e9fb-4d51-c034-f479e87c5986"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   age           job  marital    education  default housing loan    contact  \\\n",
              "0   56   pink-collar  married        basic       no      no   no  telephone   \n",
              "1   57   pink-collar  married  high.school  unknown      no   no  telephone   \n",
              "2   37   pink-collar  married  high.school       no     yes   no  telephone   \n",
              "3   40  white-collar  married        basic       no      no   no  telephone   \n",
              "4   56   pink-collar  married  high.school       no      no  yes  telephone   \n",
              "\n",
              "  month day_of_week  ...  campaign  pdays  previous     poutcome emp.var.rate  \\\n",
              "0   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "1   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "2   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "3   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "4   may         mon  ...         1    999         0  nonexistent          1.1   \n",
              "\n",
              "   cons.price.idx  cons.conf.idx  euribor3m  nr.employed  y  \n",
              "0          93.994          -36.4      4.857       5191.0  0  \n",
              "1          93.994          -36.4      4.857       5191.0  0  \n",
              "2          93.994          -36.4      4.857       5191.0  0  \n",
              "3          93.994          -36.4      4.857       5191.0  0  \n",
              "4          93.994          -36.4      4.857       5191.0  0  \n",
              "\n",
              "[5 rows x 21 columns]"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-d68191d3-07ce-4158-b1c6-3fe9e4207f34\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>job</th>\n",
              "      <th>marital</th>\n",
              "      <th>education</th>\n",
              "      <th>default</th>\n",
              "      <th>housing</th>\n",
              "      <th>loan</th>\n",
              "      <th>contact</th>\n",
              "      <th>month</th>\n",
              "      <th>day_of_week</th>\n",
              "      <th>...</th>\n",
              "      <th>campaign</th>\n",
              "      <th>pdays</th>\n",
              "      <th>previous</th>\n",
              "      <th>poutcome</th>\n",
              "      <th>emp.var.rate</th>\n",
              "      <th>cons.price.idx</th>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <th>euribor3m</th>\n",
              "      <th>nr.employed</th>\n",
              "      <th>y</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>56</td>\n",
              "      <td>pink-collar</td>\n",
              "      <td>married</td>\n",
              "      <td>basic</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>57</td>\n",
              "      <td>pink-collar</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>unknown</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>37</td>\n",
              "      <td>pink-collar</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>no</td>\n",
              "      <td>yes</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>40</td>\n",
              "      <td>white-collar</td>\n",
              "      <td>married</td>\n",
              "      <td>basic</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>56</td>\n",
              "      <td>pink-collar</td>\n",
              "      <td>married</td>\n",
              "      <td>high.school</td>\n",
              "      <td>no</td>\n",
              "      <td>no</td>\n",
              "      <td>yes</td>\n",
              "      <td>telephone</td>\n",
              "      <td>may</td>\n",
              "      <td>mon</td>\n",
              "      <td>...</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>nonexistent</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.0</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d68191d3-07ce-4158-b1c6-3fe9e4207f34')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-d68191d3-07ce-4158-b1c6-3fe9e4207f34 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-d68191d3-07ce-4158-b1c6-3fe9e4207f34');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "col_list = df_enc.select_dtypes(include = \"object\").columns\n",
        "print(col_list)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "-O1WtWRbGx1s",
        "outputId": "0a8ef9e5-2eef-40a3-eed1-ee16fb9e9e40"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Index(['job', 'marital', 'education', 'default', 'housing', 'loan', 'contact',\n",
            "       'month', 'day_of_week', 'poutcome'],\n",
            "      dtype='object')\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Encoding"
      ],
      "metadata": {
        "id": "r_e8gNkEJYnQ"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Using OneHotEncoding: pd.get_dummies"
      ],
      "metadata": {
        "id": "yQ4fh-Ce5PF6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc= pd.get_dummies(df_enc, columns=col_list)\n",
        "df_enc.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 300
        },
        "id": "r15BXDPj4efS",
        "outputId": "5341e90b-eaaa-4a29-d7b5-32e2b08f42b4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "   age  duration  campaign  pdays  previous  emp.var.rate  cons.price.idx  \\\n",
              "0   56       261         1    999         0           1.1          93.994   \n",
              "1   57       149         1    999         0           1.1          93.994   \n",
              "2   37       226         1    999         0           1.1          93.994   \n",
              "3   40       151         1    999         0           1.1          93.994   \n",
              "4   56       307         1    999         0           1.1          93.994   \n",
              "\n",
              "   cons.conf.idx  euribor3m  nr.employed  ...  month_oct  month_sep  \\\n",
              "0          -36.4      4.857       5191.0  ...          0          0   \n",
              "1          -36.4      4.857       5191.0  ...          0          0   \n",
              "2          -36.4      4.857       5191.0  ...          0          0   \n",
              "3          -36.4      4.857       5191.0  ...          0          0   \n",
              "4          -36.4      4.857       5191.0  ...          0          0   \n",
              "\n",
              "   day_of_week_fri  day_of_week_mon  day_of_week_thu  day_of_week_tue  \\\n",
              "0                0                1                0                0   \n",
              "1                0                1                0                0   \n",
              "2                0                1                0                0   \n",
              "3                0                1                0                0   \n",
              "4                0                1                0                0   \n",
              "\n",
              "   day_of_week_wed  poutcome_failure  poutcome_nonexistent  poutcome_success  \n",
              "0                0                 0                     1                 0  \n",
              "1                0                 0                     1                 0  \n",
              "2                0                 0                     1                 0  \n",
              "3                0                 0                     1                 0  \n",
              "4                0                 0                     1                 0  \n",
              "\n",
              "[5 rows x 58 columns]"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-dd00134a-62f3-466b-8858-31a383af7bb7\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>duration</th>\n",
              "      <th>campaign</th>\n",
              "      <th>pdays</th>\n",
              "      <th>previous</th>\n",
              "      <th>emp.var.rate</th>\n",
              "      <th>cons.price.idx</th>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <th>euribor3m</th>\n",
              "      <th>nr.employed</th>\n",
              "      <th>...</th>\n",
              "      <th>month_oct</th>\n",
              "      <th>month_sep</th>\n",
              "      <th>day_of_week_fri</th>\n",
              "      <th>day_of_week_mon</th>\n",
              "      <th>day_of_week_thu</th>\n",
              "      <th>day_of_week_tue</th>\n",
              "      <th>day_of_week_wed</th>\n",
              "      <th>poutcome_failure</th>\n",
              "      <th>poutcome_nonexistent</th>\n",
              "      <th>poutcome_success</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>56</td>\n",
              "      <td>261</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>57</td>\n",
              "      <td>149</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>37</td>\n",
              "      <td>226</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>40</td>\n",
              "      <td>151</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>56</td>\n",
              "      <td>307</td>\n",
              "      <td>1</td>\n",
              "      <td>999</td>\n",
              "      <td>0</td>\n",
              "      <td>1.1</td>\n",
              "      <td>93.994</td>\n",
              "      <td>-36.4</td>\n",
              "      <td>4.857</td>\n",
              "      <td>5191.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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 58 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-dd00134a-62f3-466b-8858-31a383af7bb7')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-dd00134a-62f3-466b-8858-31a383af7bb7 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-dd00134a-62f3-466b-8858-31a383af7bb7');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 38
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Label Encoding for featur selection\n",
        "The goal is keeping the features with higher correlation with the 'y' feature. Also removing those of highly correlated that were detected in matrix correlation "
      ],
      "metadata": {
        "id": "-exRqkmq5HgP"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "le = LabelEncoder()\n",
        "for colsn in col_list:\n",
        "    df[colsn] = le.fit_transform(df[colsn])"
      ],
      "metadata": {
        "id": "t5KudClvlEMt"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "candidate_features = df.columns.drop(['y'])\n",
        "candidate_features.values"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "u4grgDzzcveE",
        "outputId": "68c5973e-b276-40e9-8244-b19ab61a466a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array(['age', 'job', 'marital', 'education', 'default', 'housing', 'loan',\n",
              "       'contact', 'month', 'day_of_week', 'duration', 'campaign', 'pdays',\n",
              "       'previous', 'poutcome', 'emp.var.rate', 'cons.price.idx',\n",
              "       'cons.conf.idx', 'euribor3m', 'nr.employed'], dtype=object)"
            ]
          },
          "metadata": {},
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "kbest = SelectKBest(k=16, score_func=f_classif)\n",
        "kbest.fit(df.loc[:, candidate_features], df.y)\n",
        "kbest.scores_"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Dw3lbnkvYbYR",
        "outputId": "981f63a7-5776-47f2-a1f3-f1ecf369c39c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([3.80946593e+01, 1.34624937e+02, 8.81070467e+01, 1.64283651e+02,\n",
              "       4.10590785e+02, 5.49665339e+00, 9.92371254e-01, 8.81707070e+02,\n",
              "       1.51493970e+00, 1.05030959e+01, 8.09410163e+03, 1.82156673e+02,\n",
              "       4.86117327e+03, 2.30425709e+03, 7.05678164e+02, 4.02382993e+03,\n",
              "       7.78589795e+02, 1.24409975e+02, 4.30947905e+03, 5.92661060e+03])"
            ]
          },
          "metadata": {},
          "execution_count": 41
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "kbestfeatures = pd.DataFrame({'feature':candidate_features.values,'score':kbest.scores_})\n",
        "kbestfeatures.set_index('feature',inplace=True)\n",
        "kbestfeatures.sort_values(by='score', ascending=False)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 708
        },
        "id": "b3kEcXHmo-rU",
        "outputId": "533993a5-e934-41b8-f930-93cb7c15759b"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "                      score\n",
              "feature                    \n",
              "duration        8094.101634\n",
              "nr.employed     5926.610601\n",
              "pdays           4861.173272\n",
              "euribor3m       4309.479048\n",
              "emp.var.rate    4023.829925\n",
              "previous        2304.257088\n",
              "contact          881.707070\n",
              "cons.price.idx   778.589795\n",
              "poutcome         705.678164\n",
              "default          410.590785\n",
              "campaign         182.156673\n",
              "education        164.283651\n",
              "job              134.624937\n",
              "cons.conf.idx    124.409975\n",
              "marital           88.107047\n",
              "age               38.094659\n",
              "day_of_week       10.503096\n",
              "housing            5.496653\n",
              "month              1.514940\n",
              "loan               0.992371"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-62389f88-693d-4b6b-873f-e6fe7243e548\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>score</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>feature</th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>duration</th>\n",
              "      <td>8094.101634</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>nr.employed</th>\n",
              "      <td>5926.610601</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>pdays</th>\n",
              "      <td>4861.173272</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>euribor3m</th>\n",
              "      <td>4309.479048</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>emp.var.rate</th>\n",
              "      <td>4023.829925</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>previous</th>\n",
              "      <td>2304.257088</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>contact</th>\n",
              "      <td>881.707070</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>cons.price.idx</th>\n",
              "      <td>778.589795</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>poutcome</th>\n",
              "      <td>705.678164</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>default</th>\n",
              "      <td>410.590785</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>campaign</th>\n",
              "      <td>182.156673</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>education</th>\n",
              "      <td>164.283651</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>job</th>\n",
              "      <td>134.624937</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <td>124.409975</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>marital</th>\n",
              "      <td>88.107047</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>age</th>\n",
              "      <td>38.094659</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>day_of_week</th>\n",
              "      <td>10.503096</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>housing</th>\n",
              "      <td>5.496653</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>month</th>\n",
              "      <td>1.514940</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>loan</th>\n",
              "      <td>0.992371</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-62389f88-693d-4b6b-873f-e6fe7243e548')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-62389f88-693d-4b6b-873f-e6fe7243e548 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-62389f88-693d-4b6b-873f-e6fe7243e548');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Selected features base on the kbest method are:"
      ],
      "metadata": {
        "id": "Uc0MLdrQ60qp"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "selected_features = candidate_features[kbest.get_support()]\n",
        "selected_features = selected_features.insert(15,'y')\n",
        "selected_features"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bFXRxxLb1MEY",
        "outputId": "93f03f0e-435a-4941-a322-c32a2733275c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['age', 'job', 'marital', 'education', 'default', 'contact', 'duration',\n",
              "       'campaign', 'pdays', 'previous', 'poutcome', 'emp.var.rate',\n",
              "       'cons.price.idx', 'cons.conf.idx', 'euribor3m', 'y', 'nr.employed'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 43
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "candidate_features"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "qtAIgxGZLS7J",
        "outputId": "3293cbbb-3ab5-4af9-dd52-70945d2a9078"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['age', 'job', 'marital', 'education', 'default', 'housing', 'loan',\n",
              "       'contact', 'month', 'day_of_week', 'duration', 'campaign', 'pdays',\n",
              "       'previous', 'poutcome', 'emp.var.rate', 'cons.price.idx',\n",
              "       'cons.conf.idx', 'euribor3m', 'nr.employed'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 44
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "numerical_cols"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3Kc7FIrBBi0D",
        "outputId": "6fdd3c68-70e0-414c-b119-43d286e7a778"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['age', 'duration', 'campaign', 'pdays', 'previous', 'emp.var.rate',\n",
              "       'cons.price.idx', 'cons.conf.idx', 'euribor3m', 'nr.employed'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 45
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "result_1 = list(set(candidate_features).difference(selected_features))\n",
        "print(result_1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yPwA_AgBL3R5",
        "outputId": "0f2f1dbe-3c56-4b0f-9040-6f33a558131c"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "['loan', 'day_of_week', 'housing', 'month']\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##KNN sample selection"
      ],
      "metadata": {
        "id": "BjgnYYCXdW3s"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "x = df_enc[cols].values\n",
        "model = KNN(contamination=.1)\n",
        "model.fit(x)\n",
        "predicted = model.predict(x)\n",
        "\n",
        "outliers = df_enc.loc[(predicted == 1),:]\n",
        "inliers = df_enc.loc[(predicted == 0),:]"
      ],
      "metadata": {
        "id": "Je90LfJ19b6d"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc = df_enc.drop(index = df_enc.loc[(predicted == 1),:].index )\n",
        "df_enc.info()"
      ],
      "metadata": {
        "id": "E4pnhEj9_QcV",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "ee4985c0-c708-4700-ecb7-86c9f64e9b88"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<class 'pandas.core.frame.DataFrame'>\n",
            "Int64Index: 37600 entries, 0 to 41180\n",
            "Data columns (total 58 columns):\n",
            " #   Column                         Non-Null Count  Dtype  \n",
            "---  ------                         --------------  -----  \n",
            " 0   age                            37600 non-null  int64  \n",
            " 1   duration                       37600 non-null  int64  \n",
            " 2   campaign                       37600 non-null  int64  \n",
            " 3   pdays                          37600 non-null  int64  \n",
            " 4   previous                       37600 non-null  int64  \n",
            " 5   emp.var.rate                   37600 non-null  float64\n",
            " 6   cons.price.idx                 37600 non-null  float64\n",
            " 7   cons.conf.idx                  37600 non-null  float64\n",
            " 8   euribor3m                      37600 non-null  float64\n",
            " 9   nr.employed                    37600 non-null  float64\n",
            " 10  y                              37600 non-null  int64  \n",
            " 11  job_blue-collar                37600 non-null  uint8  \n",
            " 12  job_pink-collar                37600 non-null  uint8  \n",
            " 13  job_retired                    37600 non-null  uint8  \n",
            " 14  job_self-employed              37600 non-null  uint8  \n",
            " 15  job_student                    37600 non-null  uint8  \n",
            " 16  job_unemployed                 37600 non-null  uint8  \n",
            " 17  job_unknown                    37600 non-null  uint8  \n",
            " 18  job_white-collar               37600 non-null  uint8  \n",
            " 19  marital_divorced               37600 non-null  uint8  \n",
            " 20  marital_married                37600 non-null  uint8  \n",
            " 21  marital_single                 37600 non-null  uint8  \n",
            " 22  marital_unknown                37600 non-null  uint8  \n",
            " 23  education_basic                37600 non-null  uint8  \n",
            " 24  education_high.school          37600 non-null  uint8  \n",
            " 25  education_illiterate           37600 non-null  uint8  \n",
            " 26  education_professional.course  37600 non-null  uint8  \n",
            " 27  education_university.degree    37600 non-null  uint8  \n",
            " 28  education_unknown              37600 non-null  uint8  \n",
            " 29  default_no                     37600 non-null  uint8  \n",
            " 30  default_unknown                37600 non-null  uint8  \n",
            " 31  default_yes                    37600 non-null  uint8  \n",
            " 32  housing_no                     37600 non-null  uint8  \n",
            " 33  housing_unknown                37600 non-null  uint8  \n",
            " 34  housing_yes                    37600 non-null  uint8  \n",
            " 35  loan_no                        37600 non-null  uint8  \n",
            " 36  loan_unknown                   37600 non-null  uint8  \n",
            " 37  loan_yes                       37600 non-null  uint8  \n",
            " 38  contact_cellular               37600 non-null  uint8  \n",
            " 39  contact_telephone              37600 non-null  uint8  \n",
            " 40  month_apr                      37600 non-null  uint8  \n",
            " 41  month_aug                      37600 non-null  uint8  \n",
            " 42  month_dec                      37600 non-null  uint8  \n",
            " 43  month_jul                      37600 non-null  uint8  \n",
            " 44  month_jun                      37600 non-null  uint8  \n",
            " 45  month_mar                      37600 non-null  uint8  \n",
            " 46  month_may                      37600 non-null  uint8  \n",
            " 47  month_nov                      37600 non-null  uint8  \n",
            " 48  month_oct                      37600 non-null  uint8  \n",
            " 49  month_sep                      37600 non-null  uint8  \n",
            " 50  day_of_week_fri                37600 non-null  uint8  \n",
            " 51  day_of_week_mon                37600 non-null  uint8  \n",
            " 52  day_of_week_thu                37600 non-null  uint8  \n",
            " 53  day_of_week_tue                37600 non-null  uint8  \n",
            " 54  day_of_week_wed                37600 non-null  uint8  \n",
            " 55  poutcome_failure               37600 non-null  uint8  \n",
            " 56  poutcome_nonexistent           37600 non-null  uint8  \n",
            " 57  poutcome_success               37600 non-null  uint8  \n",
            "dtypes: float64(5), int64(6), uint8(47)\n",
            "memory usage: 5.1 MB\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#standard Scaler for Numerical Variables\n",
        "# normalize data\n",
        "from sklearn.preprocessing import StandardScaler, MinMaxScaler, RobustScaler,Normalizer\n",
        "rr = RobustScaler()\n",
        "ss = StandardScaler()\n",
        "mm = MinMaxScaler()\n",
        "nn = Normalizer()\n",
        "\n",
        "scaler = ss\n",
        "\n",
        "\n",
        "df_enc[numerical_cols] = scaler.fit_transform(df_enc[numerical_cols])"
      ],
      "metadata": {
        "id": "G97HjR1z3MAA"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 300
        },
        "id": "8Vin6j2I53F9",
        "outputId": "8e276cb3-8293-4342-960c-817799326e68"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "        age  duration  campaign     pdays  previous  emp.var.rate  \\\n",
              "0  1.734565  0.165454 -0.602919  0.076012 -0.315927      0.579505   \n",
              "1  1.839917 -0.407251 -0.602919  0.076012 -0.315927      0.579505   \n",
              "2 -0.267123 -0.013516 -0.602919  0.076012 -0.315927      0.579505   \n",
              "3  0.048933 -0.397024 -0.602919  0.076012 -0.315927      0.579505   \n",
              "4  1.734565  0.400672 -0.602919  0.076012 -0.315927      0.579505   \n",
              "\n",
              "   cons.price.idx  cons.conf.idx  euribor3m  nr.employed  ...  month_oct  \\\n",
              "0        0.728214       0.966856   0.642695     0.224755  ...          0   \n",
              "1        0.728214       0.966856   0.642695     0.224755  ...          0   \n",
              "2        0.728214       0.966856   0.642695     0.224755  ...          0   \n",
              "3        0.728214       0.966856   0.642695     0.224755  ...          0   \n",
              "4        0.728214       0.966856   0.642695     0.224755  ...          0   \n",
              "\n",
              "   month_sep  day_of_week_fri  day_of_week_mon  day_of_week_thu  \\\n",
              "0          0                0                1                0   \n",
              "1          0                0                1                0   \n",
              "2          0                0                1                0   \n",
              "3          0                0                1                0   \n",
              "4          0                0                1                0   \n",
              "\n",
              "   day_of_week_tue  day_of_week_wed  poutcome_failure  poutcome_nonexistent  \\\n",
              "0                0                0                 0                     1   \n",
              "1                0                0                 0                     1   \n",
              "2                0                0                 0                     1   \n",
              "3                0                0                 0                     1   \n",
              "4                0                0                 0                     1   \n",
              "\n",
              "   poutcome_success  \n",
              "0                 0  \n",
              "1                 0  \n",
              "2                 0  \n",
              "3                 0  \n",
              "4                 0  \n",
              "\n",
              "[5 rows x 58 columns]"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-03254f72-e8b0-4ea6-afc7-d42e4703fafd\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>duration</th>\n",
              "      <th>campaign</th>\n",
              "      <th>pdays</th>\n",
              "      <th>previous</th>\n",
              "      <th>emp.var.rate</th>\n",
              "      <th>cons.price.idx</th>\n",
              "      <th>cons.conf.idx</th>\n",
              "      <th>euribor3m</th>\n",
              "      <th>nr.employed</th>\n",
              "      <th>...</th>\n",
              "      <th>month_oct</th>\n",
              "      <th>month_sep</th>\n",
              "      <th>day_of_week_fri</th>\n",
              "      <th>day_of_week_mon</th>\n",
              "      <th>day_of_week_thu</th>\n",
              "      <th>day_of_week_tue</th>\n",
              "      <th>day_of_week_wed</th>\n",
              "      <th>poutcome_failure</th>\n",
              "      <th>poutcome_nonexistent</th>\n",
              "      <th>poutcome_success</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>1.734565</td>\n",
              "      <td>0.165454</td>\n",
              "      <td>-0.602919</td>\n",
              "      <td>0.076012</td>\n",
              "      <td>-0.315927</td>\n",
              "      <td>0.579505</td>\n",
              "      <td>0.728214</td>\n",
              "      <td>0.966856</td>\n",
              "      <td>0.642695</td>\n",
              "      <td>0.224755</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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>1.839917</td>\n",
              "      <td>-0.407251</td>\n",
              "      <td>-0.602919</td>\n",
              "      <td>0.076012</td>\n",
              "      <td>-0.315927</td>\n",
              "      <td>0.579505</td>\n",
              "      <td>0.728214</td>\n",
              "      <td>0.966856</td>\n",
              "      <td>0.642695</td>\n",
              "      <td>0.224755</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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>-0.267123</td>\n",
              "      <td>-0.013516</td>\n",
              "      <td>-0.602919</td>\n",
              "      <td>0.076012</td>\n",
              "      <td>-0.315927</td>\n",
              "      <td>0.579505</td>\n",
              "      <td>0.728214</td>\n",
              "      <td>0.966856</td>\n",
              "      <td>0.642695</td>\n",
              "      <td>0.224755</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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>0.048933</td>\n",
              "      <td>-0.397024</td>\n",
              "      <td>-0.602919</td>\n",
              "      <td>0.076012</td>\n",
              "      <td>-0.315927</td>\n",
              "      <td>0.579505</td>\n",
              "      <td>0.728214</td>\n",
              "      <td>0.966856</td>\n",
              "      <td>0.642695</td>\n",
              "      <td>0.224755</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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>1.734565</td>\n",
              "      <td>0.400672</td>\n",
              "      <td>-0.602919</td>\n",
              "      <td>0.076012</td>\n",
              "      <td>-0.315927</td>\n",
              "      <td>0.579505</td>\n",
              "      <td>0.728214</td>\n",
              "      <td>0.966856</td>\n",
              "      <td>0.642695</td>\n",
              "      <td>0.224755</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>1</td>\n",
              "      <td>0</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 58 columns</p>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-03254f72-e8b0-4ea6-afc7-d42e4703fafd')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-03254f72-e8b0-4ea6-afc7-d42e4703fafd button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-03254f72-e8b0-4ea6-afc7-d42e4703fafd');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 50
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df_enc.columns"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tpK8TDCsAxo4",
        "outputId": "a7b96698-167e-454a-9fb2-a71c3272e36f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['age', 'duration', 'campaign', 'pdays', 'previous', 'emp.var.rate',\n",
              "       'cons.price.idx', 'cons.conf.idx', 'euribor3m', 'nr.employed', 'y',\n",
              "       'job_blue-collar', 'job_pink-collar', 'job_retired',\n",
              "       'job_self-employed', 'job_student', 'job_unemployed', 'job_unknown',\n",
              "       'job_white-collar', 'marital_divorced', 'marital_married',\n",
              "       'marital_single', 'marital_unknown', 'education_basic',\n",
              "       'education_high.school', 'education_illiterate',\n",
              "       'education_professional.course', 'education_university.degree',\n",
              "       'education_unknown', 'default_no', 'default_unknown', 'default_yes',\n",
              "       'housing_no', 'housing_unknown', 'housing_yes', 'loan_no',\n",
              "       'loan_unknown', 'loan_yes', 'contact_cellular', 'contact_telephone',\n",
              "       'month_apr', 'month_aug', 'month_dec', 'month_jul', 'month_jun',\n",
              "       'month_mar', 'month_may', 'month_nov', 'month_oct', 'month_sep',\n",
              "       'day_of_week_fri', 'day_of_week_mon', 'day_of_week_thu',\n",
              "       'day_of_week_tue', 'day_of_week_wed', 'poutcome_failure',\n",
              "       'poutcome_nonexistent', 'poutcome_success'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 51
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##DataSet"
      ],
      "metadata": {
        "id": "meqrjwQnTd5l"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "X = df_enc.drop(columns=['y'])\n",
        "y = df_enc['y']\n",
        "print(X.shape)\n",
        "print(y.shape)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RF8IOrgFTQZS",
        "outputId": "4751b524-7447-4b55-e37e-02c679d0dc1a"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(37600, 57)\n",
            "(37600,)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "y.value_counts()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XtA_JfttTIPI",
        "outputId": "814914fc-b2d4-4c43-be9d-43c9c8feba55"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0    34895\n",
              "1     2705\n",
              "Name: y, dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 69
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Resampling"
      ],
      "metadata": {
        "id": "1uaONZ-WVKua"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sampler = RandomOverSampler(random_state=42)\n",
        "X_sampled, y_sampled = sampler.fit_resample(X, y)\n",
        "pd.Series(y_sampled).value_counts().to_frame()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 112
        },
        "id": "vVIfCD8aTpvU",
        "outputId": "71acd9a3-f091-41be-a907-3dfb20c61cf4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "       y\n",
              "0  34895\n",
              "1  34895"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-8400d9ba-26b8-4586-a174-8a296e831004\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>y</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>34895</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>34895</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-8400d9ba-26b8-4586-a174-8a296e831004')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-8400d9ba-26b8-4586-a174-8a296e831004 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-8400d9ba-26b8-4586-a174-8a296e831004');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 70
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "X_samp_p = Output + 'X_sampled.csv'\n",
        "y_samp_p= Output + 'y_sampled.csv'\n",
        "X_sampled.to_csv(X_samp_p, index=False)\n",
        "y_sampled.to_csv(y_samp_p, index=False)"
      ],
      "metadata": {
        "id": "1WqQY3I0YH0d"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "##Principal Component Analysis"
      ],
      "metadata": {
        "id": "NwimVmQzVBW6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#Highly correlated with other features\n",
        "remove_cols1 = ['emp.var.rate','euribor3m']\n",
        "#Low correlation with the 'y' label\n",
        "remove_cols2 =['day_of_week', 'month', 'loan', 'housing']\n",
        "\n",
        "rm_cols =['emp.var.rate','euribor3m','housing_no', 'housing_unknown', 'housing_yes', \n",
        "          'loan_no','loan_unknown', 'loan_yes','month_apr', 'month_aug', 'month_dec',\n",
        "          'month_jul', 'month_jun','month_mar', 'month_may', 'month_nov', 'month_oct', \n",
        "          'month_sep','day_of_week_fri', 'day_of_week_mon', 'day_of_week_thu',\n",
        "          'day_of_week_tue', 'day_of_week_wed']"
      ],
      "metadata": {
        "id": "oqpJ6SsCv8TZ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_rm = df_enc.drop(rm_cols,axis=1)\n",
        "df_rm.info()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "18z_rOR3wXTR",
        "outputId": "e156e751-6d4a-4b06-9da1-9d559fde4d28"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "<class 'pandas.core.frame.DataFrame'>\n",
            "Int64Index: 37600 entries, 0 to 41180\n",
            "Data columns (total 35 columns):\n",
            " #   Column                         Non-Null Count  Dtype  \n",
            "---  ------                         --------------  -----  \n",
            " 0   age                            37600 non-null  float64\n",
            " 1   duration                       37600 non-null  float64\n",
            " 2   campaign                       37600 non-null  float64\n",
            " 3   pdays                          37600 non-null  float64\n",
            " 4   previous                       37600 non-null  float64\n",
            " 5   cons.price.idx                 37600 non-null  float64\n",
            " 6   cons.conf.idx                  37600 non-null  float64\n",
            " 7   nr.employed                    37600 non-null  float64\n",
            " 8   y                              37600 non-null  int64  \n",
            " 9   job_blue-collar                37600 non-null  uint8  \n",
            " 10  job_pink-collar                37600 non-null  uint8  \n",
            " 11  job_retired                    37600 non-null  uint8  \n",
            " 12  job_self-employed              37600 non-null  uint8  \n",
            " 13  job_student                    37600 non-null  uint8  \n",
            " 14  job_unemployed                 37600 non-null  uint8  \n",
            " 15  job_unknown                    37600 non-null  uint8  \n",
            " 16  job_white-collar               37600 non-null  uint8  \n",
            " 17  marital_divorced               37600 non-null  uint8  \n",
            " 18  marital_married                37600 non-null  uint8  \n",
            " 19  marital_single                 37600 non-null  uint8  \n",
            " 20  marital_unknown                37600 non-null  uint8  \n",
            " 21  education_basic                37600 non-null  uint8  \n",
            " 22  education_high.school          37600 non-null  uint8  \n",
            " 23  education_illiterate           37600 non-null  uint8  \n",
            " 24  education_professional.course  37600 non-null  uint8  \n",
            " 25  education_university.degree    37600 non-null  uint8  \n",
            " 26  education_unknown              37600 non-null  uint8  \n",
            " 27  default_no                     37600 non-null  uint8  \n",
            " 28  default_unknown                37600 non-null  uint8  \n",
            " 29  default_yes                    37600 non-null  uint8  \n",
            " 30  contact_cellular               37600 non-null  uint8  \n",
            " 31  contact_telephone              37600 non-null  uint8  \n",
            " 32  poutcome_failure               37600 non-null  uint8  \n",
            " 33  poutcome_nonexistent           37600 non-null  uint8  \n",
            " 34  poutcome_success               37600 non-null  uint8  \n",
            "dtypes: float64(8), int64(1), uint8(26)\n",
            "memory usage: 3.8 MB\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "pca = PCA(n_components = 10)\n",
        "pca.fit(X_sampled)\n",
        "X = pca.transform(X_sampled)\n",
        "print(X_sampled.shape, y_sampled.shape,X.shape)"
      ],
      "metadata": {
        "id": "zjxqxFDDU5xQ",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "38cc0312-95c5-4d66-9f4a-b2c523f46ec6"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "(69790, 57) (69790,) (69790, 10)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df_y = pd.DataFrame(data = y_sampled, columns = ['y'])\n",
        "df_X = pd.DataFrame(data = X, columns = ['PC_1', 'PC_2','PC_3', 'PC_4','PC_5','PC_6', 'PC_7','PC_8', 'PC_9','PC_10'])"
      ],
      "metadata": {
        "id": "lTNn2j6ZVSF3"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_X.head()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "WOQAA1VZZNpb",
        "outputId": "c3e6b333-c794-473f-ef90-8daabb7c872e"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "       PC_1      PC_2      PC_3      PC_4      PC_5      PC_6      PC_7  \\\n",
              "0 -1.585724  1.206304 -1.281867  1.243291  1.052113 -0.130384 -0.911600   \n",
              "1 -1.562223  1.226528 -1.831567  1.083381  1.139825 -0.130978 -0.934458   \n",
              "2 -1.452580  1.103869 -1.062771  0.242538 -0.596542  0.176359 -0.838436   \n",
              "3 -1.429701  1.122022 -1.487277  0.305936 -0.313612  0.151828 -0.885753   \n",
              "4 -1.597920  1.207887 -1.046916  1.285781  0.910081 -0.120395 -0.837654   \n",
              "\n",
              "       PC_8      PC_9     PC_10  \n",
              "0  0.967854  0.090649 -0.568793  \n",
              "1  0.947986  0.240112 -0.550584  \n",
              "2  0.581073  0.059275  0.846881  \n",
              "3  0.761757  0.246368 -0.600256  \n",
              "4  0.869661  0.488248 -0.454807  "
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-6867cc60-72b3-4992-9664-ff58899a8505\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <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>PC_1</th>\n",
              "      <th>PC_2</th>\n",
              "      <th>PC_3</th>\n",
              "      <th>PC_4</th>\n",
              "      <th>PC_5</th>\n",
              "      <th>PC_6</th>\n",
              "      <th>PC_7</th>\n",
              "      <th>PC_8</th>\n",
              "      <th>PC_9</th>\n",
              "      <th>PC_10</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>-1.585724</td>\n",
              "      <td>1.206304</td>\n",
              "      <td>-1.281867</td>\n",
              "      <td>1.243291</td>\n",
              "      <td>1.052113</td>\n",
              "      <td>-0.130384</td>\n",
              "      <td>-0.911600</td>\n",
              "      <td>0.967854</td>\n",
              "      <td>0.090649</td>\n",
              "      <td>-0.568793</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>-1.562223</td>\n",
              "      <td>1.226528</td>\n",
              "      <td>-1.831567</td>\n",
              "      <td>1.083381</td>\n",
              "      <td>1.139825</td>\n",
              "      <td>-0.130978</td>\n",
              "      <td>-0.934458</td>\n",
              "      <td>0.947986</td>\n",
              "      <td>0.240112</td>\n",
              "      <td>-0.550584</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>-1.452580</td>\n",
              "      <td>1.103869</td>\n",
              "      <td>-1.062771</td>\n",
              "      <td>0.242538</td>\n",
              "      <td>-0.596542</td>\n",
              "      <td>0.176359</td>\n",
              "      <td>-0.838436</td>\n",
              "      <td>0.581073</td>\n",
              "      <td>0.059275</td>\n",
              "      <td>0.846881</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>-1.429701</td>\n",
              "      <td>1.122022</td>\n",
              "      <td>-1.487277</td>\n",
              "      <td>0.305936</td>\n",
              "      <td>-0.313612</td>\n",
              "      <td>0.151828</td>\n",
              "      <td>-0.885753</td>\n",
              "      <td>0.761757</td>\n",
              "      <td>0.246368</td>\n",
              "      <td>-0.600256</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>-1.597920</td>\n",
              "      <td>1.207887</td>\n",
              "      <td>-1.046916</td>\n",
              "      <td>1.285781</td>\n",
              "      <td>0.910081</td>\n",
              "      <td>-0.120395</td>\n",
              "      <td>-0.837654</td>\n",
              "      <td>0.869661</td>\n",
              "      <td>0.488248</td>\n",
              "      <td>-0.454807</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-6867cc60-72b3-4992-9664-ff58899a8505')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-6867cc60-72b3-4992-9664-ff58899a8505 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-6867cc60-72b3-4992-9664-ff58899a8505');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 74
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df_y.value_counts()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lh7EhkpLZSOI",
        "outputId": "5d9551b1-2e3b-4e62-ed9c-2564295cb235"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "y\n",
              "0    34895\n",
              "1    34895\n",
              "dtype: int64"
            ]
          },
          "metadata": {},
          "execution_count": 75
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#!git pull '/content/Interview/'\n",
        "!git add '/content/Interview/Model'"
      ],
      "metadata": {
        "id": "t9eOcVaIY_Pp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "path_y = Output + 'df_y.csv'\n",
        "path_X = Output + 'df_X.csv'\n",
        "df_y.to_csv(path_y, index=False)\n",
        "df_X.to_csv(path_X, index=False)"
      ],
      "metadata": {
        "id": "xnkufzUHVkuN"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df_y.to_csv('/content/df_y.csv', index=False)\n",
        "df_X.to_csv('/content/df_X.csv', index=False)"
      ],
      "metadata": {
        "id": "yuyxyTsKaYDy"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!cd '/content/Interview/'"
      ],
      "metadata": {
        "id": "PApjUwZIXiig"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!git commit -m 'Aviva'  # commit in Colab\n",
        "!git push '/content/Interview/'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vQvathnlWDXn",
        "outputId": "487d264a-8f57-4a47-d813-8a12b1e630f5"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[main 96f2904] Aviva\n",
            " 2 files changed, 139582 insertions(+), 139582 deletions(-)\n",
            " rewrite Model/X_sampled.csv (67%)\n",
            " rewrite Model/df_X.csv (68%)\n",
            "Everything up-to-date\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!git push"
      ],
      "metadata": {
        "id": "qONisf7dXrlv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "5e99738e-696b-4aad-9b72-7ece64a18209"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Counting objects: 5, done.\n",
            "Delta compression using up to 2 threads.\n",
            "Compressing objects: 100% (5/5), done.\n",
            "Writing objects: 100% (5/5), 7.06 MiB | 1.90 MiB/s, done.\n",
            "Total 5 (delta 3), reused 0 (delta 0)\n",
            "remote: Resolving deltas: 100% (3/3), completed with 3 local objects.\u001b[K\n",
            "To https://github.com/Sahel-Eskandar/Interview.git\n",
            "   e4d9892..96f2904  main -> main\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "BURAUCs-Wy5G"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}