{"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":"code","source":"# Import libraries\nimport os\nimport warnings\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc  # Garbage collector\n\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-22T17:58:36.812323Z","iopub.execute_input":"2022-06-22T17:58:36.812810Z","iopub.status.idle":"2022-06-22T17:58:36.818755Z","shell.execute_reply.started":"2022-06-22T17:58:36.812762Z","shell.execute_reply":"2022-06-22T17:58:36.817629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Description\nThe objective of this competition is to predict the probability that a customer does not pay back their credit card balance amount in the future based on their monthly customer profile. The target binary variable is calculated by observing 18 months performance window after the latest credit card statement, and if the customer does not pay due amount in 120 days after their latest statement date it is considered a default event.\n\nThe dataset contains aggregated profile features for each customer at each statement date. Features are anonymized and normalized, and fall into the following general categories:\n\nD_* = Delinquency variables\nS_* = Spend variables\nP_* = Payment variables\nB_* = Balance variables\nR_* = Risk variables\nwith the following features being categorical:\n\n['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n\nYour task is to predict, for each customer_ID, the probability of a future payment default (target = 1).\n\nNote that the negative class has been subsampled for this dataset at 5%, and thus receives a 20x weighting in the scoring metric.","metadata":{}},{"cell_type":"markdown","source":"### Data Exploration","metadata":{}},{"cell_type":"code","source":"# Reading feather format data(memory efficient) \n# Source: ttps://www.kaggle.com/datasets/munumbutt/amexfeather\n\ntrain_raw = pd.read_feather('../input/amexfeather/train_data.ftr')","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:58:36.820677Z","iopub.execute_input":"2022-06-22T17:58:36.821228Z","iopub.status.idle":"2022-06-22T17:58:40.456372Z","shell.execute_reply.started":"2022-06-22T17:58:36.821183Z","shell.execute_reply":"2022-06-22T17:58:40.455305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw.head(15)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:58:40.457729Z","iopub.execute_input":"2022-06-22T17:58:40.458628Z","iopub.status.idle":"2022-06-22T17:58:40.495845Z","shell.execute_reply.started":"2022-06-22T17:58:40.458590Z","shell.execute_reply":"2022-06-22T17:58:40.494707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:29.927219Z","iopub.execute_input":"2022-06-22T17:48:29.928009Z","iopub.status.idle":"2022-06-22T17:48:29.970561Z","shell.execute_reply.started":"2022-06-22T17:48:29.927940Z","shell.execute_reply":"2022-06-22T17:48:29.969619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_raw.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:29.972810Z","iopub.execute_input":"2022-06-22T17:48:29.973410Z","iopub.status.idle":"2022-06-22T17:48:29.980884Z","shell.execute_reply.started":"2022-06-22T17:48:29.973368Z","shell.execute_reply":"2022-06-22T17:48:29.979737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Number of unique customers in the dataset\ntrain_raw[\"customer_ID\"].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:29.982687Z","iopub.execute_input":"2022-06-22T17:48:29.983112Z","iopub.status.idle":"2022-06-22T17:48:31.151600Z","shell.execute_reply.started":"2022-06-22T17:48:29.983075Z","shell.execute_reply":"2022-06-22T17:48:31.150614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Date range\ntrain_raw[\"S_2\"].min(), train_raw[\"S_2\"].max()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:31.152811Z","iopub.execute_input":"2022-06-22T17:48:31.153387Z","iopub.status.idle":"2022-06-22T17:48:31.197802Z","shell.execute_reply.started":"2022-06-22T17:48:31.153349Z","shell.execute_reply":"2022-06-22T17:48:31.196856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No. of features for each category:\")\nfor pref in [\"D_\", \"S_\", \"P_\", \"B_\", \"R_\"]:\n    print(f\"{pref} : {len([i for i in train_raw.columns if i.startswith(pref)])}\")\n","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:31.198998Z","iopub.execute_input":"2022-06-22T17:48:31.199334Z","iopub.status.idle":"2022-06-22T17:48:31.205541Z","shell.execute_reply.started":"2022-06-22T17:48:31.199305Z","shell.execute_reply":"2022-06-22T17:48:31.204405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Target values distribution\ntrain_raw[\"target\"].value_counts(\"%\")","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:31.206703Z","iopub.execute_input":"2022-06-22T17:48:31.207054Z","iopub.status.idle":"2022-06-22T17:48:31.250200Z","shell.execute_reply.started":"2022-06-22T17:48:31.207017Z","shell.execute_reply":"2022-06-22T17:48:31.249110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Missing values\ntmp = train_raw.isna().sum().mul(100).div(len(train_raw)).sort_values(ascending=False)\n\nfig, ax = plt.subplots(2,1, figsize=(30,10))\nsns.barplot(x=tmp[:100].index, y=tmp[:100].values, ax=ax[0])\nsns.barplot(x=tmp[100:].index, y=tmp[100:].values, ax=ax[1])\nax[0].set_ylabel(\"Percentage [%]\"), ax[1].set_ylabel(\"Percentage [%]\")\nax[0].tick_params(axis='x', rotation=90); ax[1].tick_params(axis='x', rotation=90)\nplt.suptitle(\"Amount of missing data\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:31.251607Z","iopub.execute_input":"2022-06-22T17:48:31.252064Z","iopub.status.idle":"2022-06-22T17:48:39.647214Z","shell.execute_reply.started":"2022-06-22T17:48:31.252030Z","shell.execute_reply":"2022-06-22T17:48:39.646332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"missingDF = pd.DataFrame(tmp).reset_index()\nmissingDF[missingDF[0]>90]","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:39.649471Z","iopub.execute_input":"2022-06-22T17:48:39.649817Z","iopub.status.idle":"2022-06-22T17:48:39.667630Z","shell.execute_reply.started":"2022-06-22T17:48:39.649787Z","shell.execute_reply":"2022-06-22T17:48:39.666497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# columns with no missing values\nlen(missingDF[missingDF[0]==0])","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:39.668706Z","iopub.execute_input":"2022-06-22T17:48:39.669040Z","iopub.status.idle":"2022-06-22T17:48:39.676385Z","shell.execute_reply.started":"2022-06-22T17:48:39.669013Z","shell.execute_reply":"2022-06-22T17:48:39.675486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# only 3 columns in payment(P_) category, checking missing data in them\ntrain_raw[[i for i in train_raw.columns if i.startswith(\"P_\")]].isna().sum().div(len(train_raw)).mul(100)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T17:48:39.677512Z","iopub.execute_input":"2022-06-22T17:48:39.678190Z","iopub.status.idle":"2022-06-22T17:48:39.838012Z","shell.execute_reply.started":"2022-06-22T17:48:39.678158Z","shell.execute_reply":"2022-06-22T17:48:39.837040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Data Exploration for Two Customers with different \"target\" values(risk scores)","metadata":{}},{"cell_type":"code","source":"filtered_customer_data =train_raw[train_raw[\"customer_ID\"].isin(['0000099d6bd597052cdcda90ffabf56573fe9d7c79be5fbac11a8ed792feb62a', '00000fd6641609c6ece5454664794f0340ad84dddce9a267a310b5ae68e9d8e5'])]","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:01:17.891499Z","iopub.execute_input":"2022-06-22T18:01:17.891965Z","iopub.status.idle":"2022-06-22T18:01:18.241762Z","shell.execute_reply.started":"2022-06-22T18:01:17.891913Z","shell.execute_reply":"2022-06-22T18:01:18.240885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_customer_data.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:01:49.094077Z","iopub.execute_input":"2022-06-22T18:01:49.094513Z","iopub.status.idle":"2022-06-22T18:01:49.123081Z","shell.execute_reply.started":"2022-06-22T18:01:49.094473Z","shell.execute_reply":"2022-06-22T18:01:49.122035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_customer_data.tail(1)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:01:56.080450Z","iopub.execute_input":"2022-06-22T18:01:56.080887Z","iopub.status.idle":"2022-06-22T18:01:56.107382Z","shell.execute_reply.started":"2022-06-22T18:01:56.080851Z","shell.execute_reply":"2022-06-22T18:01:56.106148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_customer_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:02:12.144849Z","iopub.execute_input":"2022-06-22T18:02:12.145306Z","iopub.status.idle":"2022-06-22T18:02:12.169701Z","shell.execute_reply.started":"2022-06-22T18:02:12.145268Z","shell.execute_reply":"2022-06-22T18:02:12.168989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filtered_customer_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:02:18.962192Z","iopub.execute_input":"2022-06-22T18:02:18.963063Z","iopub.status.idle":"2022-06-22T18:02:18.967644Z","shell.execute_reply.started":"2022-06-22T18:02:18.963022Z","shell.execute_reply":"2022-06-22T18:02:18.966594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filtered_customer_data.isna().sum().mul(100).div(len(train_raw)).sort_values(ascending=False)[:5]","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:02:26.750473Z","iopub.execute_input":"2022-06-22T18:02:26.751552Z","iopub.status.idle":"2022-06-22T18:02:26.756273Z","shell.execute_reply.started":"2022-06-22T18:02:26.751503Z","shell.execute_reply":"2022-06-22T18:02:26.755040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# excluding categorical and date features\nplot_cols = [i for i in filtered_customer_data.columns if i not in [\"target\",\"customer_ID\",\"S_2\",'B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']]","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:34:05.877506Z","iopub.execute_input":"2022-06-22T18:34:05.878078Z","iopub.status.idle":"2022-06-22T18:34:05.884824Z","shell.execute_reply.started":"2022-06-22T18:34:05.878035Z","shell.execute_reply":"2022-06-22T18:34:05.883805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(plot_cols)","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:34:12.113768Z","iopub.execute_input":"2022-06-22T18:34:12.114222Z","iopub.status.idle":"2022-06-22T18:34:12.121339Z","shell.execute_reply.started":"2022-06-22T18:34:12.114186Z","shell.execute_reply":"2022-06-22T18:34:12.120451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### How each feature varies with time for both target values","metadata":{}},{"cell_type":"code","source":"plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)\nfig, axs = plt.subplots(23,8,figsize=(25, 150))\nfor i,ax in zip(plot_cols[:],axs.ravel()):\n    filtered_customer_data.groupby(\"customer_ID\").plot(x=\"S_2\", y=i, marker=\"o\", ax=ax)\n    ax.legend([\"0\",\"1\"])\n    ax.set_title(i)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-22T18:48:50.914532Z","iopub.execute_input":"2022-06-22T18:48:50.914943Z","iopub.status.idle":"2022-06-22T18:49:52.200770Z","shell.execute_reply.started":"2022-06-22T18:48:50.914911Z","shell.execute_reply":"2022-06-22T18:49:52.199054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### MORE EDA COMING SOON\n# DO UPVOTE!","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}