{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# импортируем библиотеки numpy, pandas и missingno\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import missingno as msno\n",
    "# импортируем модуль stats библиотеки scipy\n",
    "from scipy import stats\n",
    "# импортируем функцию train_test_split(), с помощью\n",
    "# которой разбиваем данные на обучающие и тестовые\n",
    "from sklearn.model_selection import train_test_split\n",
    "# импортируем классы SimpleImputer, StandardScaler, Pipeline\n",
    "# и функцию cross_val_score для нашей функции importance_auc()\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.model_selection import cross_val_score\n",
    "# импортируем функцию roc_auc_score() для\n",
    "# вычисления AUC-ROC\n",
    "from sklearn.metrics import roc_auc_score\n",
    "# импортируем класс LogisticRegression\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "# импортируем собственный класс логистической регрессии\n",
    "from custom_logreg_classes import RegularizedLogisticRegression_Newton"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.766127</td>\n",
       "      <td>45</td>\n",
       "      <td>2</td>\n",
       "      <td>0.802982</td>\n",
       "      <td>9120.0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0.957151</td>\n",
       "      <td>40</td>\n",
       "      <td>0</td>\n",
       "      <td>0.121876</td>\n",
       "      <td>2600.0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.658180</td>\n",
       "      <td>38</td>\n",
       "      <td>1</td>\n",
       "      <td>0.085113</td>\n",
       "      <td>3042.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0.233810</td>\n",
       "      <td>30</td>\n",
       "      <td>0</td>\n",
       "      <td>0.036050</td>\n",
       "      <td>3300.0</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0.907239</td>\n",
       "      <td>49</td>\n",
       "      <td>1</td>\n",
       "      <td>0.024926</td>\n",
       "      <td>63588.0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "1                 1                              0.766127   45   \n",
       "2                 0                              0.957151   40   \n",
       "3                 0                              0.658180   38   \n",
       "4                 0                              0.233810   30   \n",
       "5                 0                              0.907239   49   \n",
       "\n",
       "   NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "1                                     2   0.802982         9120.0   \n",
       "2                                     0   0.121876         2600.0   \n",
       "3                                     1   0.085113         3042.0   \n",
       "4                                     0   0.036050         3300.0   \n",
       "5                                     1   0.024926        63588.0   \n",
       "\n",
       "   NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "1                               13                        0   \n",
       "2                                4                        0   \n",
       "3                                2                        1   \n",
       "4                                5                        0   \n",
       "5                                7                        0   \n",
       "\n",
       "   NumberRealEstateLoansOrLines  NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "1                             6                                     0   \n",
       "2                             0                                     0   \n",
       "3                             0                                     0   \n",
       "4                             0                                     0   \n",
       "5                             1                                     0   \n",
       "\n",
       "   NumberOfDependents  \n",
       "1                 2.0  \n",
       "2                 1.0  \n",
       "3                 0.0  \n",
       "4                 0.0  \n",
       "5                 0.0  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# считываем данные\n",
    "data = pd.read_csv('Data/cs-training.csv', \n",
    "                   index_col='Unnamed: 0')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 150000 entries, 1 to 150000\n",
      "Data columns (total 11 columns):\n",
      " #   Column                                Non-Null Count   Dtype  \n",
      "---  ------                                --------------   -----  \n",
      " 0   SeriousDlqin2yrs                      150000 non-null  int64  \n",
      " 1   RevolvingUtilizationOfUnsecuredLines  150000 non-null  float64\n",
      " 2   age                                   150000 non-null  int64  \n",
      " 3   NumberOfTime30-59DaysPastDueNotWorse  150000 non-null  int64  \n",
      " 4   DebtRatio                             150000 non-null  float64\n",
      " 5   MonthlyIncome                         120269 non-null  float64\n",
      " 6   NumberOfOpenCreditLinesAndLoans       150000 non-null  int64  \n",
      " 7   NumberOfTimes90DaysLate               150000 non-null  int64  \n",
      " 8   NumberRealEstateLoansOrLines          150000 non-null  int64  \n",
      " 9   NumberOfTime60-89DaysPastDueNotWorse  150000 non-null  int64  \n",
      " 10  NumberOfDependents                    146076 non-null  float64\n",
      "dtypes: float64(4), int64(7)\n",
      "memory usage: 13.7 MB\n"
     ]
    }
   ],
   "source": [
    "# смотрим типы переменных и информацию о количестве пропусков\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 130536 characters]\n",
      "text/plain": [
       "<Figure size 1100x1100 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# визуализируем пропуски с помощью missingno\n",
    "msno.matrix(data, sparkline=False, figsize=(11, 11));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>120269.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>150000.000</td>\n",
       "      <td>146076.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.067</td>\n",
       "      <td>6.048</td>\n",
       "      <td>52.295</td>\n",
       "      <td>0.421</td>\n",
       "      <td>353.005</td>\n",
       "      <td>6670.221</td>\n",
       "      <td>8.453</td>\n",
       "      <td>0.266</td>\n",
       "      <td>1.018</td>\n",
       "      <td>0.240</td>\n",
       "      <td>0.757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.250</td>\n",
       "      <td>249.755</td>\n",
       "      <td>14.772</td>\n",
       "      <td>4.193</td>\n",
       "      <td>2037.819</td>\n",
       "      <td>14384.674</td>\n",
       "      <td>5.146</td>\n",
       "      <td>4.169</td>\n",
       "      <td>1.130</td>\n",
       "      <td>4.155</td>\n",
       "      <td>1.115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.030</td>\n",
       "      <td>41.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.175</td>\n",
       "      <td>3400.000</td>\n",
       "      <td>5.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.154</td>\n",
       "      <td>52.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.367</td>\n",
       "      <td>5400.000</td>\n",
       "      <td>8.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.559</td>\n",
       "      <td>63.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.868</td>\n",
       "      <td>8249.000</td>\n",
       "      <td>11.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>2.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000</td>\n",
       "      <td>50708.000</td>\n",
       "      <td>109.000</td>\n",
       "      <td>98.000</td>\n",
       "      <td>329664.000</td>\n",
       "      <td>3008750.000</td>\n",
       "      <td>58.000</td>\n",
       "      <td>98.000</td>\n",
       "      <td>54.000</td>\n",
       "      <td>98.000</td>\n",
       "      <td>20.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines        age  \\\n",
       "count        150000.000                            150000.000 150000.000   \n",
       "mean              0.067                                 6.048     52.295   \n",
       "std               0.250                               249.755     14.772   \n",
       "min               0.000                                 0.000      0.000   \n",
       "25%               0.000                                 0.030     41.000   \n",
       "50%               0.000                                 0.154     52.000   \n",
       "75%               0.000                                 0.559     63.000   \n",
       "max               1.000                             50708.000    109.000   \n",
       "\n",
       "       NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "count                            150000.000 150000.000     120269.000   \n",
       "mean                                  0.421    353.005       6670.221   \n",
       "std                                   4.193   2037.819      14384.674   \n",
       "min                                   0.000      0.000          0.000   \n",
       "25%                                   0.000      0.175       3400.000   \n",
       "50%                                   0.000      0.367       5400.000   \n",
       "75%                                   0.000      0.868       8249.000   \n",
       "max                                  98.000 329664.000    3008750.000   \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "count                       150000.000               150000.000   \n",
       "mean                             8.453                    0.266   \n",
       "std                              5.146                    4.169   \n",
       "min                              0.000                    0.000   \n",
       "25%                              5.000                    0.000   \n",
       "50%                              8.000                    0.000   \n",
       "75%                             11.000                    0.000   \n",
       "max                             58.000                   98.000   \n",
       "\n",
       "       NumberRealEstateLoansOrLines  NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "count                    150000.000                            150000.000   \n",
       "mean                          1.018                                 0.240   \n",
       "std                           1.130                                 4.155   \n",
       "min                           0.000                                 0.000   \n",
       "25%                           0.000                                 0.000   \n",
       "50%                           1.000                                 0.000   \n",
       "75%                           2.000                                 0.000   \n",
       "max                          54.000                                98.000   \n",
       "\n",
       "       NumberOfDependents  \n",
       "count          146076.000  \n",
       "mean                0.757  \n",
       "std                 1.115  \n",
       "min                 0.000  \n",
       "25%                 0.000  \n",
       "50%                 0.000  \n",
       "75%                 1.000  \n",
       "max                20.000  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# смотрим статистики количественных переменных\n",
    "pd.set_option('display.float_format', lambda x: '%.3f' % x)\n",
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "73764    3008750.000\n",
       "137141   1794060.000\n",
       "111366   1560100.000\n",
       "50641    1072500.000\n",
       "122544    835040.000\n",
       "123292    730483.000\n",
       "93565     702500.000\n",
       "96550     699530.000\n",
       "119137    649587.000\n",
       "37079     629000.000\n",
       "Name: MonthlyIncome, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# выведем 10 наибольших значений MonthlyIncome\n",
    "data['MonthlyIncome'].nlargest(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>35974</th>\n",
       "      <td>0</td>\n",
       "      <td>0.440</td>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>0.004</td>\n",
       "      <td>582369.000</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37079</th>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>83</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>629000.000</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50641</th>\n",
       "      <td>0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>44</td>\n",
       "      <td>1</td>\n",
       "      <td>0.005</td>\n",
       "      <td>1072500.000</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73764</th>\n",
       "      <td>0</td>\n",
       "      <td>0.007</td>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>3008750.000</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93565</th>\n",
       "      <td>0</td>\n",
       "      <td>0.072</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>0.008</td>\n",
       "      <td>702500.000</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96550</th>\n",
       "      <td>0</td>\n",
       "      <td>0.064</td>\n",
       "      <td>52</td>\n",
       "      <td>0</td>\n",
       "      <td>0.004</td>\n",
       "      <td>699530.000</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>111366</th>\n",
       "      <td>0</td>\n",
       "      <td>0.164</td>\n",
       "      <td>44</td>\n",
       "      <td>0</td>\n",
       "      <td>0.004</td>\n",
       "      <td>1560100.000</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>2.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>119137</th>\n",
       "      <td>0</td>\n",
       "      <td>0.151</td>\n",
       "      <td>49</td>\n",
       "      <td>0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>649587.000</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122544</th>\n",
       "      <td>0</td>\n",
       "      <td>0.042</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>835040.000</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123292</th>\n",
       "      <td>0</td>\n",
       "      <td>0.226</td>\n",
       "      <td>67</td>\n",
       "      <td>1</td>\n",
       "      <td>0.006</td>\n",
       "      <td>730483.000</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137141</th>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "      <td>0.003</td>\n",
       "      <td>1794060.000</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137427</th>\n",
       "      <td>0</td>\n",
       "      <td>0.080</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "      <td>0.002</td>\n",
       "      <td>562466.000</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "35974                  0                                 0.440   64   \n",
       "37079                  0                                 0.000   83   \n",
       "50641                  0                                 0.469   44   \n",
       "73764                  0                                 0.007   52   \n",
       "93565                  0                                 0.072   50   \n",
       "96550                  0                                 0.064   52   \n",
       "111366                 0                                 0.164   44   \n",
       "119137                 0                                 0.151   49   \n",
       "122544                 0                                 0.042   55   \n",
       "123292                 0                                 0.226   67   \n",
       "137141                 0                                 0.000   68   \n",
       "137427                 0                                 0.080   61   \n",
       "\n",
       "        NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "35974                                      0      0.004     582369.000   \n",
       "37079                                      0      0.000     629000.000   \n",
       "50641                                      1      0.005    1072500.000   \n",
       "73764                                      0      0.001    3008750.000   \n",
       "93565                                      0      0.008     702500.000   \n",
       "96550                                      0      0.004     699530.000   \n",
       "111366                                     0      0.004    1560100.000   \n",
       "119137                                     0      0.001     649587.000   \n",
       "122544                                     0      0.000     835040.000   \n",
       "123292                                     1      0.006     730483.000   \n",
       "137141                                     0      0.003    1794060.000   \n",
       "137427                                     0      0.002     562466.000   \n",
       "\n",
       "        NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "35974                                11                        0   \n",
       "37079                                 3                        0   \n",
       "50641                                 9                        0   \n",
       "73764                                10                        0   \n",
       "93565                                12                        0   \n",
       "96550                                11                        0   \n",
       "111366                               12                        0   \n",
       "119137                                8                        0   \n",
       "122544                                8                        0   \n",
       "123292                               23                        0   \n",
       "137141                               15                        0   \n",
       "137427                                7                        0   \n",
       "\n",
       "        NumberRealEstateLoansOrLines  NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "35974                              0                                     0   \n",
       "37079                              0                                     0   \n",
       "50641                              2                                     0   \n",
       "73764                              1                                     0   \n",
       "93565                              4                                     0   \n",
       "96550                              2                                     0   \n",
       "111366                             4                                     0   \n",
       "119137                             0                                     1   \n",
       "122544                             0                                     0   \n",
       "123292                             1                                     0   \n",
       "137141                             1                                     0   \n",
       "137427                             0                                     0   \n",
       "\n",
       "        NumberOfDependents  \n",
       "35974                1.000  \n",
       "37079                0.000  \n",
       "50641                1.000  \n",
       "73764                3.000  \n",
       "93565                2.000  \n",
       "96550                1.000  \n",
       "111366               2.000  \n",
       "119137               3.000  \n",
       "122544               0.000  \n",
       "123292               0.000  \n",
       "137141               0.000  \n",
       "137427               1.000  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# взглянем на значения MonthlyIncome больше 500000\n",
    "data[data['MonthlyIncome'] > 500000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0</td>\n",
       "      <td>0.020</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "      <td>477.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0</td>\n",
       "      <td>0.603</td>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>0.066</td>\n",
       "      <td>333.000</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>0.473</td>\n",
       "      <td>750.000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>0</td>\n",
       "      <td>0.364</td>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "      <td>0.010</td>\n",
       "      <td>1000.000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>0</td>\n",
       "      <td>0.369</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "      <td>1687.500</td>\n",
       "      <td>1.000</td>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149883</th>\n",
       "      <td>0</td>\n",
       "      <td>0.062</td>\n",
       "      <td>77</td>\n",
       "      <td>0</td>\n",
       "      <td>714.500</td>\n",
       "      <td>1.000</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149951</th>\n",
       "      <td>0</td>\n",
       "      <td>0.005</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "      <td>1940.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149955</th>\n",
       "      <td>1</td>\n",
       "      <td>1.000</td>\n",
       "      <td>46</td>\n",
       "      <td>0</td>\n",
       "      <td>170.398</td>\n",
       "      <td>401.000</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149962</th>\n",
       "      <td>1</td>\n",
       "      <td>0.920</td>\n",
       "      <td>31</td>\n",
       "      <td>1</td>\n",
       "      <td>0.177</td>\n",
       "      <td>1125.000</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149994</th>\n",
       "      <td>0</td>\n",
       "      <td>1.000</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>820.000</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5335 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "15                     0                                 0.020   76   \n",
       "20                     0                                 0.603   25   \n",
       "32                     0                                 1.000   24   \n",
       "39                     0                                 0.364   26   \n",
       "45                     0                                 0.369   68   \n",
       "...                  ...                                   ...  ...   \n",
       "149883                 0                                 0.062   77   \n",
       "149951                 0                                 0.005   61   \n",
       "149955                 1                                 1.000   46   \n",
       "149962                 1                                 0.920   31   \n",
       "149994                 0                                 1.000   22   \n",
       "\n",
       "        NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "15                                         0    477.000          0.000   \n",
       "20                                         0      0.066        333.000   \n",
       "32                                         0      0.473        750.000   \n",
       "39                                         0      0.010       1000.000   \n",
       "45                                         0   1687.500          1.000   \n",
       "...                                      ...        ...            ...   \n",
       "149883                                     0    714.500          1.000   \n",
       "149951                                     0   1940.000          0.000   \n",
       "149955                                     0    170.398        401.000   \n",
       "149962                                     1      0.177       1125.000   \n",
       "149994                                     0      0.000        820.000   \n",
       "\n",
       "        NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "15                                    6                        0   \n",
       "20                                    2                        0   \n",
       "32                                    1                        0   \n",
       "39                                    1                        0   \n",
       "45                                   31                        0   \n",
       "...                                 ...                      ...   \n",
       "149883                                3                        0   \n",
       "149951                               10                        0   \n",
       "149955                                3                        2   \n",
       "149962                                4                        1   \n",
       "149994                                1                        0   \n",
       "\n",
       "        NumberRealEstateLoansOrLines  NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "15                                 1                                     0   \n",
       "20                                 0                                     0   \n",
       "32                                 0                                     0   \n",
       "39                                 0                                     0   \n",
       "45                                 1                                     0   \n",
       "...                              ...                                   ...   \n",
       "149883                             0                                     0   \n",
       "149951                             1                                     0   \n",
       "149955                             0                                     0   \n",
       "149962                             0                                     0   \n",
       "149994                             0                                     0   \n",
       "\n",
       "        NumberOfDependents  \n",
       "15                   0.000  \n",
       "20                   0.000  \n",
       "32                   0.000  \n",
       "39                   0.000  \n",
       "45                   0.000  \n",
       "...                    ...  \n",
       "149883               0.000  \n",
       "149951               0.000  \n",
       "149955               2.000  \n",
       "149962               0.000  \n",
       "149994               0.000  \n",
       "\n",
       "[5335 rows x 11 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# взглянем на значения MonthlyIncome менее 1200\n",
    "data[data['MonthlyIncome'] < 1200]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0.306</td>\n",
       "      <td>57</td>\n",
       "      <td>0</td>\n",
       "      <td>5710.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>0.117</td>\n",
       "      <td>27</td>\n",
       "      <td>0</td>\n",
       "      <td>46.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0</td>\n",
       "      <td>0.061</td>\n",
       "      <td>78</td>\n",
       "      <td>0</td>\n",
       "      <td>2058.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0</td>\n",
       "      <td>0.083</td>\n",
       "      <td>62</td>\n",
       "      <td>0</td>\n",
       "      <td>977.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>0</td>\n",
       "      <td>0.073</td>\n",
       "      <td>81</td>\n",
       "      <td>0</td>\n",
       "      <td>75.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149977</th>\n",
       "      <td>0</td>\n",
       "      <td>0.001</td>\n",
       "      <td>76</td>\n",
       "      <td>0</td>\n",
       "      <td>60.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149978</th>\n",
       "      <td>0</td>\n",
       "      <td>0.236</td>\n",
       "      <td>29</td>\n",
       "      <td>0</td>\n",
       "      <td>349.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149985</th>\n",
       "      <td>0</td>\n",
       "      <td>0.038</td>\n",
       "      <td>84</td>\n",
       "      <td>0</td>\n",
       "      <td>25.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149993</th>\n",
       "      <td>0</td>\n",
       "      <td>0.872</td>\n",
       "      <td>50</td>\n",
       "      <td>0</td>\n",
       "      <td>4132.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>0</td>\n",
       "      <td>0.246</td>\n",
       "      <td>58</td>\n",
       "      <td>0</td>\n",
       "      <td>3870.000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>29731 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "7                      0                                 0.306   57   \n",
       "9                      0                                 0.117   27   \n",
       "17                     0                                 0.061   78   \n",
       "33                     0                                 0.083   62   \n",
       "42                     0                                 0.073   81   \n",
       "...                  ...                                   ...  ...   \n",
       "149977                 0                                 0.001   76   \n",
       "149978                 0                                 0.236   29   \n",
       "149985                 0                                 0.038   84   \n",
       "149993                 0                                 0.872   50   \n",
       "149998                 0                                 0.246   58   \n",
       "\n",
       "        NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "7                                          0   5710.000            NaN   \n",
       "9                                          0     46.000            NaN   \n",
       "17                                         0   2058.000            NaN   \n",
       "33                                         0    977.000            NaN   \n",
       "42                                         0     75.000            NaN   \n",
       "...                                      ...        ...            ...   \n",
       "149977                                     0     60.000            NaN   \n",
       "149978                                     0    349.000            NaN   \n",
       "149985                                     0     25.000            NaN   \n",
       "149993                                     0   4132.000            NaN   \n",
       "149998                                     0   3870.000            NaN   \n",
       "\n",
       "        NumberOfOpenCreditLinesAndLoans  NumberOfTimes90DaysLate  \\\n",
       "7                                     8                        0   \n",
       "9                                     2                        0   \n",
       "17                                   10                        0   \n",
       "33                                    6                        0   \n",
       "42                                    7                        0   \n",
       "...                                 ...                      ...   \n",
       "149977                                5                        0   \n",
       "149978                                3                        0   \n",
       "149985                                5                        0   \n",
       "149993                               11                        0   \n",
       "149998                               18                        0   \n",
       "\n",
       "        NumberRealEstateLoansOrLines  NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "7                                  3                                     0   \n",
       "9                                  0                                     0   \n",
       "17                                 2                                     0   \n",
       "33                                 1                                     0   \n",
       "42                                 0                                     0   \n",
       "...                              ...                                   ...   \n",
       "149977                             0                                     0   \n",
       "149978                             0                                     0   \n",
       "149985                             0                                     0   \n",
       "149993                             1                                     0   \n",
       "149998                             1                                     0   \n",
       "\n",
       "        NumberOfDependents  \n",
       "7                    0.000  \n",
       "9                      NaN  \n",
       "17                   0.000  \n",
       "33                   0.000  \n",
       "42                   0.000  \n",
       "...                    ...  \n",
       "149977               0.000  \n",
       "149978               0.000  \n",
       "149985               0.000  \n",
       "149993               3.000  \n",
       "149998               0.000  \n",
       "\n",
       "[29731 rows x 11 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# взглянем на пропуски переменной MonthlyIncome\n",
    "data[data['MonthlyIncome'].isnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# заменяем пропуски и значения меньше 1200 в MonthlyIncome\n",
    "# минимально допустимым значением дохода\n",
    "data['MonthlyIncome'] = np.where((data['MonthlyIncome'].isnull()) | \n",
    "                                 (data['MonthlyIncome'] < 1200), \n",
    "                                 1200, \n",
    "                                 data['MonthlyIncome'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# значения переменной age меньше 18 заменяем\n",
    "# минимально допустимым значением возраста\n",
    "data['age'] = np.where(data['age'] < 18, 18, data['age'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# пропуски в переменной NumberOfDependents заменяем нулем, \n",
    "# а значения больше 4 выделяем в отдельную категорию\n",
    "data.loc[data['NumberOfDependents'].isnull(), 'NumberOfDependents'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# все значения переменной RevolvingUtilizationOfUnsecuredLines\n",
    "# больше 2 пометим как пропуски\n",
    "data['RevolvingUtilizationOfUnsecuredLines'] = np.where(\n",
    "    data['RevolvingUtilizationOfUnsecuredLines'] > 2, \n",
    "    np.NaN, \n",
    "    data['RevolvingUtilizationOfUnsecuredLines'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем переменную Ratio - отношение количества \n",
    "# просрочек 90+ к общему количеству просрочек\n",
    "sum_of_delinq = (data['NumberOfTimes90DaysLate'] + \n",
    "                 data['NumberOfTime30-59DaysPastDueNotWorse'] + \n",
    "                 data['NumberOfTime60-89DaysPastDueNotWorse'])\n",
    "\n",
    "cond = (data['NumberOfTimes90DaysLate'] == 0) | (sum_of_delinq == 0)\n",
    "data['Ratio'] = np.where(\n",
    "    cond, 0, data['NumberOfTimes90DaysLate'] / sum_of_delinq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем индикатор нулевых значений переменной \n",
    "# NumberOfOpenCreditLinesAndLoans\n",
    "data['NumberOfOpenCreditLinesAndLoans_is_0'] = np.where(\n",
    "    data['NumberOfOpenCreditLinesAndLoans'] == 0, 'T', 'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем индикатор нулевых значений переменной \n",
    "# NumberRealEstateLoansOrLines\n",
    "data['NumberRealEstateLoansOrLines_is_0'] = np.where(\n",
    "    data['NumberRealEstateLoansOrLines'] == 0, 'T', 'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем индикатор нулевых значений переменной \n",
    "# RevolvingUtilizationOfUnsecuredLines\n",
    "data['RevolvingUtilizationOfUnsecuredLines_is_0'] = np.where(\n",
    "    data['RevolvingUtilizationOfUnsecuredLines'] == 0, 'T', 'F')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NumberOfTime30-59DaysPastDueNotWorse:\n",
      "[ 2  0  1  3  4  5  7 10  6 98 12  8  9 96 13 11]\n",
      "\n",
      "NumberOfTime60-89DaysPastDueNotWorse:\n",
      "[ 0  1  2  5  3 98  4  6  7  8 96 11  9]\n",
      "\n",
      "NumberOfTimes90DaysLate:\n",
      "[ 0  1  3  2  5  4 98 10  9  6  7  8 15 96 11 13 14 17 12]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# смотрим уникальные значения целочисленных переменных, \n",
    "# характеризующих глубину просрочки\n",
    "for col in ['NumberOfTime30-59DaysPastDueNotWorse', \n",
    "            'NumberOfTime60-89DaysPastDueNotWorse',\n",
    "            'NumberOfTimes90DaysLate']:\n",
    "    print(f\"{col}:\\n{data[col].unique()}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cat_0    126018\n",
      "cat_1     16033\n",
      "cat_2      4598\n",
      "cat_3      1754\n",
      "cat_4      1597\n",
      "Name: NumberOfTime30-59DaysPastDueNotWorse, dtype: int64\n",
      "\n",
      "cat_0    142396\n",
      "cat_1      5731\n",
      "cat_2      1118\n",
      "cat_4       437\n",
      "cat_3       318\n",
      "Name: NumberOfTime60-89DaysPastDueNotWorse, dtype: int64\n",
      "\n",
      "cat_0    141662\n",
      "cat_1      5243\n",
      "cat_2      1555\n",
      "cat_4       873\n",
      "cat_3       667\n",
      "Name: NumberOfTimes90DaysLate, dtype: int64\n",
      "\n",
      "cat_0.0    90826\n",
      "cat_1.0    26316\n",
      "cat_2.0    19522\n",
      "cat_3.0     9483\n",
      "cat_4.0     3853\n",
      "Name: NumberOfDependents, dtype: int64\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# преобразовываем переменные в категориальные, применив\n",
    "# биннинг и перевод в единый строковый формат\n",
    "for col in ['NumberOfTime30-59DaysPastDueNotWorse', \n",
    "            'NumberOfTime60-89DaysPastDueNotWorse',\n",
    "            'NumberOfTimes90DaysLate',\n",
    "            'NumberOfDependents']:\n",
    "    data.loc[data[col] > 3, col] = 4\n",
    "    data[col] = data[col].apply(lambda x: f\"cat_{x}\")\n",
    "\n",
    "# смотрим частоты\n",
    "for col in ['NumberOfTime30-59DaysPastDueNotWorse', \n",
    "            'NumberOfTime60-89DaysPastDueNotWorse',\n",
    "            'NumberOfTimes90DaysLate',\n",
    "            'NumberOfDependents']:\n",
    "    print(data[col].value_counts())\n",
    "    print('')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# пишем функцию, которая создает взаимодействие \n",
    "# в результате конъюнкции переменных \n",
    "# f1 и f2\n",
    "def make_interact(df, interact_list):\n",
    "    for i in lst:\n",
    "        f1 = i[0]\n",
    "        f2 = i[1]\n",
    "        df[f1 + ' + ' + f2 + '_interact'] = (df[f1].astype(str) + ' + ' \n",
    "                                             + df[f2].astype(str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SeriousDlqin2yrs</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines</th>\n",
       "      <th>age</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse</th>\n",
       "      <th>DebtRatio</th>\n",
       "      <th>MonthlyIncome</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans</th>\n",
       "      <th>NumberOfTimes90DaysLate</th>\n",
       "      <th>NumberRealEstateLoansOrLines</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse</th>\n",
       "      <th>NumberOfDependents</th>\n",
       "      <th>Ratio</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans_is_0</th>\n",
       "      <th>NumberRealEstateLoansOrLines_is_0</th>\n",
       "      <th>RevolvingUtilizationOfUnsecuredLines_is_0</th>\n",
       "      <th>NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact</th>\n",
       "      <th>NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact</th>\n",
       "      <th>NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact</th>\n",
       "      <th>NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact</th>\n",
       "      <th>NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.766</td>\n",
       "      <td>45</td>\n",
       "      <td>cat_2</td>\n",
       "      <td>0.803</td>\n",
       "      <td>9120.000</td>\n",
       "      <td>13</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>6</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_2.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_2.0 + cat_2</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_2 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0.957</td>\n",
       "      <td>40</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.122</td>\n",
       "      <td>2600.000</td>\n",
       "      <td>4</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_1.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>T</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_1.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>T + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.658</td>\n",
       "      <td>38</td>\n",
       "      <td>cat_1</td>\n",
       "      <td>0.085</td>\n",
       "      <td>3042.000</td>\n",
       "      <td>2</td>\n",
       "      <td>cat_1</td>\n",
       "      <td>0</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.500</td>\n",
       "      <td>F</td>\n",
       "      <td>T</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_1</td>\n",
       "      <td>cat_0 + cat_1</td>\n",
       "      <td>cat_1 + cat_0</td>\n",
       "      <td>T + cat_1</td>\n",
       "      <td>F + cat_1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0.234</td>\n",
       "      <td>30</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.036</td>\n",
       "      <td>3300.000</td>\n",
       "      <td>5</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>T</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>T + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0.907</td>\n",
       "      <td>49</td>\n",
       "      <td>cat_1</td>\n",
       "      <td>0.025</td>\n",
       "      <td>63588.000</td>\n",
       "      <td>7</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>1</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_1</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_1 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149996</th>\n",
       "      <td>0</td>\n",
       "      <td>0.041</td>\n",
       "      <td>74</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.225</td>\n",
       "      <td>2100.000</td>\n",
       "      <td>4</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>1</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149997</th>\n",
       "      <td>0</td>\n",
       "      <td>0.300</td>\n",
       "      <td>44</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.717</td>\n",
       "      <td>5584.000</td>\n",
       "      <td>4</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>1</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_2.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_2.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149998</th>\n",
       "      <td>0</td>\n",
       "      <td>0.246</td>\n",
       "      <td>58</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>3870.000</td>\n",
       "      <td>1200.000</td>\n",
       "      <td>18</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>1</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149999</th>\n",
       "      <td>0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>30</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>5716.000</td>\n",
       "      <td>4</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>T</td>\n",
       "      <td>T</td>\n",
       "      <td>cat_0.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>T + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>150000</th>\n",
       "      <td>0</td>\n",
       "      <td>0.850</td>\n",
       "      <td>64</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>0.250</td>\n",
       "      <td>8158.000</td>\n",
       "      <td>8</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>2</td>\n",
       "      <td>cat_0</td>\n",
       "      <td>cat_0.0</td>\n",
       "      <td>0.000</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>F</td>\n",
       "      <td>cat_0.0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>cat_0 + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "      <td>F + cat_0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150000 rows × 20 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        SeriousDlqin2yrs  RevolvingUtilizationOfUnsecuredLines  age  \\\n",
       "1                      1                                 0.766   45   \n",
       "2                      0                                 0.957   40   \n",
       "3                      0                                 0.658   38   \n",
       "4                      0                                 0.234   30   \n",
       "5                      0                                 0.907   49   \n",
       "...                  ...                                   ...  ...   \n",
       "149996                 0                                 0.041   74   \n",
       "149997                 0                                 0.300   44   \n",
       "149998                 0                                 0.246   58   \n",
       "149999                 0                                 0.000   30   \n",
       "150000                 0                                 0.850   64   \n",
       "\n",
       "       NumberOfTime30-59DaysPastDueNotWorse  DebtRatio  MonthlyIncome  \\\n",
       "1                                     cat_2      0.803       9120.000   \n",
       "2                                     cat_0      0.122       2600.000   \n",
       "3                                     cat_1      0.085       3042.000   \n",
       "4                                     cat_0      0.036       3300.000   \n",
       "5                                     cat_1      0.025      63588.000   \n",
       "...                                     ...        ...            ...   \n",
       "149996                                cat_0      0.225       2100.000   \n",
       "149997                                cat_0      0.717       5584.000   \n",
       "149998                                cat_0   3870.000       1200.000   \n",
       "149999                                cat_0      0.000       5716.000   \n",
       "150000                                cat_0      0.250       8158.000   \n",
       "\n",
       "        NumberOfOpenCreditLinesAndLoans NumberOfTimes90DaysLate  \\\n",
       "1                                    13                   cat_0   \n",
       "2                                     4                   cat_0   \n",
       "3                                     2                   cat_1   \n",
       "4                                     5                   cat_0   \n",
       "5                                     7                   cat_0   \n",
       "...                                 ...                     ...   \n",
       "149996                                4                   cat_0   \n",
       "149997                                4                   cat_0   \n",
       "149998                               18                   cat_0   \n",
       "149999                                4                   cat_0   \n",
       "150000                                8                   cat_0   \n",
       "\n",
       "        NumberRealEstateLoansOrLines NumberOfTime60-89DaysPastDueNotWorse  \\\n",
       "1                                  6                                cat_0   \n",
       "2                                  0                                cat_0   \n",
       "3                                  0                                cat_0   \n",
       "4                                  0                                cat_0   \n",
       "5                                  1                                cat_0   \n",
       "...                              ...                                  ...   \n",
       "149996                             1                                cat_0   \n",
       "149997                             1                                cat_0   \n",
       "149998                             1                                cat_0   \n",
       "149999                             0                                cat_0   \n",
       "150000                             2                                cat_0   \n",
       "\n",
       "       NumberOfDependents  Ratio NumberOfOpenCreditLinesAndLoans_is_0  \\\n",
       "1                 cat_2.0  0.000                                    F   \n",
       "2                 cat_1.0  0.000                                    F   \n",
       "3                 cat_0.0  0.500                                    F   \n",
       "4                 cat_0.0  0.000                                    F   \n",
       "5                 cat_0.0  0.000                                    F   \n",
       "...                   ...    ...                                  ...   \n",
       "149996            cat_0.0  0.000                                    F   \n",
       "149997            cat_2.0  0.000                                    F   \n",
       "149998            cat_0.0  0.000                                    F   \n",
       "149999            cat_0.0  0.000                                    F   \n",
       "150000            cat_0.0  0.000                                    F   \n",
       "\n",
       "       NumberRealEstateLoansOrLines_is_0  \\\n",
       "1                                      F   \n",
       "2                                      T   \n",
       "3                                      T   \n",
       "4                                      T   \n",
       "5                                      F   \n",
       "...                                  ...   \n",
       "149996                                 F   \n",
       "149997                                 F   \n",
       "149998                                 F   \n",
       "149999                                 T   \n",
       "150000                                 F   \n",
       "\n",
       "       RevolvingUtilizationOfUnsecuredLines_is_0  \\\n",
       "1                                              F   \n",
       "2                                              F   \n",
       "3                                              F   \n",
       "4                                              F   \n",
       "5                                              F   \n",
       "...                                          ...   \n",
       "149996                                         F   \n",
       "149997                                         F   \n",
       "149998                                         F   \n",
       "149999                                         T   \n",
       "150000                                         F   \n",
       "\n",
       "       NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact  \\\n",
       "1                                         cat_2.0 + cat_2                   \n",
       "2                                         cat_1.0 + cat_0                   \n",
       "3                                         cat_0.0 + cat_1                   \n",
       "4                                         cat_0.0 + cat_0                   \n",
       "5                                         cat_0.0 + cat_1                   \n",
       "...                                                   ...                   \n",
       "149996                                    cat_0.0 + cat_0                   \n",
       "149997                                    cat_2.0 + cat_0                   \n",
       "149998                                    cat_0.0 + cat_0                   \n",
       "149999                                    cat_0.0 + cat_0                   \n",
       "150000                                    cat_0.0 + cat_0                   \n",
       "\n",
       "       NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact  \\\n",
       "1                                           cat_0 + cat_0                        \n",
       "2                                           cat_0 + cat_0                        \n",
       "3                                           cat_0 + cat_1                        \n",
       "4                                           cat_0 + cat_0                        \n",
       "5                                           cat_0 + cat_0                        \n",
       "...                                                   ...                        \n",
       "149996                                      cat_0 + cat_0                        \n",
       "149997                                      cat_0 + cat_0                        \n",
       "149998                                      cat_0 + cat_0                        \n",
       "149999                                      cat_0 + cat_0                        \n",
       "150000                                      cat_0 + cat_0                        \n",
       "\n",
       "       NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact  \\\n",
       "1                                           cat_2 + cat_0                                     \n",
       "2                                           cat_0 + cat_0                                     \n",
       "3                                           cat_1 + cat_0                                     \n",
       "4                                           cat_0 + cat_0                                     \n",
       "5                                           cat_1 + cat_0                                     \n",
       "...                                                   ...                                     \n",
       "149996                                      cat_0 + cat_0                                     \n",
       "149997                                      cat_0 + cat_0                                     \n",
       "149998                                      cat_0 + cat_0                                     \n",
       "149999                                      cat_0 + cat_0                                     \n",
       "150000                                      cat_0 + cat_0                                     \n",
       "\n",
       "       NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact  \\\n",
       "1                                               F + cat_0                     \n",
       "2                                               T + cat_0                     \n",
       "3                                               T + cat_1                     \n",
       "4                                               T + cat_0                     \n",
       "5                                               F + cat_0                     \n",
       "...                                                   ...                     \n",
       "149996                                          F + cat_0                     \n",
       "149997                                          F + cat_0                     \n",
       "149998                                          F + cat_0                     \n",
       "149999                                          T + cat_0                     \n",
       "150000                                          F + cat_0                     \n",
       "\n",
       "       NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact  \n",
       "1                                               F + cat_0                       \n",
       "2                                               F + cat_0                       \n",
       "3                                               F + cat_1                       \n",
       "4                                               F + cat_0                       \n",
       "5                                               F + cat_0                       \n",
       "...                                                   ...                       \n",
       "149996                                          F + cat_0                       \n",
       "149997                                          F + cat_0                       \n",
       "149998                                          F + cat_0                       \n",
       "149999                                          F + cat_0                       \n",
       "150000                                          F + cat_0                       \n",
       "\n",
       "[150000 rows x 20 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем список списков - список 2-факторных взаимодействий\n",
    "lst = [\n",
    "    ['NumberOfDependents', \n",
    "     'NumberOfTime30-59DaysPastDueNotWorse'],\n",
    "    ['NumberOfTime60-89DaysPastDueNotWorse', \n",
    "     'NumberOfTimes90DaysLate'],\n",
    "    ['NumberOfTime30-59DaysPastDueNotWorse', \n",
    "     'NumberOfTime60-89DaysPastDueNotWorse'],\n",
    "    ['NumberRealEstateLoansOrLines_is_0', \n",
    "     'NumberOfTimes90DaysLate'],\n",
    "    ['NumberOfOpenCreditLinesAndLoans_is_0', \n",
    "     'NumberOfTimes90DaysLate']\n",
    "]\n",
    "\n",
    "# создаем взаимодействия\n",
    "make_interact(data, interact_list=lst)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем обучающий массив предикторов, тестовый массив предикторов,\n",
    "# обучающий массив меток, тестовый массив меток\n",
    "train, test, y_train, y_test = train_test_split(\n",
    "    data.drop('SeriousDlqin2yrs', axis=1), \n",
    "    data['SeriousDlqin2yrs'], \n",
    "    test_size=.3, \n",
    "    stratify=data['SeriousDlqin2yrs'], \n",
    "    random_state=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем обучающий и тестовый наборы для h2o\n",
    "h2o_train, h2o_test, _, _ = train_test_split(\n",
    "    data, data, test_size=.3, \n",
    "    stratify=data['SeriousDlqin2yrs'], \n",
    "    random_state=100)\n",
    "\n",
    "# записываем наборы в CSV-файлы\n",
    "h2o_train.to_csv('h2o_train.csv', sep=';', index=False)\n",
    "h2o_test.to_csv('h2o_test.csv', sep=';', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# пишем функцию, вычисляющую AUC для модели с одним предиктором\n",
    "def importance_auc(train, y_train, imp_strategy):\n",
    "    # создаем копию обучающего набора\n",
    "    train_copy = train.copy()\n",
    "    # создаем список переменных\n",
    "    col_list = train_copy.select_dtypes(include=['number']).columns  \n",
    "    # создаем список, в который будем записывать auc\n",
    "    auc_list = []\n",
    "    for i in col_list:\n",
    "        pipe = Pipeline([\n",
    "            ('imputer', SimpleImputer(strategy=imp_strategy)),\n",
    "            ('scaler', StandardScaler()), \n",
    "            ('logreg', LogisticRegression(solver='liblinear'))\n",
    "        ])     \n",
    "        auc = cross_val_score(pipe, \n",
    "                              train_copy[[i]], \n",
    "                              y_train, \n",
    "                              scoring='roc_auc', \n",
    "                              cv=5).mean()\n",
    "        auc_list.append(auc)\n",
    "    \n",
    "    result = pd.DataFrame({'Переменная': col_list,\n",
    "                           'AUC': auc_list})\n",
    "    return np.round(result.sort_values(\n",
    "        by='AUC', ascending=False), 3) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Переменная</th>\n",
       "      <th>AUC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>RevolvingUtilizationOfUnsecuredLines</td>\n",
       "      <td>0.779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Ratio</td>\n",
       "      <td>0.652</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>age</td>\n",
       "      <td>0.634</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NumberOfOpenCreditLinesAndLoans</td>\n",
       "      <td>0.540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>NumberRealEstateLoansOrLines</td>\n",
       "      <td>0.535</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MonthlyIncome</td>\n",
       "      <td>0.532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DebtRatio</td>\n",
       "      <td>0.475</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             Переменная   AUC\n",
       "0  RevolvingUtilizationOfUnsecuredLines 0.779\n",
       "6                                 Ratio 0.652\n",
       "1                                   age 0.634\n",
       "4       NumberOfOpenCreditLinesAndLoans 0.540\n",
       "5          NumberRealEstateLoansOrLines 0.535\n",
       "3                         MonthlyIncome 0.532\n",
       "2                             DebtRatio 0.475"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# применяем функцию importance_auc()\n",
    "importance_auc(train, y_train, 'median')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем импутацию пропусков в переменной \n",
    "# RevolvingUtilizationOfUnsecuredLines средним\n",
    "train['RevolvingUtilizationOfUnsecuredLines'].fillna(\n",
    "    train['RevolvingUtilizationOfUnsecuredLines'].mean(), inplace=True)\n",
    "test['RevolvingUtilizationOfUnsecuredLines'].fillna(\n",
    "    train['RevolvingUtilizationOfUnsecuredLines'].mean(), inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем логарифмическое преобразование \n",
    "# переменной MonthlyIncome\n",
    "train['MonthlyIncome'] = np.log(train['MonthlyIncome'].clip(0.01))\n",
    "test['MonthlyIncome'] = np.log(test['MonthlyIncome'].clip(0.01))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count   105000.000\n",
       "mean       353.037\n",
       "std       2051.316\n",
       "min          0.000\n",
       "25%          0.175\n",
       "50%          0.367\n",
       "75%          0.870\n",
       "max     329664.000\n",
       "Name: DebtRatio, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# выводим описательные статистики\n",
    "train['DebtRatio'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71.64095238095238"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# допустим, мы знаем, что не выдаем кредиты с коэффициентом \n",
    "# долговой нагрузки больше 0.7, найдем соответствующий этому\n",
    "# значению процентиль\n",
    "stats.percentileofscore(train['DebtRatio'], 0.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6999722155381308"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# проверим, какому числу соответствует этот процентиль\n",
    "np.percentile(train['DebtRatio'], 71.641)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/numpy/lib/function_base.py:4650: UserWarning: Warning: 'partition' will ignore the 'mask' of the MaskedArray.\n",
      "  arr.partition(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "count   105000.000\n",
       "mean         0.390\n",
       "std          0.250\n",
       "min          0.000\n",
       "25%          0.175\n",
       "50%          0.367\n",
       "75%          0.700\n",
       "max          0.700\n",
       "dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# создаем копию серии\n",
    "debt_ratio = train['DebtRatio'].copy()\n",
    "\n",
    "# выполняем винзоризацию\n",
    "debt_ratio = stats.mstats.winsorize(debt_ratio, \n",
    "                                    limits=(0, 0.28359))\n",
    "# выводим описательные статистики\n",
    "pd.Series(debt_ratio).describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# создаем собственный класс, выполняющий винзоризацию\n",
    "class OutlierRemover():\n",
    "    \"\"\"\n",
    "    Параметры:\n",
    "    lower_quantile: float, по умолчанию 0.25\n",
    "        Нижний квантиль.\n",
    "    upper_quantile: float, по умолчанию 0.75\n",
    "        Верхний квантиль.\n",
    "    k: float, по умолчанию 1.5\n",
    "        Коэффициент.\n",
    "    copy: bool, по умолчанию True\n",
    "        Возвращает копию.\n",
    "    \"\"\"\n",
    "    def __init__(self, copy=True, lower_quantile=0.25, \n",
    "                 upper_quantile=0.75, k=1.5):\n",
    "        # все параметры для инициализации публичных атрибутов \n",
    "        # должны быть заданы в методе __init__\n",
    "        \n",
    "        # публичные атрибуты\n",
    "        self.copy = copy\n",
    "        self.lower_quantile = lower_quantile\n",
    "        self.upper_quantile = upper_quantile\n",
    "        self.k = k\n",
    "        \n",
    "    def __is_numpy(self, X):\n",
    "        # частный метод, который с помощью функции isinstance()\n",
    "        # проверяет, является ли наш объект массивом NumPy\n",
    "        return isinstance(X, np.ndarray)\n",
    "                \n",
    "    def fit(self, X, y=None):\n",
    "        # fit должен принимать в качестве аргументов X и y\n",
    "        \n",
    "        # обучение модели осуществляется прямо здесь\n",
    "        # создаем пустой словарь, в котором ключами\n",
    "        # будут имена/целые числа, а значениями - кортежами\n",
    "        self._dict = {}\n",
    "        \n",
    "        # если 1D-массив, то переводим в 2D\n",
    "        if len(X.shape) == 1:\n",
    "            X = X.reshape(-1, 1)\n",
    "            \n",
    "        # записываем количество столбцов\n",
    "        ncols = X.shape[1]\n",
    "        \n",
    "        # записываем результат __is_numpy()\n",
    "        is_np = self.__is_numpy(X)\n",
    "        \n",
    "        # если объект - массив NumPy, выполняем следующие действия:\n",
    "        if is_np:\n",
    "            # по каждому столбцу массива NumPy\n",
    "            for col in range(ncols):\n",
    "                lower = np.quantile(X[:, col], self.lower_quantile)\n",
    "                upper = np.quantile(X[:, col], self.upper_quantile)\n",
    "                IQR = (upper - lower) * self.k\n",
    "                self._dict[col] = (lower, upper, IQR)\n",
    "        # в противном случае, т.е. если объект - датафрейм pandas,\n",
    "        # выполняем следующие действия:\n",
    "        else:\n",
    "            # по каждому столбцу датафрейма pandas\n",
    "            for col in X.columns:\n",
    "                # вычисляем и записываем в словарь\n",
    "                lower = X[col].quantile(self.lower_quantile)\n",
    "                upper = X[col].quantile(self.upper_quantile)\n",
    "                IQR = (upper - lower) * self.k\n",
    "                self._dict[col] = (lower, upper, IQR)\n",
    "\n",
    "        # fit возвращает self\n",
    "        return self\n",
    "    \n",
    "    def transform(self, X):\n",
    "        # transform принимает в качестве аргумента только X\n",
    "        \n",
    "        # выполняем копирование массива во избежание \n",
    "        # предупреждения SettingWithCopyWarning\n",
    "        # \"A value is trying to be set on a copy of \n",
    "        # a slice from a DataFrame (Происходит попытка изменить \n",
    "        # значение в копии среза данных датафрейма)\"\n",
    "        if self.copy:\n",
    "            X = X.copy()\n",
    "        \n",
    "        # если 1D-массив, то переводим в 2D\n",
    "        if len(X.shape) == 1:\n",
    "            X = X.reshape(-1, 1)\n",
    "            \n",
    "        # записываем количество столбцов\n",
    "        ncols = X.shape[1]\n",
    "        \n",
    "        # записываем результат __is_numpy()\n",
    "        is_np = self.__is_numpy(X)\n",
    "        \n",
    "        # применяем преобразование к X\n",
    "        # если объект - массив NumPy, выполняем следующие действия:\n",
    "        if is_np:\n",
    "            # по каждому столбцу массива NumPy\n",
    "            for col in range(ncols):\n",
    "                # заменяем\n",
    "                X[:, col] = np.where(\n",
    "                    X[:, col] < (self._dict[col][0] - self._dict[col][2]), \n",
    "                    self._dict[col][0] - self._dict[col][2], \n",
    "                    X[:, col])\n",
    "                X[:, col] = np.where(\n",
    "                    X[:, col] >= (self._dict[col][1] + self._dict[col][2]), \n",
    "                    self._dict[col][1] + self._dict[col][2], \n",
    "                    X[:, col])\n",
    "                \n",
    "        # в противном случае, т.е. если объект - датафрейм pandas,\n",
    "        # выполняем следующие действия:\n",
    "        else:\n",
    "            # по каждому столбцу датафрейма pandas\n",
    "            for col in X.columns:\n",
    "                # заменяем\n",
    "                X[col] = np.where(\n",
    "                    X[col] < (self._dict[col][0] - self._dict[col][2]), \n",
    "                    self._dict[col][0] - self._dict[col][2], \n",
    "                    X[col])\n",
    "                X[col] = np.where(\n",
    "                    X[col] >= (self._dict[col][1] + self._dict[col][2]), \n",
    "                    self._dict[col][1] + self._dict[col][2], \n",
    "                    X[col]) \n",
    "        # transform возвращает X\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем винзоризацию переменной DebtRatio\n",
    "# с помощью нашего класса OutlierRemover\n",
    "rem = OutlierRemover(lower_quantile=0, \n",
    "                     upper_quantile=0.8,\n",
    "                     k=1.15)\n",
    "rem.fit(train[['DebtRatio']])\n",
    "train['DebtRatio'] = rem.transform(train[['DebtRatio']])\n",
    "test['DebtRatio'] = rem.transform(test[['DebtRatio']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем биннинг переменных DebtRatio и \n",
    "# RevolvingUtilizationOfUnsecuredLines\n",
    "bins = np.round(np.arange(0.0, 1.05, 0.05), 2).tolist()\n",
    "bins[0] = -1\n",
    "for col in ['DebtRatio', 'RevolvingUtilizationOfUnsecuredLines']:\n",
    "    train[col+'_bins'] = pd.cut(\n",
    "        train[col], bins, labels=bins[1:]).astype('float')\n",
    "    train.loc[(train[col] > 1), col+'_bins'] = 2\n",
    "    test[col+'_bins'] = pd.cut(\n",
    "        test[col], bins, labels=bins[1:]).astype('float')\n",
    "    test.loc[(test[col] > 1), col+'_bins'] = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# преобразовываем DebtRatio_bins в категориальную переменную\n",
    "train['DebtRatio_bins'] = train['DebtRatio_bins'].apply(lambda x: f\"cat_{x}\")\n",
    "test['DebtRatio_bins'] = test['DebtRatio_bins'].apply(lambda x: f\"cat_{x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем стандартизацию\n",
    "num_cols = train.select_dtypes(include=['number']).columns.tolist()\n",
    "train_copy = train.copy()\n",
    "for col in num_cols:\n",
    "    train[col] = (train[col] - train[col].mean()) / train[col].std()\n",
    "    test[col] = (test[col] - train_copy[col].mean()) / train_copy[col].std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# проверяем наличие пропусков\n",
    "print(train.isnull().sum().sum())\n",
    "print(test.isnull().sum().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# выполняем дамми-кодирование\n",
    "X_train = pd.get_dummies(train)\n",
    "X_test = pd.get_dummies(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150 150\n"
     ]
    }
   ],
   "source": [
    "# проверяем количество признаков\n",
    "print(X_train.shape[1], X_test.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUC-ROC на обучающей выборке: 0.864\n",
      "AUC-ROC на тестовой выборке: 0.864\n",
      "CPU times: user 1.78 s, sys: 263 ms, total: 2.04 s\n",
      "Wall time: 1.37 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# строим модель логистической регрессии на основе\n",
    "# класса LogisticRegression\n",
    "logreg = LogisticRegression(solver='liblinear').fit(X_train, y_train)\n",
    "print(\"AUC-ROC на обучающей выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_train, logreg.predict_proba(X_train)[:, 1])))\n",
    "print(\"AUC-ROC на тестовой выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_test, logreg.predict_proba(X_test)[:, 1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUC-ROC на обучающей выборке: 0.864\n",
      "AUC-ROC на тестовой выборке: 0.864\n",
      "CPU times: user 2.9 s, sys: 767 ms, total: 3.66 s\n",
      "Wall time: 469 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# строим модель логистической регрессии на основе собственного \n",
    "# класса RegularizedLogisticRegression_Newton\n",
    "custom_logreg = RegularizedLogisticRegression_Newton(\n",
    "    lambda_=0.001, max_iter=2)\n",
    "custom_logreg.fit(X_train, y_train)\n",
    "print(\"AUC-ROC на обучающей выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_train, custom_logreg.predict_proba(X_train))))\n",
    "print(\"AUC-ROC на тестовой выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_test, custom_logreg.predict_proba(X_test))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Константа: -0.224\n",
      "Регрессионные коэффициенты:\n",
      "RevolvingUtilizationOfUnsecuredLines 0.441\n",
      "age -0.235\n",
      "DebtRatio -0.301\n",
      "MonthlyIncome -0.044\n",
      "NumberOfOpenCreditLinesAndLoans 0.166\n",
      "NumberRealEstateLoansOrLines 0.154\n",
      "Ratio 0.274\n",
      "RevolvingUtilizationOfUnsecuredLines_bins 0.158\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_0 -0.462\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_1 -0.008\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_2 -0.003\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_3 0.128\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_4 0.121\n",
      "NumberOfTimes90DaysLate_cat_0 -0.068\n",
      "NumberOfTimes90DaysLate_cat_1 -0.156\n",
      "NumberOfTimes90DaysLate_cat_2 -0.083\n",
      "NumberOfTimes90DaysLate_cat_3 0.048\n",
      "NumberOfTimes90DaysLate_cat_4 0.035\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_0 -0.444\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_1 -0.199\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_2 0.025\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_3 0.227\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_4 0.167\n",
      "NumberOfDependents_cat_0.0 -0.083\n",
      "NumberOfDependents_cat_1.0 -0.079\n",
      "NumberOfDependents_cat_2.0 -0.097\n",
      "NumberOfDependents_cat_3.0 0.01\n",
      "NumberOfDependents_cat_4.0 0.025\n",
      "NumberOfOpenCreditLinesAndLoans_is_0_F -0.192\n",
      "NumberOfOpenCreditLinesAndLoans_is_0_T -0.032\n",
      "NumberRealEstateLoansOrLines_is_0_F -0.195\n",
      "NumberRealEstateLoansOrLines_is_0_T -0.029\n",
      "RevolvingUtilizationOfUnsecuredLines_is_0_F -0.25\n",
      "RevolvingUtilizationOfUnsecuredLines_is_0_T 0.026\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_0 -0.203\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_1 -0.01\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_2 0.117\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_3 -0.007\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_4 0.02\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_0 -0.074\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_1 -0.072\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_2 -0.035\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_3 0.141\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_4 -0.039\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_0 -0.039\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_1 -0.026\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_2 0.04\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_3 -0.246\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_4 0.174\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_0 -0.057\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_1 -0.037\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_2 -0.144\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_3 -0.037\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_4 0.285\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_0 -0.089\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_1 0.138\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_2 0.018\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_3 0.277\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_4 -0.319\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_0 -0.452\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_1 -0.308\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_2 -0.105\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_3 0.208\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_4 0.214\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_0 0.088\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_1 0.079\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_2 0.028\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_3 -0.211\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_4 -0.183\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_0 0.137\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_1 0.196\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_2 -0.162\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_3 -0.132\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_4 -0.014\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_0 -0.129\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_1 -0.347\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_2 -0.201\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_3 0.377\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_4 0.527\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_0 0.289\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_1 0.224\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_2 0.357\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_3 -0.194\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_4 -0.509\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_0 -0.846\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_1 -0.044\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_2 0.046\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_3 0.492\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_4 -0.11\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_0 -0.426\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_1 -0.307\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_2 0.04\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_3 0.45\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_4 0.234\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_0 0.059\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_1 -0.084\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_2 -0.271\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_3 -0.055\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_4 0.348\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_0 0.213\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_1 0.1\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_2 0.147\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_3 -0.214\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_4 -0.118\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_0 0.556\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_1 0.137\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_2 0.063\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_3 -0.447\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_4 -0.188\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0 -0.208\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1 -0.065\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2 -0.006\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3 0.085\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4 -0.001\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0 0.14\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1 -0.09\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2 -0.077\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3 -0.037\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4 0.036\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0 -0.269\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1 -0.018\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2 0.091\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3 -0.117\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4 0.122\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0 0.201\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1 -0.137\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2 -0.174\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3 0.164\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4 -0.087\n",
      "DebtRatio_bins_cat_0.05 -0.417\n",
      "DebtRatio_bins_cat_0.1 -0.294\n",
      "DebtRatio_bins_cat_0.15 -0.36\n",
      "DebtRatio_bins_cat_0.2 -0.329\n",
      "DebtRatio_bins_cat_0.25 -0.376\n",
      "DebtRatio_bins_cat_0.3 -0.368\n",
      "DebtRatio_bins_cat_0.35 -0.299\n",
      "DebtRatio_bins_cat_0.4 -0.169\n",
      "DebtRatio_bins_cat_0.45 0.014\n",
      "DebtRatio_bins_cat_0.5 -0.192\n",
      "DebtRatio_bins_cat_0.55 -0.05\n",
      "DebtRatio_bins_cat_0.6 0.141\n",
      "DebtRatio_bins_cat_0.65 0.05\n",
      "DebtRatio_bins_cat_0.7 0.189\n",
      "DebtRatio_bins_cat_0.75 0.338\n",
      "DebtRatio_bins_cat_0.8 0.223\n",
      "DebtRatio_bins_cat_0.85 0.383\n",
      "DebtRatio_bins_cat_0.9 0.252\n",
      "DebtRatio_bins_cat_0.95 0.202\n",
      "DebtRatio_bins_cat_1.0 0.348\n",
      "DebtRatio_bins_cat_2.0 0.488\n"
     ]
    }
   ],
   "source": [
    "# запишем коэффициенты, предварительно округлив до 3-го знака, \n",
    "# и названия предикторов в отдельные объекты\n",
    "coef = np.round(logreg.coef_, 3)\n",
    "feat_labels = X_train.columns\n",
    "\n",
    "# запишем свободный член (константу) в отдельный объект, при этом\n",
    "# переводим массив, полученный с помощью атрибута intercept_, \n",
    "# в скаляр с округлением до 3-го знака\n",
    "intercept = np.round(logreg.intercept_.item(), 3)\n",
    "\n",
    "# печатаем название \"Константа\"    \n",
    "print(\"Константа:\", intercept)\n",
    "# печатаем название \"Регрессионные коэффициенты\"\n",
    "print(\"Регрессионные коэффициенты:\")\n",
    "# для удобства сопоставим каждому названию \n",
    "# предиктора соответствующий коэффициент\n",
    "for c, feature in zip(coef[0], feat_labels):\n",
    "    print(feature, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Экспоненциальные коэффициенты:\n",
      "RevolvingUtilizationOfUnsecuredLines 1.554\n",
      "age 0.791\n",
      "DebtRatio 0.74\n",
      "MonthlyIncome 0.957\n",
      "NumberOfOpenCreditLinesAndLoans 1.181\n",
      "NumberRealEstateLoansOrLines 1.166\n",
      "Ratio 1.315\n",
      "RevolvingUtilizationOfUnsecuredLines_bins 1.171\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_0 0.63\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_1 0.992\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_2 0.997\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_3 1.137\n",
      "NumberOfTime30-59DaysPastDueNotWorse_cat_4 1.129\n",
      "NumberOfTimes90DaysLate_cat_0 0.934\n",
      "NumberOfTimes90DaysLate_cat_1 0.856\n",
      "NumberOfTimes90DaysLate_cat_2 0.92\n",
      "NumberOfTimes90DaysLate_cat_3 1.049\n",
      "NumberOfTimes90DaysLate_cat_4 1.036\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_0 0.641\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_1 0.82\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_2 1.025\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_3 1.255\n",
      "NumberOfTime60-89DaysPastDueNotWorse_cat_4 1.182\n",
      "NumberOfDependents_cat_0.0 0.92\n",
      "NumberOfDependents_cat_1.0 0.924\n",
      "NumberOfDependents_cat_2.0 0.908\n",
      "NumberOfDependents_cat_3.0 1.01\n",
      "NumberOfDependents_cat_4.0 1.025\n",
      "NumberOfOpenCreditLinesAndLoans_is_0_F 0.825\n",
      "NumberOfOpenCreditLinesAndLoans_is_0_T 0.969\n",
      "NumberRealEstateLoansOrLines_is_0_F 0.823\n",
      "NumberRealEstateLoansOrLines_is_0_T 0.971\n",
      "RevolvingUtilizationOfUnsecuredLines_is_0_F 0.779\n",
      "RevolvingUtilizationOfUnsecuredLines_is_0_T 1.026\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_0 0.816\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_1 0.99\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_2 1.124\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_3 0.993\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_4 1.02\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_0 0.929\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_1 0.931\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_2 0.966\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_3 1.151\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_4 0.962\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_0 0.962\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_1 0.974\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_2 1.041\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_3 0.782\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_4 1.19\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_0 0.945\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_1 0.964\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_2 0.866\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_3 0.964\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_4 1.33\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_0 0.915\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_1 1.148\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_2 1.018\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_3 1.319\n",
      "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_4 0.727\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_0 0.636\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_1 0.735\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_2 0.9\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_3 1.231\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_4 1.239\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_0 1.092\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_1 1.082\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_2 1.028\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_3 0.81\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_4 0.833\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_0 1.147\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_1 1.217\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_2 0.85\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_3 0.876\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_4 0.986\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_0 0.879\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_1 0.707\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_2 0.818\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_3 1.458\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_4 1.694\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_0 1.335\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_1 1.251\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_2 1.429\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_3 0.824\n",
      "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_4 0.601\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_0 0.429\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_1 0.957\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_2 1.047\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_3 1.636\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_4 0.896\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_0 0.653\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_1 0.736\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_2 1.041\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_3 1.568\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_4 1.264\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_0 1.061\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_1 0.919\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_2 0.763\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_3 0.946\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_4 1.416\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_0 1.237\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_1 1.105\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_2 1.158\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_3 0.807\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_4 0.889\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_0 1.744\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_1 1.147\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_2 1.065\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_3 0.64\n",
      "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_4 0.829\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0 0.812\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1 0.937\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2 0.994\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3 1.089\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4 0.999\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0 1.15\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1 0.914\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2 0.926\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3 0.964\n",
      "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4 1.037\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0 0.764\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1 0.982\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2 1.095\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3 0.89\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4 1.13\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0 1.223\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1 0.872\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2 0.84\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3 1.178\n",
      "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4 0.917\n",
      "DebtRatio_bins_cat_0.05 0.659\n",
      "DebtRatio_bins_cat_0.1 0.745\n",
      "DebtRatio_bins_cat_0.15 0.698\n",
      "DebtRatio_bins_cat_0.2 0.72\n",
      "DebtRatio_bins_cat_0.25 0.687\n",
      "DebtRatio_bins_cat_0.3 0.692\n",
      "DebtRatio_bins_cat_0.35 0.742\n",
      "DebtRatio_bins_cat_0.4 0.845\n",
      "DebtRatio_bins_cat_0.45 1.014\n",
      "DebtRatio_bins_cat_0.5 0.825\n",
      "DebtRatio_bins_cat_0.55 0.951\n",
      "DebtRatio_bins_cat_0.6 1.151\n",
      "DebtRatio_bins_cat_0.65 1.051\n",
      "DebtRatio_bins_cat_0.7 1.208\n",
      "DebtRatio_bins_cat_0.75 1.402\n",
      "DebtRatio_bins_cat_0.8 1.25\n",
      "DebtRatio_bins_cat_0.85 1.467\n",
      "DebtRatio_bins_cat_0.9 1.287\n",
      "DebtRatio_bins_cat_0.95 1.224\n",
      "DebtRatio_bins_cat_1.0 1.416\n",
      "DebtRatio_bins_cat_2.0 1.629\n"
     ]
    }
   ],
   "source": [
    "# вычислим экспоненциальные коэффициенты\n",
    "# и запишем их в отдельный объект\n",
    "exp_coef = np.round(np.exp(coef), 3)\n",
    "\n",
    "# печатаем название \"Экспоненциальные коэффициенты\"\n",
    "print(\"Экспоненциальные коэффициенты:\")\n",
    "# для удобства сопоставим каждому названию \n",
    "# предиктора соответствующий коэффициент\n",
    "for c, feature in zip(exp_coef[0], feat_labels):\n",
    "    print(feature, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "150"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# посмотрим, сколько у нас предикторов\n",
    "X_train.shape[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# импортируем функцию mutual_info_classif(), \n",
    "# вычисляющую взаимную информацию\n",
    "from sklearn.feature_selection import mutual_info_classif"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.03554945, 0.00691024, 0.0063855 , 0.00431225, 0.00299539,\n",
       "       0.00502602, 0.03543437, 0.03886811, 0.04024899, 0.00511791,\n",
       "       0.0073015 , 0.00448629, 0.00625128, 0.05134628, 0.01132081,\n",
       "       0.00746872, 0.00569454, 0.00682421, 0.04033447, 0.01069616,\n",
       "       0.00517347, 0.00195424, 0.00305554, 0.00961195, 0.00127681,\n",
       "       0.00123597, 0.00132315, 0.00098079, 0.02494812, 0.00242526,\n",
       "       0.01045547, 0.00457554, 0.02134913, 0.00150772, 0.01554666,\n",
       "       0.00308106, 0.00341671, 0.00202696, 0.00355244, 0.00085887,\n",
       "       0.00094575, 0.0014693 , 0.00230439, 0.        , 0.00107891,\n",
       "       0.00141593, 0.00078324, 0.001349  , 0.00166957, 0.00064534,\n",
       "       0.00054897, 0.        , 0.0005893 , 0.        , 0.00048398,\n",
       "       0.00009747, 0.00022171, 0.        , 0.        , 0.05611989,\n",
       "       0.00593722, 0.00350718, 0.00241599, 0.00244635, 0.00449697,\n",
       "       0.004168  , 0.00254333, 0.        , 0.0014999 , 0.00203501,\n",
       "       0.00039286, 0.00199841, 0.00099514, 0.        , 0.00074084,\n",
       "       0.00061903, 0.00032482, 0.00076403, 0.00040824, 0.00090263,\n",
       "       0.        , 0.00051753, 0.00115684, 0.0008667 , 0.04469336,\n",
       "       0.00387651, 0.00121287, 0.        , 0.00086125, 0.00296888,\n",
       "       0.00284324, 0.00136853, 0.00193087, 0.        , 0.00251689,\n",
       "       0.00209437, 0.        , 0.00074285, 0.00032231, 0.00230055,\n",
       "       0.00033287, 0.00113781, 0.00039416, 0.        , 0.0008171 ,\n",
       "       0.00256122, 0.00027553, 0.00110623, 0.0018853 , 0.01455033,\n",
       "       0.00636683, 0.00174254, 0.00071068, 0.00256246, 0.00350414,\n",
       "       0.00458082, 0.00597274, 0.00173829, 0.00453591, 0.04935872,\n",
       "       0.01057036, 0.00771199, 0.00344229, 0.00446772, 0.        ,\n",
       "       0.00096513, 0.00037889, 0.00042809, 0.00241315, 0.00104231,\n",
       "       0.        , 0.00038312, 0.        , 0.00013747, 0.        ,\n",
       "       0.        , 0.00035417, 0.00058031, 0.        , 0.        ,\n",
       "       0.        , 0.        , 0.00009103, 0.00024306, 0.0006497 ,\n",
       "       0.00015015, 0.00062691, 0.00054507, 0.00000302, 0.00122575])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# отключаем экспоненциальное представление\n",
    "np.set_printoptions(precision=None, suppress=True)\n",
    "\n",
    "# вычисляем взаимную информацию между предикторами \n",
    "# и зависимой переменной, функция возвращает \n",
    "# взаимную информацию для каждого предиктора,\n",
    "# чем меньше значение, тем меньше информации \n",
    "# о зависимой переменной несет наш предиктор\n",
    "mi = mutual_info_classif(X_train, y_train, random_state=0)\n",
    "mi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_0                0.056\n",
       "NumberOfTimes90DaysLate_cat_0                                                                        0.051\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0                    0.049\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_0   0.045\n",
       "NumberOfTime60-89DaysPastDueNotWorse_cat_0                                                           0.040\n",
       "NumberOfTime30-59DaysPastDueNotWorse_cat_0                                                           0.040\n",
       "RevolvingUtilizationOfUnsecuredLines_bins                                                            0.039\n",
       "RevolvingUtilizationOfUnsecuredLines                                                                 0.036\n",
       "Ratio                                                                                                0.035\n",
       "NumberOfOpenCreditLinesAndLoans_is_0_F                                                               0.025\n",
       "RevolvingUtilizationOfUnsecuredLines_is_0_F                                                          0.021\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_0                   0.016\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0                       0.015\n",
       "NumberOfTimes90DaysLate_cat_1                                                                        0.011\n",
       "NumberOfTime60-89DaysPastDueNotWorse_cat_1                                                           0.011\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1                    0.011\n",
       "NumberRealEstateLoansOrLines_is_0_F                                                                  0.010\n",
       "NumberOfDependents_cat_0.0                                                                           0.010\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2                    0.008\n",
       "NumberOfTimes90DaysLate_cat_2                                                                        0.007\n",
       "NumberOfTime30-59DaysPastDueNotWorse_cat_2                                                           0.007\n",
       "age                                                                                                  0.007\n",
       "NumberOfTimes90DaysLate_cat_4                                                                        0.007\n",
       "DebtRatio                                                                                            0.006\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1                       0.006\n",
       "NumberOfTime30-59DaysPastDueNotWorse_cat_4                                                           0.006\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2                       0.006\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_1                0.006\n",
       "NumberOfTimes90DaysLate_cat_3                                                                        0.006\n",
       "NumberOfTime60-89DaysPastDueNotWorse_cat_2                                                           0.005\n",
       "NumberOfTime30-59DaysPastDueNotWorse_cat_1                                                           0.005\n",
       "NumberRealEstateLoansOrLines                                                                         0.005\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1                       0.005\n",
       "NumberRealEstateLoansOrLines_is_0_T                                                                  0.005\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4                       0.005\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_0                0.004\n",
       "NumberOfTime30-59DaysPastDueNotWorse_cat_3                                                           0.004\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4                    0.004\n",
       "MonthlyIncome                                                                                        0.004\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_1                0.004\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_1   0.004\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_4                   0.004\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_2                0.004\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0                       0.004\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3                    0.003\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_2                   0.003\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_1                   0.003\n",
       "NumberOfTime60-89DaysPastDueNotWorse_cat_4                                                           0.003\n",
       "NumberOfOpenCreditLinesAndLoans                                                                      0.003\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_0   0.003\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_1   0.003\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4                       0.003\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_1   0.003\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_2                0.003\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_0   0.003\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_4                0.002\n",
       "NumberOfOpenCreditLinesAndLoans_is_0_T                                                               0.002\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_3                0.002\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_4                    0.002\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_3                   0.002\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_0   0.002\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_1   0.002\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_0                0.002\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_3                   0.002\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_2                0.002\n",
       "NumberOfTime60-89DaysPastDueNotWorse_cat_3                                                           0.002\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_3   0.002\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_4   0.002\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2                       0.002\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3                       0.002\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_4                   0.002\n",
       "RevolvingUtilizationOfUnsecuredLines_is_0_T                                                          0.002\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_4                0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_2                   0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_1                   0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_2   0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_3                   0.001\n",
       "NumberOfDependents_cat_3.0                                                                           0.001\n",
       "NumberOfDependents_cat_1.0                                                                           0.001\n",
       "NumberOfDependents_cat_2.0                                                                           0.001\n",
       "DebtRatio_bins_cat_2.0                                                                               0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_2   0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_3                0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_2   0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_3   0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_0                   0.001\n",
       "DebtRatio_bins_cat_0.05                                                                              0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_3                0.001\n",
       "NumberOfDependents_cat_4.0                                                                           0.001\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1                    0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_1                   0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_0                0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_4                0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_4   0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_0                   0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_0   0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_2                   0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_3                0.001\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_3   0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_0                0.001\n",
       "NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_3                       0.001\n",
       "DebtRatio_bins_cat_0.8                                                                               0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_0                   0.001\n",
       "DebtRatio_bins_cat_0.9                                                                               0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_1                0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_3                   0.001\n",
       "DebtRatio_bins_cat_0.45                                                                              0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_1                   0.001\n",
       "DebtRatio_bins_cat_0.95                                                                              0.001\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_2                0.001\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_0                   0.000\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_3                    0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_4                0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_3   0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_1                0.000\n",
       "DebtRatio_bins_cat_0.15                                                                              0.000\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_2                    0.000\n",
       "DebtRatio_bins_cat_0.4                                                                               0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_1   0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_3 + cat_2                0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_4   0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_4 + cat_2   0.000\n",
       "DebtRatio_bins_cat_0.75                                                                              0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_2                   0.000\n",
       "DebtRatio_bins_cat_0.85                                                                              0.000\n",
       "DebtRatio_bins_cat_0.25                                                                              0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_1                   0.000\n",
       "DebtRatio_bins_cat_0.7                                                                               0.000\n",
       "DebtRatio_bins_cat_1.0                                                                               0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_3                   0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_4.0 + cat_4                   0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_4 + cat_1                0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_3   0.000\n",
       "DebtRatio_bins_cat_0.65                                                                              0.000\n",
       "DebtRatio_bins_cat_0.6                                                                               0.000\n",
       "DebtRatio_bins_cat_0.55                                                                              0.000\n",
       "DebtRatio_bins_cat_0.5                                                                               0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_2   0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_2 + cat_4                0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_4                   0.000\n",
       "DebtRatio_bins_cat_0.3                                                                               0.000\n",
       "DebtRatio_bins_cat_0.2                                                                               0.000\n",
       "DebtRatio_bins_cat_0.1                                                                               0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_3 + cat_4   0.000\n",
       "NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_3                0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_4                   0.000\n",
       "NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_2                   0.000\n",
       "NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_4   0.000\n",
       "NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0                    0.000\n",
       "DebtRatio_bins_cat_0.35                                                                              0.000\n",
       "dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# увеличиваем количество строк вывода\n",
    "pd.set_option('display.max_rows', 200)\n",
    "\n",
    "# для большей ясности добавим имена переменных и\n",
    "# отсортируем по убыванию взаимной информации\n",
    "\n",
    "# превращаем массив NumPy со значениями\n",
    "# взаимной информации в серию\n",
    "mi = pd.Series(mi)\n",
    "# метками индекса будут имена переменных\n",
    "mi.index = X_train.columns\n",
    "# сортируем по убыванию\n",
    "mi.sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1514024 characters]\n",
      "text/plain": [
       "<Figure size 600x4000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# строим график значений взаимной информации\n",
    "mi.sort_values().plot.barh(figsize=(6, 40));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "121\n"
     ]
    }
   ],
   "source": [
    "# отбираем переменные выше порога и формируем список\n",
    "select_columns = mi.index[mi > 0.0003].tolist()\n",
    "print(len(select_columns))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUC-ROC на обучающей выборке: 0.863\n",
      "AUC-ROC на тестовой выборке: 0.862\n"
     ]
    }
   ],
   "source": [
    "# строим модель логистической регрессии на наборах предикторов,\n",
    "# сокращенных с помощью взаимной информации \n",
    "logreg_mi = LogisticRegression(solver='liblinear').fit(\n",
    "    X_train[select_columns], y_train)\n",
    "print(\"AUC-ROC на обучающей выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_train, logreg_mi.predict_proba(\n",
    "        X_train[select_columns])[:, 1])))\n",
    "print(\"AUC-ROC на тестовой выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_test, logreg_mi.predict_proba(\n",
    "        X_test[select_columns])[:, 1])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:39: UserWarning: You entered values for metric and clf, defaulting to clf and ignoring metric\n",
      "  warnings.warn('You entered values for metric and clf, defaulting to clf and ignoring metric')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  1\n",
      "Round:  1  iteration:  2\n",
      "Round:  1  iteration:  3\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  6\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  8\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore6_x', 'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  9\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x', 'fscore7_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n",
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:184: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.\n",
      "  df['Mean'] = df.mean(axis=1)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  1  iteration:  10\n",
      "Round:  2  iteration:  1\n",
      "Round:  2  iteration:  2\n",
      "Round:  2  iteration:  3\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  6\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  8\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore6_x', 'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  9\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x', 'fscore7_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n",
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:184: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.\n",
      "  df['Mean'] = df.mean(axis=1)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  2  iteration:  10\n",
      "Round:  3  iteration:  1\n",
      "Round:  3  iteration:  2\n",
      "Round:  3  iteration:  3\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  5\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  6\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  8\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore6_x', 'fscore4_x', 'fscore2_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Round:  3  iteration:  9\n",
      "Round:  3  iteration:  10\n",
      "BoostARoota ran successfully! Algorithm went through  3  rounds.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:180: FutureWarning: Passing 'suffixes' which cause duplicate columns {'fscore1_x', 'fscore5_x', 'fscore3_x', 'fscore7_x'} in the result is deprecated and will raise a MergeError in a future version.\n",
      "  df = pd.merge(df, df2, on='feature', how='outer')\n",
      "/Users/artemgruzdev/anaconda3/lib/python3.10/site-packages/boostaroota/boostaroota.py:184: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.\n",
      "  df['Mean'] = df.mean(axis=1)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<boostaroota.boostaroota.BoostARoota at 0x7f8628fc4610>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# импортируем BoostARoota\n",
    "from boostaroota import BoostARoota\n",
    "from sklearn.ensemble import ExtraTreesClassifier\n",
    "clf = ExtraTreesClassifier(random_state=42)\n",
    "\n",
    "# обучаем BoostARoota\n",
    "br = BoostARoota(metric='logloss', clf=clf)\n",
    "br.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# получаем массивы предикторов, отобранные с помощью BoostARoota\n",
    "X_train_boosta = br.transform(X_train)\n",
    "X_test_boosta = br.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(105000, 66)\n"
     ]
    }
   ],
   "source": [
    "# посмотрим, сколько предикторов оставила BoostARoota\n",
    "print(X_train_boosta.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['RevolvingUtilizationOfUnsecuredLines',\n",
       " 'age',\n",
       " 'DebtRatio',\n",
       " 'MonthlyIncome',\n",
       " 'NumberOfOpenCreditLinesAndLoans',\n",
       " 'NumberRealEstateLoansOrLines',\n",
       " 'Ratio',\n",
       " 'RevolvingUtilizationOfUnsecuredLines_bins',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse_cat_0',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse_cat_1',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse_cat_2',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse_cat_3',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse_cat_4',\n",
       " 'NumberOfTimes90DaysLate_cat_0',\n",
       " 'NumberOfTimes90DaysLate_cat_1',\n",
       " 'NumberOfTimes90DaysLate_cat_2',\n",
       " 'NumberOfTimes90DaysLate_cat_4',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse_cat_0',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse_cat_1',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse_cat_2',\n",
       " 'NumberOfDependents_cat_0.0',\n",
       " 'NumberOfDependents_cat_1.0',\n",
       " 'NumberOfDependents_cat_2.0',\n",
       " 'NumberOfDependents_cat_3.0',\n",
       " 'NumberOfDependents_cat_4.0',\n",
       " 'NumberRealEstateLoansOrLines_is_0_F',\n",
       " 'NumberRealEstateLoansOrLines_is_0_T',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_0',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_1',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_2',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_0.0 + cat_4',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_1.0 + cat_0',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_2.0 + cat_0',\n",
       " 'NumberOfDependents + NumberOfTime30-59DaysPastDueNotWorse_interact_cat_3.0 + cat_0',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_0',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_0 + cat_1',\n",
       " 'NumberOfTime60-89DaysPastDueNotWorse + NumberOfTimes90DaysLate_interact_cat_1 + cat_0',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_0',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_0 + cat_1',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_1 + cat_0',\n",
       " 'NumberOfTime30-59DaysPastDueNotWorse + NumberOfTime60-89DaysPastDueNotWorse_interact_cat_2 + cat_0',\n",
       " 'NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0',\n",
       " 'NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_0',\n",
       " 'NumberRealEstateLoansOrLines_is_0 + NumberOfTimes90DaysLate_interact_T + cat_1',\n",
       " 'NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_0',\n",
       " 'NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_1',\n",
       " 'NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_2',\n",
       " 'NumberOfOpenCreditLinesAndLoans_is_0 + NumberOfTimes90DaysLate_interact_F + cat_4',\n",
       " 'DebtRatio_bins_cat_0.05',\n",
       " 'DebtRatio_bins_cat_0.1',\n",
       " 'DebtRatio_bins_cat_0.15',\n",
       " 'DebtRatio_bins_cat_0.2',\n",
       " 'DebtRatio_bins_cat_0.25',\n",
       " 'DebtRatio_bins_cat_0.3',\n",
       " 'DebtRatio_bins_cat_0.35',\n",
       " 'DebtRatio_bins_cat_0.4',\n",
       " 'DebtRatio_bins_cat_0.45',\n",
       " 'DebtRatio_bins_cat_0.5',\n",
       " 'DebtRatio_bins_cat_0.55',\n",
       " 'DebtRatio_bins_cat_0.6',\n",
       " 'DebtRatio_bins_cat_0.65',\n",
       " 'DebtRatio_bins_cat_0.7',\n",
       " 'DebtRatio_bins_cat_0.75',\n",
       " 'DebtRatio_bins_cat_0.8',\n",
       " 'DebtRatio_bins_cat_0.85',\n",
       " 'DebtRatio_bins_cat_2.0']"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# посмотрим, какие предикторы оставила BoostARoota\n",
    "X_train_boosta.columns.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AUC-ROC на тестовой выборке: 0.863\n",
      "AUC-ROC на тестовой выборке: 0.863\n"
     ]
    }
   ],
   "source": [
    "# строим модель логистической регрессии на наборах предикторов,\n",
    "# сокращенных с помощью BoostARoota\n",
    "logreg_boosta = LogisticRegression(solver='liblinear').fit(\n",
    "    X_train_boosta, y_train)\n",
    "print(\"AUC-ROC на тестовой выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_train, logreg_boosta.predict_proba(\n",
    "        X_train_boosta)[:, 1])))\n",
    "print(\"AUC-ROC на тестовой выборке: {:.3f}\".format(\n",
    "    roc_auc_score(y_test, logreg_boosta.predict_proba(\n",
    "        X_test_boosta)[:, 1])))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
