{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%config Completer.use_jedi = False\npd.set_option('display.max_columns', None)\npd.set_option('display.max_rows', None)\npd.set_option('display.max_colwidth', None)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-14T05:22:24.588947Z","iopub.execute_input":"2022-08-14T05:22:24.589631Z","iopub.status.idle":"2022-08-14T05:22:25.13612Z","shell.execute_reply.started":"2022-08-14T05:22:24.589561Z","shell.execute_reply":"2022-08-14T05:22:25.134976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The 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\n* D_* = Delinquency variables\n* S_* = Spend variables\n* P_* = Payment variables\n* B_* = Balance variables\n* R_* = Risk variables\n**with 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).","metadata":{}},{"cell_type":"code","source":"# test=pd.read_parquet('../input/amex-parquet/test_data.parquet')\ntrain=pd.read_parquet('../input/amex-parquet/train_data.parquet')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:25.14304Z","iopub.execute_input":"2022-08-14T05:22:25.143484Z","iopub.status.idle":"2022-08-14T05:22:33.310719Z","shell.execute_reply.started":"2022-08-14T05:22:25.143441Z","shell.execute_reply":"2022-08-14T05:22:33.309573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"shape of training data:\",train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:33.312959Z","iopub.execute_input":"2022-08-14T05:22:33.313367Z","iopub.status.idle":"2022-08-14T05:22:33.319977Z","shell.execute_reply.started":"2022-08-14T05:22:33.313333Z","shell.execute_reply":"2022-08-14T05:22:33.318761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking the null's in data","metadata":{}},{"cell_type":"code","source":"percent_missing = train.isnull().sum() * 100 / len(train)\nmissing_value_df = pd.DataFrame({'column_name': train.columns,\n                                 'percent_missing': percent_missing})\nmissing_value_df.sort_values('percent_missing',ascending=False, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:33.323295Z","iopub.execute_input":"2022-08-14T05:22:33.323751Z","iopub.status.idle":"2022-08-14T05:22:36.075364Z","shell.execute_reply.started":"2022-08-14T05:22:33.323707Z","shell.execute_reply":"2022-08-14T05:22:36.074187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc={'figure.figsize':(11.7,8.27)})\nax = sns.barplot(x=\"percent_missing\", y=\"column_name\", data=missing_value_df[missing_value_df['percent_missing']>5]).set_title('Graphs showing more then 5 percente of null value columns')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:36.077911Z","iopub.execute_input":"2022-08-14T05:22:36.078299Z","iopub.status.idle":"2022-08-14T05:22:36.588471Z","shell.execute_reply.started":"2022-08-14T05:22:36.078266Z","shell.execute_reply":"2022-08-14T05:22:36.58726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"count of unique customers:\",train.customer_ID.nunique())","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:36.589807Z","iopub.execute_input":"2022-08-14T05:22:36.590197Z","iopub.status.idle":"2022-08-14T05:22:37.560978Z","shell.execute_reply.started":"2022-08-14T05:22:36.590142Z","shell.execute_reply":"2022-08-14T05:22:37.559702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Filtering data for one customer lets see how many records it has.","metadata":{}},{"cell_type":"code","source":"train[train.customer_ID=='0000099d6bd597052cdcda90ffabf56573fe9d7c79be5fbac11a8ed792feb62a']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:37.5626Z","iopub.execute_input":"2022-08-14T05:22:37.562981Z","iopub.status.idle":"2022-08-14T05:22:38.131317Z","shell.execute_reply.started":"2022-08-14T05:22:37.562947Z","shell.execute_reply":"2022-08-14T05:22:38.129989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As we have no of records lets pick the latest one only\ntrain_data=train.groupby('customer_ID').tail(1)\ntrain_data=train_data.set_index(['customer_ID'])\n#Drop date column since it is no longer relevant\ntrain_data.drop(['S_2'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:39.779466Z","iopub.execute_input":"2022-08-14T05:22:39.780473Z","iopub.status.idle":"2022-08-14T05:22:42.501099Z","shell.execute_reply.started":"2022-08-14T05:22:39.780429Z","shell.execute_reply":"2022-08-14T05:22:42.499974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"checking for the above member again for latest record\")\ntrain_data[train_data.index=='0000099d6bd597052cdcda90ffabf56573fe9d7c79be5fbac11a8ed792feb62a']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:42.503051Z","iopub.execute_input":"2022-08-14T05:22:42.504074Z","iopub.status.idle":"2022-08-14T05:22:42.667565Z","shell.execute_reply.started":"2022-08-14T05:22:42.504025Z","shell.execute_reply":"2022-08-14T05:22:42.666139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"shape of new data frame : \",train_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:42.834292Z","iopub.execute_input":"2022-08-14T05:22:42.835411Z","iopub.status.idle":"2022-08-14T05:22:42.843148Z","shell.execute_reply.started":"2022-08-14T05:22:42.835353Z","shell.execute_reply":"2022-08-14T05:22:42.84147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Identify columns which are not numeric\n# features being categorical:*\n# ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n# 'D_63', 'D_64' are of object must be categorical\ntrain_data.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:22:44.613292Z","iopub.execute_input":"2022-08-14T05:22:44.613727Z","iopub.status.idle":"2022-08-14T05:22:44.625896Z","shell.execute_reply.started":"2022-08-14T05:22:44.613691Z","shell.execute_reply":"2022-08-14T05:22:44.625047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform one-hot encoding for D_63 and D_64\n# Drop columns D_63 and D_64 subsequently\ntrain_D63 = pd.get_dummies(train_data[['D_63']])\ntrain_data = pd.concat([train_data, train_D63], axis=1)\ntrain_data = train_data.drop(['D_63'], axis=1)\n\ntrain_D64 = pd.get_dummies(train_data[['D_64']])\ntrain_data = pd.concat([train_data, train_D64], axis=1)\ntrain_data = train_data.drop(['D_64'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:23:03.409944Z","iopub.execute_input":"2022-08-14T05:23:03.410378Z","iopub.status.idle":"2022-08-14T05:23:04.760569Z","shell.execute_reply.started":"2022-08-14T05:23:03.410344Z","shell.execute_reply":"2022-08-14T05:23:04.759196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:23:06.617389Z","iopub.execute_input":"2022-08-14T05:23:06.617834Z","iopub.status.idle":"2022-08-14T05:23:06.631034Z","shell.execute_reply.started":"2022-08-14T05:23:06.617793Z","shell.execute_reply":"2022-08-14T05:23:06.629618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets check for the columns\ntrain_data.columns\n# We now have 197 columns including target\n# We need to reduce the dimensionality of the data\n# We shall remove highly correlated features.","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:23:08.04627Z","iopub.execute_input":"2022-08-14T05:23:08.04718Z","iopub.status.idle":"2022-08-14T05:23:08.054362Z","shell.execute_reply.started":"2022-08-14T05:23:08.047123Z","shell.execute_reply":"2022-08-14T05:23:08.053476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We shall remove highly correlated features.\ntrain_without_target=train_data.drop(['target'],axis=1)\ncor_matrix = train_without_target.corr().abs()\nupper_tri = cor_matrix.where((np.triu(np.ones(cor_matrix.shape), k=1) + np.tril(np.ones(cor_matrix.shape), k=-1)).astype(bool))\n#Drop out columns with absolute correlation of more than 85%\nto_drop = [column for column in upper_tri.columns if any(upper_tri[column] > 0.85)]\ntrain_drop_highcorr=train_without_target.drop(to_drop,axis=1)\ntrain_drop_highcorr.shape\n#We are now left with 159 columns (excluding target), which is still significant","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:24:00.293755Z","iopub.execute_input":"2022-08-14T05:24:00.294235Z","iopub.status.idle":"2022-08-14T05:24:43.042302Z","shell.execute_reply.started":"2022-08-14T05:24:00.294198Z","shell.execute_reply":"2022-08-14T05:24:43.040984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets remove columns with low variance=0.06. Keep only columns with high variance\nfrom sklearn.feature_selection import VarianceThreshold\nfrom itertools import compress\ndef fs_variance(df, threshold:float=0.06):\n    \"\"\"\n    Return a list of selected variables based on the threshold.\n    \"\"\"\n    # The list of columns in the data frame\n    features = list(df.columns)\n    \n    # Initialize and fit the method\n    vt = VarianceThreshold(threshold = threshold)\n    _ = vt.fit(df)\n    \n    # Get which column names which pass the threshold\n    feat_select = list(compress(features, vt.get_support()))\n    \n    return feat_select\ncolumns_to_keep=fs_variance(train_drop_highcorr)\n# columns_to_keep.extend(cat_features)\n# We are left with 74 columns (excluding target), which passed the threshold.\ntrain_final=train_data[columns_to_keep]\nlen(columns_to_keep)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:24:57.956798Z","iopub.execute_input":"2022-08-14T05:24:57.95759Z","iopub.status.idle":"2022-08-14T05:24:59.155193Z","shell.execute_reply.started":"2022-08-14T05:24:57.957534Z","shell.execute_reply":"2022-08-14T05:24:59.154237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Concat target onto train_final\ntrain_final1=train_final.join(train_data['target'])\nx_train=train_final1.drop(['target'],axis=1)\ny_train=train_final1['target']","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:25:23.873531Z","iopub.execute_input":"2022-08-14T05:25:23.873993Z","iopub.status.idle":"2022-08-14T05:25:24.127814Z","shell.execute_reply.started":"2022-08-14T05:25:23.873955Z","shell.execute_reply":"2022-08-14T05:25:24.126599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split train data into training and testing sets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score  \nfrom sklearn.metrics import precision_score                         \nfrom sklearn.metrics import recall_score\nx_train_split, x_test_split, y_train_split, y_test_split = train_test_split(x_train, y_train, test_size=0.30, random_state=20)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:29:26.995936Z","iopub.execute_input":"2022-08-14T05:29:26.996815Z","iopub.status.idle":"2022-08-14T05:29:27.449906Z","shell.execute_reply.started":"2022-08-14T05:29:26.996768Z","shell.execute_reply":"2022-08-14T05:29:27.44862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost import CatBoostClassifier\nmodel = CatBoostClassifier()\n# Fit model\nmodel.fit(x_train_split,y_train_split,verbose=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:29:28.429487Z","iopub.execute_input":"2022-08-14T05:29:28.430391Z","iopub.status.idle":"2022-08-14T05:31:18.193712Z","shell.execute_reply.started":"2022-08-14T05:29:28.430347Z","shell.execute_reply":"2022-08-14T05:31:18.192677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Test the model\ny_predict=model.predict(x_test_split)\nprint('\\n CatBoost Classifier Accuracy: {:.3f}'.format(accuracy_score(y_test_split, y_predict)))\n# Achieved 89.3% accuracy","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:31:18.436131Z","iopub.execute_input":"2022-08-14T05:31:18.436652Z","iopub.status.idle":"2022-08-14T05:31:18.628357Z","shell.execute_reply.started":"2022-08-14T05:31:18.436599Z","shell.execute_reply":"2022-08-14T05:31:18.627437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\n CatBoost Classifier Precision: {:.3f}'.format(precision_score (y_test_split, y_predict)))\n# Achieved Precision Score of 0.792","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:31:18.700713Z","iopub.execute_input":"2022-08-14T05:31:18.701077Z","iopub.status.idle":"2022-08-14T05:31:18.76965Z","shell.execute_reply.started":"2022-08-14T05:31:18.701043Z","shell.execute_reply":"2022-08-14T05:31:18.768235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('\\n CatBoost Classifier Recall: {:.3f}'.format(recall_score (y_test_split, y_predict)))\n#Achieved Recall Score of 0.791","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:28:59.469631Z","iopub.execute_input":"2022-08-14T05:28:59.470123Z","iopub.status.idle":"2022-08-14T05:28:59.52917Z","shell.execute_reply.started":"2022-08-14T05:28:59.470076Z","shell.execute_reply":"2022-08-14T05:28:59.527915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"columns_to_load=list(columns_to_keep)\ncolumns_to_load=columns_to_load+['D_63','D_64','customer_ID','S_2']\ncolumns_to_load.remove('D_63_CO')\ncolumns_to_load.remove('D_63_CR')\ncolumns_to_load.remove('D_63_CL')\ncolumns_to_load.remove('D_64_O')\ncolumns_to_load.remove('D_64_R')\ncolumns_to_load.remove('D_64_U')","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:32:22.387322Z","iopub.execute_input":"2022-08-14T05:32:22.388392Z","iopub.status.idle":"2022-08-14T05:32:22.395454Z","shell.execute_reply.started":"2022-08-14T05:32:22.38834Z","shell.execute_reply":"2022-08-14T05:32:22.39417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_parquet('../input/amex-parquet/test_data.parquet',columns=columns_to_load)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:32:46.710496Z","iopub.execute_input":"2022-08-14T05:32:46.711501Z","iopub.status.idle":"2022-08-14T05:33:21.466717Z","shell.execute_reply.started":"2022-08-14T05:32:46.71146Z","shell.execute_reply":"2022-08-14T05:33:21.465654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test_data.groupby('customer_ID').tail(1)\ntest=test.set_index(['customer_ID'])\n\n#Drop date column since it is no longer relevant\ntest.drop(['S_2'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:33:21.468389Z","iopub.execute_input":"2022-08-14T05:33:21.469001Z","iopub.status.idle":"2022-08-14T05:33:23.597281Z","shell.execute_reply.started":"2022-08-14T05:33:21.468964Z","shell.execute_reply":"2022-08-14T05:33:23.595926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_D63 = pd.get_dummies(test[['D_63']])\ntest = pd.concat([test, test_D63], axis=1)\ntest = test.drop(['D_63'], axis=1)\n\ntest_D64 = pd.get_dummies(test[['D_64']])\ntest = pd.concat([test, test_D64], axis=1)\ntest = test.drop(['D_64'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:33:27.084909Z","iopub.execute_input":"2022-08-14T05:33:27.085369Z","iopub.status.idle":"2022-08-14T05:33:28.105456Z","shell.execute_reply.started":"2022-08-14T05:33:27.085328Z","shell.execute_reply":"2022-08-14T05:33:28.104097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final=test[columns_to_keep]\ny_test_predict=model.predict_proba(test_final)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:33:30.69798Z","iopub.execute_input":"2022-08-14T05:33:30.698403Z","iopub.status.idle":"2022-08-14T05:33:31.846259Z","shell.execute_reply.started":"2022-08-14T05:33:30.69837Z","shell.execute_reply":"2022-08-14T05:33:31.845019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Retrieve the probability of default\ny_predict_final=y_test_predict[:,1]\n\n# Merge the prediction and customer_ID into submission dataframe\nsubmission = pd.DataFrame({\"customer_ID\":test_final.index,\"prediction\":y_predict_final})\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-14T05:33:45.695016Z","iopub.execute_input":"2022-08-14T05:33:45.696027Z","iopub.status.idle":"2022-08-14T05:33:49.413349Z","shell.execute_reply.started":"2022-08-14T05:33:45.695978Z","shell.execute_reply":"2022-08-14T05:33:49.411715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}