{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Ralation between number of each customer's statements and default","metadata":{}},{"cell_type":"markdown","source":"## About","metadata":{}},{"cell_type":"markdown","source":"This notebook analyzed the relation between the number of statements per customer and default.\n\nIn conclusion, you can find that customers with missing statements have a higher probability of default.\n\n#### *If you like this notebook, kindly upvote it !!*\n\nThank you.","metadata":{}},{"cell_type":"markdown","source":"## 1. Import libraries and Read dataset","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\n\nsns.set()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:23:43.313353Z","iopub.execute_input":"2022-07-09T16:23:43.314054Z","iopub.status.idle":"2022-07-09T16:23:43.953517Z","shell.execute_reply.started":"2022-07-09T16:23:43.313851Z","shell.execute_reply":"2022-07-09T16:23:43.952267Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_path = Path('/kaggle/input/amex-default-prediction/')\ninput_path_feather = Path('/kaggle/input/parquet-files-amexdefault-prediction/')\n\ntrain_data = pd.read_feather(input_path_feather / 'train_data.ftr')[['customer_ID', 'S_2']]\ntrain_labels = pd.read_csv(input_path / 'train_labels.csv', index_col='customer_ID')\ndf_train = train_data.merge(train_labels.reset_index(), how='inner', on='customer_ID')\n\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:23:43.955611Z","iopub.execute_input":"2022-07-09T16:23:43.956358Z","iopub.status.idle":"2022-07-09T16:24:13.372036Z","shell.execute_reply.started":"2022-07-09T16:23:43.956311Z","shell.execute_reply":"2022-07-09T16:24:13.370099Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Extract data","metadata":{}},{"cell_type":"markdown","source":"#### Number of statements for each `customer_ID` and `target`","metadata":{}},{"cell_type":"code","source":"default = [train_labels.loc[i]['target'] for i in df_train['customer_ID'].value_counts().index]\n\nn_per = 100\ndefault_per_id = [sum(default[i * n_per:(i + 1) * n_per]) for i in range(int(len(default) / n_per))]","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:13.374281Z","iopub.execute_input":"2022-07-09T16:24:13.3748Z","iopub.status.idle":"2022-07-09T16:24:49.762687Z","shell.execute_reply.started":"2022-07-09T16:24:13.374747Z","shell.execute_reply":"2022-07-09T16:24:49.761347Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Number of target class, by number of each customer's statements","metadata":{}},{"cell_type":"code","source":"bincount = np.bincount(df_train['customer_ID'].value_counts().values)[::-1][:-1]\nbin_cum = np.insert(bincount.cumsum(), 0, 0)\nbin_default = np.array([sum(default[bin_cum[i]:bin_cum[i + 1]]) for i in range(len(bincount))])\nratio = bin_default / bincount","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:49.765312Z","iopub.execute_input":"2022-07-09T16:24:49.765831Z","iopub.status.idle":"2022-07-09T16:24:50.401696Z","shell.execute_reply.started":"2022-07-09T16:24:49.765789Z","shell.execute_reply":"2022-07-09T16:24:50.400248Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Number of monthly statements","metadata":{}},{"cell_type":"code","source":"n_record_month = np.array([[0] * 12, [0] * 12])\nfor date, n in df_train[df_train['target'] == 0]['S_2'].value_counts().items():\n    month = int(date[5:7]) - 1\n    n_record_month[0][month] += n\nfor date, n in df_train[df_train['target'] == 1]['S_2'].value_counts().items():\n    month = int(date[5:7]) - 1\n    n_record_month[1][month] += n\n\nn_record_last_month = np.array([[0] * 12, [0] * 12])\nfor date, n in df_train[df_train['target'] == 0].groupby('customer_ID').tail(1)['S_2'].value_counts().items():\n    month = int(date[5:7]) - 1\n    n_record_last_month[0][month] += n\nfor date, n in df_train[df_train['target'] == 1].groupby('customer_ID').tail(1)['S_2'].value_counts().items():\n    month = int(date[5:7]) - 1\n    n_record_last_month[1][month] += n","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:50.403376Z","iopub.execute_input":"2022-07-09T16:24:50.403725Z","iopub.status.idle":"2022-07-09T16:24:52.979084Z","shell.execute_reply.started":"2022-07-09T16:24:50.403694Z","shell.execute_reply":"2022-07-09T16:24:52.97794Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Illustrate graphs","metadata":{}},{"cell_type":"markdown","source":"### Figure 1\n\n#### Number of target class (0 or 1), by number of each customer's statements\n\n#### Ratio of target class (0 or 1), by number of each customer's statements","metadata":{}},{"cell_type":"code","source":"width = 0.4\nfig = plt.figure(figsize=(24, 9))\nax1 = fig.add_subplot(1, 2, 1)\nax1.bar(np.arange(len(ratio)) - width / 2, bincount - bin_default, width=width, label='paid off (class 0)')\nax1.bar(np.arange(len(ratio)) + width /2, bin_default, width=width, label='default (class 1)')\nax1.legend(title='target')\nax1.set_xticks(np.arange(len(ratio)), np.arange(1, len(ratio) + 1)[::-1])\nax1.set_yscale('log')\nax1.set_xlabel('number of statements  (large  <--->  small)')\nax1.set_ylabel('number of customers')\n\nax2 = fig.add_subplot(1, 2, 2)\nax2.bar(np.arange(len(ratio)), 1 - ratio, bottom=ratio, label='paid off (class 0)')\nax2.bar(np.arange(len(ratio)), ratio, label='default (class 1)')\nax2.set_xticks(np.arange(len(ratio)), np.arange(1, len(ratio) + 1)[::-1])\nax2.legend(title='target')\nax2.set_xlabel('number of statements  (large  <--->  small)')\nax2.set_ylabel('ratio of target class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:52.980557Z","iopub.execute_input":"2022-07-09T16:24:52.981253Z","iopub.status.idle":"2022-07-09T16:24:53.864254Z","shell.execute_reply.started":"2022-07-09T16:24:52.981212Z","shell.execute_reply":"2022-07-09T16:24:53.863148Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Figure 2\n\n#### Blue line: Number of defaults per 100 customers\n\n#### Orange line: Number of each customer's statements per 100 customers","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(24, 9))\nax1 = fig.add_subplot(1, 2, 1)\nax1.plot(default_per_id, color='C0', label='default')\nax2 = ax1.twinx()\nax2.plot(df_train['customer_ID'].value_counts().values[::n_per], color='C1', label='statements')\nh1, l1 = ax1.get_legend_handles_labels()\nh2, l2 = ax2.get_legend_handles_labels()\nax1.legend(h1 + h2, l1 + l2, loc='upper right')\nax1.set_xlabel(f'sorted customer (per {n_per})')\nax1.set_ylabel(f'number of defaults (per {n_per} customers)')\nax1.grid(True)\nax2.set_ylabel('number of statements')\nax2.grid(False)\nax1.set_ylim(5, 65)\n\ni_sep = 3850\nax3 = fig.add_subplot(1, 2, 2)\nax3.plot(default_per_id[i_sep:], color='C0', label='default')\nax4 = ax3.twinx()\nax4.plot(train_data['customer_ID'].value_counts().values[::n_per][i_sep:], color='C1', label='statements')\nh3, l3 = ax3.get_legend_handles_labels()\nh4, l4 = ax4.get_legend_handles_labels()\nax3.legend(h3 + h4, l3 + l4, loc='upper right')\nax3.set_xlabel(f'sorted customer (per {n_per})')\nax3.set_ylabel(f'number of defaults (per {n_per} customers)')\nax3.grid(True)\nax4.set_ylabel('number of statements')\nax4.grid(False)\nax3.set_xticks(np.arange(0, len(default_per_id[i_sep:]), n_per), np.arange(0, len(default_per_id[i_sep:]), n_per) + i_sep)\nax3.set_ylim(5, 65)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:53.865586Z","iopub.execute_input":"2022-07-09T16:24:53.865936Z","iopub.status.idle":"2022-07-09T16:24:55.580046Z","shell.execute_reply.started":"2022-07-09T16:24:53.865904Z","shell.execute_reply":"2022-07-09T16:24:55.578925Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Figure 3\n\n#### Number of monthly statements\n\n#### Number of last month's statements","metadata":{}},{"cell_type":"code","source":"fig = plt.figure(figsize=(24, 9))\nax1 = fig.add_subplot(1, 2, 1)\nax1.plot(n_record_month[0], color='C0', label='paid off (class 0)')\nax1.plot(n_record_month[1], color='C1', label='default (class 1)')\nax1.legend(title='target')\nax1.set_xlabel('month')\nax1.set_ylabel('number of statements')\nax1.set_xticks(np.arange(12), np.arange(12) + 1)\nax1.set_ylim(-10000, 680000)\n\nax2 = fig.add_subplot(1, 2, 2)\nax2.plot(n_record_last_month[0], color='C0', label='paid off (class 0)')\nax2.plot(n_record_last_month[1], color='C1', label='default (class 1)')\nax2.legend(title='target')\nax2.set_xlabel('month')\nax2.set_ylabel('number of statements')\nax2.set_xticks(np.arange(12), np.arange(12) + 1)\nax2.set_ylim(-10000, 680000)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-09T16:24:55.581709Z","iopub.execute_input":"2022-07-09T16:24:55.58204Z","iopub.status.idle":"2022-07-09T16:24:56.033764Z","shell.execute_reply.started":"2022-07-09T16:24:55.582013Z","shell.execute_reply":"2022-07-09T16:24:56.032775Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]}]}