{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:23.708337Z", "iopub.status.busy": "2023-05-20T13:31:23.707889Z", "iopub.status.idle": "2023-05-20T13:31:24.615636Z", "shell.execute_reply": "2023-05-20T13:31:24.614494Z", "shell.execute_reply.started": "2023-05-20T13:31:23.708303Z" } }, "outputs": [], "source": [ "import numpy as np \n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from scipy.stats import chi2_contingency " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:24.618585Z", "iopub.status.busy": "2023-05-20T13:31:24.617951Z", "iopub.status.idle": "2023-05-20T13:31:32.801864Z", "shell.execute_reply": "2023-05-20T13:31:32.800838Z", "shell.execute_reply.started": "2023-05-20T13:31:24.618546Z" } }, "outputs": [], "source": [ "app_train = pd.read_csv('data/application_train.csv.zip')\n", "app_test=pd.read_csv('data/application_test.csv.zip')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:32.803708Z", "iopub.status.busy": "2023-05-20T13:31:32.803286Z", "iopub.status.idle": "2023-05-20T13:31:33.178020Z", "shell.execute_reply": "2023-05-20T13:31:33.176717Z", "shell.execute_reply.started": "2023-05-20T13:31:32.803673Z" } }, "outputs": [], "source": [ "print(app_train[['ORGANIZATION_TYPE','NAME_FAMILY_STATUS', 'CODE_GENDER']].isnull().sum())\n", "print('-'*40)\n", "print(app_test[['ORGANIZATION_TYPE','NAME_FAMILY_STATUS', 'CODE_GENDER']].isnull().sum())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:33.181384Z", "iopub.status.busy": "2023-05-20T13:31:33.181034Z", "iopub.status.idle": "2023-05-20T13:31:34.800143Z", "shell.execute_reply": "2023-05-20T13:31:34.799026Z", "shell.execute_reply.started": "2023-05-20T13:31:33.181354Z" } }, "outputs": [], "source": [ "users_nan = (app_train.isnull().sum() / app_train.shape[0]) * 100\n", "users_nan[users_nan > 0].sort_values(ascending=False)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:34.803064Z", "iopub.status.busy": "2023-05-20T13:31:34.801805Z", "iopub.status.idle": "2023-05-20T13:31:37.424943Z", "shell.execute_reply": "2023-05-20T13:31:37.423842Z", "shell.execute_reply.started": "2023-05-20T13:31:34.803022Z" } }, "outputs": [], "source": [ "app_train.replace({'XNA': np.nan, 'XNP': np.nan, 'Unknown': np.nan}, inplace = True)\n", "app_test.replace({'XNA': np.nan, 'XNP': np.nan, 'Unknown': np.nan}, inplace = True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:37.426520Z", "iopub.status.busy": "2023-05-20T13:31:37.426208Z", "iopub.status.idle": "2023-05-20T13:31:37.781279Z", "shell.execute_reply": "2023-05-20T13:31:37.780147Z", "shell.execute_reply.started": "2023-05-20T13:31:37.426494Z" } }, "outputs": [], "source": [ "print(app_train[['ORGANIZATION_TYPE','NAME_FAMILY_STATUS', 'CODE_GENDER']].isnull().sum())\n", "print('-'*40)\n", "print(app_test[['ORGANIZATION_TYPE','NAME_FAMILY_STATUS', 'CODE_GENDER']].isnull().sum())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:37.783262Z", "iopub.status.busy": "2023-05-20T13:31:37.782696Z", "iopub.status.idle": "2023-05-20T13:31:39.299745Z", "shell.execute_reply": "2023-05-20T13:31:39.298642Z", "shell.execute_reply.started": "2023-05-20T13:31:37.783232Z" } }, "outputs": [], "source": [ "users_nan = (app_train.isnull().sum() / app_train.shape[0]) * 100\n", "users_nan[users_nan > 0].sort_values(ascending=False)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:39.301945Z", "iopub.status.busy": "2023-05-20T13:31:39.301331Z", "iopub.status.idle": "2023-05-20T13:31:40.848654Z", "shell.execute_reply": "2023-05-20T13:31:40.847530Z", "shell.execute_reply.started": "2023-05-20T13:31:39.301915Z" } }, "outputs": [], "source": [ "app_test.drop(app_train.columns[app_train.isnull().mean()>0.4],axis=1, inplace=True)\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:40.850599Z", "iopub.status.busy": "2023-05-20T13:31:40.850202Z", "iopub.status.idle": "2023-05-20T13:31:42.468788Z", "shell.execute_reply": "2023-05-20T13:31:42.467742Z", "shell.execute_reply.started": "2023-05-20T13:31:40.850564Z" } }, "outputs": [], "source": [ "app_train.drop(app_train.columns[app_train.isnull().mean()>0.4],axis=1, inplace=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:42.474671Z", "iopub.status.busy": "2023-05-20T13:31:42.474330Z", "iopub.status.idle": "2023-05-20T13:31:42.481468Z", "shell.execute_reply": "2023-05-20T13:31:42.480481Z", "shell.execute_reply.started": "2023-05-20T13:31:42.474643Z" } }, "outputs": [], "source": [ "app_train.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:42.482990Z", "iopub.status.busy": "2023-05-20T13:31:42.482691Z", "iopub.status.idle": "2023-05-20T13:31:42.497810Z", "shell.execute_reply": "2023-05-20T13:31:42.496609Z", "shell.execute_reply.started": "2023-05-20T13:31:42.482965Z" } }, "outputs": [], "source": [ "app_test.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:42.499575Z", "iopub.status.busy": "2023-05-20T13:31:42.499228Z", "iopub.status.idle": "2023-05-20T13:31:43.679359Z", "shell.execute_reply": "2023-05-20T13:31:43.678275Z", "shell.execute_reply.started": "2023-05-20T13:31:42.499547Z" } }, "outputs": [], "source": [ "users_nan = (app_train.isnull().sum() / app_train.shape[0]) * 100\n", "users_nan[users_nan > 0].sort_values(ascending=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:43.680950Z", "iopub.status.busy": "2023-05-20T13:31:43.680639Z", "iopub.status.idle": "2023-05-20T13:31:43.878881Z", "shell.execute_reply": "2023-05-20T13:31:43.877523Z", "shell.execute_reply.started": "2023-05-20T13:31:43.680924Z" } }, "outputs": [], "source": [ "users_nan = (app_test.isnull().sum() / app_test.shape[0]) * 100\n", "users_nan[users_nan > 0].sort_values(ascending=False)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:43.880911Z", "iopub.status.busy": "2023-05-20T13:31:43.880546Z", "iopub.status.idle": "2023-05-20T13:31:48.486015Z", "shell.execute_reply": "2023-05-20T13:31:48.484853Z", "shell.execute_reply.started": "2023-05-20T13:31:43.880873Z" } }, "outputs": [], "source": [ "# Columns have less 14% NaN Values and categorical\n", "Cat_columns_lower_percentage_nan = [i for i in app_train.columns[(((app_train.isnull().sum() / app_train.shape[0]) * 100) > 0) \n", " & (((app_train.isnull().sum() / app_train.shape[0]) * 100) < 14)] \n", " if app_train[i].dtype == 'O']\n", "\n", "# Columns have less 14% NaN Values and numerical\n", "num_columns_lower_percentage_nan = [i for i in app_train.columns[(((app_train.isnull().sum() / app_train.shape[0]) * 100) > 0) \n", " & (((app_train.isnull().sum() / app_train.shape[0]) * 100) < 14)] \n", " if app_train[i].dtype != 'O']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:48.487653Z", "iopub.status.busy": "2023-05-20T13:31:48.487321Z", "iopub.status.idle": "2023-05-20T13:31:48.899460Z", "shell.execute_reply": "2023-05-20T13:31:48.898112Z", "shell.execute_reply.started": "2023-05-20T13:31:48.487625Z" } }, "outputs": [], "source": [ "for i in Cat_columns_lower_percentage_nan:\n", " app_test[i].fillna(app_train[i].mode()[0], inplace=True)\n", " app_train[i].fillna(app_train[i].mode()[0], inplace=True)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:48.901595Z", "iopub.status.busy": "2023-05-20T13:31:48.901227Z", "iopub.status.idle": "2023-05-20T13:31:48.907826Z", "shell.execute_reply": "2023-05-20T13:31:48.906526Z", "shell.execute_reply.started": "2023-05-20T13:31:48.901557Z" } }, "outputs": [], "source": [ "col_mod_transfrom = [i for i in num_columns_lower_percentage_nan if i not in ['EXT_SOURCE_2', 'AMT_ANNUITY','AMT_GOODS_PRICE']]\n", "col_mean_transform = ['EXT_SOURCE_2', 'AMT_ANNUITY']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:48.911188Z", "iopub.status.busy": "2023-05-20T13:31:48.910234Z", "iopub.status.idle": "2023-05-20T13:31:49.048717Z", "shell.execute_reply": "2023-05-20T13:31:49.047610Z", "shell.execute_reply.started": "2023-05-20T13:31:48.911124Z" } }, "outputs": [], "source": [ "for i in col_mod_transfrom:\n", " app_test[i].fillna(app_train[i].mode()[0], inplace=True)\n", " app_train[i].fillna(app_train[i].mode()[0], inplace=True)\n", "for i in col_mean_transform:\n", " app_test[i].fillna(app_train[i].mean(), inplace=True)\n", " app_train[i].fillna(app_train[i].mean(), inplace=True)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.050761Z", "iopub.status.busy": "2023-05-20T13:31:49.050280Z", "iopub.status.idle": "2023-05-20T13:31:49.067588Z", "shell.execute_reply": "2023-05-20T13:31:49.066219Z", "shell.execute_reply.started": "2023-05-20T13:31:49.050725Z" } }, "outputs": [], "source": [ "app_train['AMT_GOODS_PRICE'].fillna(app_train['AMT_GOODS_PRICE'].median(),inplace = True)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.070174Z", "iopub.status.busy": "2023-05-20T13:31:49.069475Z", "iopub.status.idle": "2023-05-20T13:31:49.134864Z", "shell.execute_reply": "2023-05-20T13:31:49.133757Z", "shell.execute_reply.started": "2023-05-20T13:31:49.070111Z" } }, "outputs": [], "source": [ "all_numerical_cols = list(app_train.select_dtypes(exclude='object').columns)\n", "\n", "cont_cols = [col for col in all_numerical_cols if col != \"TARGET\" and col[:5]!='FLAG_']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.136879Z", "iopub.status.busy": "2023-05-20T13:31:49.136528Z", "iopub.status.idle": "2023-05-20T13:31:49.170520Z", "shell.execute_reply": "2023-05-20T13:31:49.169241Z", "shell.execute_reply.started": "2023-05-20T13:31:49.136849Z" } }, "outputs": [], "source": [ "app_train[(abs(app_train['DAYS_BIRTH']) < abs(app_train['DAYS_EMPLOYED'])) & (app_train['DAYS_EMPLOYED'] != 365243)]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.172092Z", "iopub.status.busy": "2023-05-20T13:31:49.171745Z", "iopub.status.idle": "2023-05-20T13:31:49.179130Z", "shell.execute_reply": "2023-05-20T13:31:49.178185Z", "shell.execute_reply.started": "2023-05-20T13:31:49.172040Z" } }, "outputs": [], "source": [ "proper_days_empolyed_df = app_train\n", "proper_days_empolyed_df['YEARS_EMPLOYED'] = proper_days_empolyed_df['DAYS_EMPLOYED']/-365.25\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.181041Z", "iopub.status.busy": "2023-05-20T13:31:49.180556Z", "iopub.status.idle": "2023-05-20T13:31:49.238079Z", "shell.execute_reply": "2023-05-20T13:31:49.236986Z", "shell.execute_reply.started": "2023-05-20T13:31:49.181007Z" } }, "outputs": [], "source": [ "app_train['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True) \n", "app_test['DAYS_EMPLOYED'].replace({365243:np.nan},inplace=True) " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.239903Z", "iopub.status.busy": "2023-05-20T13:31:49.239586Z", "iopub.status.idle": "2023-05-20T13:31:49.246895Z", "shell.execute_reply": "2023-05-20T13:31:49.245736Z", "shell.execute_reply.started": "2023-05-20T13:31:49.239876Z" } }, "outputs": [], "source": [ "proper_days_empolyed_df = app_train\n", "proper_days_empolyed_df['YEARS_EMPLOYED'] = proper_days_empolyed_df['DAYS_EMPLOYED']/-365.25\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.248350Z", "iopub.status.busy": "2023-05-20T13:31:49.247973Z", "iopub.status.idle": "2023-05-20T13:31:49.329255Z", "shell.execute_reply": "2023-05-20T13:31:49.328177Z", "shell.execute_reply.started": "2023-05-20T13:31:49.248321Z" } }, "outputs": [], "source": [ "app_train.groupby(['OCCUPATION_TYPE'])['DAYS_EMPLOYED'].mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.331266Z", "iopub.status.busy": "2023-05-20T13:31:49.330437Z", "iopub.status.idle": "2023-05-20T13:31:49.397670Z", "shell.execute_reply": "2023-05-20T13:31:49.396570Z", "shell.execute_reply.started": "2023-05-20T13:31:49.331236Z" } }, "outputs": [], "source": [ "## create dataframe with total income > 1M\n", "susp_df1 = app_train[app_train['AMT_INCOME_TOTAL']>1e+6][['AMT_INCOME_TOTAL','AMT_CREDIT','AMT_ANNUITY','CNT_CHILDREN', 'TARGET']].sort_values(by='AMT_INCOME_TOTAL', ascending=False)\n", "\n", "## create Credit/Income and Annuity/Income percentages\n", "susp_df1['Credit/Income'] = susp_df1['AMT_CREDIT']/susp_df1['AMT_INCOME_TOTAL']\n", "susp_df1['Annuity/Income'] = susp_df1['AMT_ANNUITY']/susp_df1['AMT_INCOME_TOTAL']\n", "\n", "## show only clients with difficuties\n", "susp_df1[susp_df1['TARGET']==1].sort_values(by='Credit/Income', ascending=True)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.399844Z", "iopub.status.busy": "2023-05-20T13:31:49.399208Z", "iopub.status.idle": "2023-05-20T13:31:49.523335Z", "shell.execute_reply": "2023-05-20T13:31:49.522194Z", "shell.execute_reply.started": "2023-05-20T13:31:49.399813Z" } }, "outputs": [], "source": [ "app_train = app_train[app_train['AMT_INCOME_TOTAL'] != 117000000.0]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.525201Z", "iopub.status.busy": "2023-05-20T13:31:49.524772Z", "iopub.status.idle": "2023-05-20T13:31:49.604686Z", "shell.execute_reply": "2023-05-20T13:31:49.603377Z", "shell.execute_reply.started": "2023-05-20T13:31:49.525143Z" } }, "outputs": [], "source": [ "## extract dataframe with DAYS_BIRTH and TARGET only\n", "susp_df2 = app_train[['DAYS_BIRTH','TARGET']]\n", "\n", "## create column represnts the age in years\n", "susp_df2['YEARS_BIRTH'] = np.abs(susp_df2['DAYS_BIRTH']) / 365.25\n", "\n", "## show datafame\n", "display(susp_df2.sort_values(by='YEARS_BIRTH', ascending=False))\n", "\n", "## show the value counts of those who are aged > 65 with respect to target\n", "display(susp_df2[(susp_df2['YEARS_BIRTH']>65)]['TARGET'].value_counts())" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2023-05-20T13:31:49.615247Z", "iopub.status.busy": "2023-05-20T13:31:49.614863Z", "iopub.status.idle": "2023-05-20T13:31:50.648599Z", "shell.execute_reply": "2023-05-20T13:31:50.647443Z", "shell.execute_reply.started": "2023-05-20T13:31:49.615217Z" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | NAME_CONTRACT_TYPE | \n", "CODE_GENDER | \n", "FLAG_OWN_CAR | \n", "FLAG_OWN_REALTY | \n", "NAME_TYPE_SUITE | \n", "NAME_INCOME_TYPE | \n", "NAME_EDUCATION_TYPE | \n", "NAME_FAMILY_STATUS | \n", "NAME_HOUSING_TYPE | \n", "OCCUPATION_TYPE | \n", "WEEKDAY_APPR_PROCESS_START | \n", "ORGANIZATION_TYPE | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| count | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "307510 | \n", "211119 | \n", "307510 | \n", "252136 | \n", "
| unique | \n", "2 | \n", "2 | \n", "2 | \n", "2 | \n", "7 | \n", "8 | \n", "5 | \n", "5 | \n", "6 | \n", "18 | \n", "7 | \n", "57 | \n", "
| top | \n", "Cash loans | \n", "F | \n", "N | \n", "Y | \n", "Unaccompanied | \n", "Working | \n", "Secondary / secondary special | \n", "Married | \n", "House / apartment | \n", "Laborers | \n", "TUESDAY | \n", "Business Entity Type 3 | \n", "
| freq | \n", "278231 | \n", "202451 | \n", "202923 | \n", "213311 | \n", "249817 | \n", "158773 | \n", "218390 | \n", "196433 | \n", "272867 | \n", "55185 | \n", "53900 | \n", "67991 | \n", "