{"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":"#-------------------------------------------------------------------------------------------------------------------------------\nimport pandas as pd                                                 # Importing for panel data analysis\n#from pandas_profiling import ProfileReport                          # Import Pandas Profiling (To generate Univariate Analysis)\n\npd.set_option('display.max_columns', None)                          # Unfolding hidden features if the cardinality is high      \npd.set_option('display.max_colwidth', None)                         # Unfolding the max feature width for better clearity      \npd.set_option('display.max_rows', None)                             # Unfolding hidden data points if the cardinality is high\npd.set_option('mode.chained_assignment', None)                      # Removing restriction over chained assignments operations\npd.set_option('display.float_format', lambda x: '%.5f' % x)         # To suppress scientific notation over exponential values\n#-------------------------------------------------------------------------------------------------------------------------------\nimport numpy as np                                                  # Importing package numpys (For Numerical Python)\n#-------------------------------------------------------------------------------------------------------------------------------\nimport matplotlib.pyplot as plt                                     # Importing pyplot interface using matplotlib\nfrom matplotlib.pylab import rcParams                               # Backend used for rendering and GUI integration                                               \nimport seaborn as sns                                               # Importin seaborm library for interactive visualization\n%matplotlib inline\n#-------------------------------------------------------------------------------------------------------------------------------\nfrom sklearn.metrics import accuracy_score                          # For calculating the accuracy for the model\nfrom sklearn.metrics import precision_score                         # For calculating the Precision of the model\nfrom sklearn.metrics import recall_score                            # For calculating the recall of the model\nfrom sklearn.metrics import precision_recall_curve                  # For precision and recall metric estimation\nfrom sklearn.metrics import confusion_matrix                        # For verifying model performance using confusion matrix\nfrom sklearn.metrics import f1_score                                # For Checking the F1-Score of our model  \nfrom sklearn.metrics import roc_curve                               # For Roc-Auc metric estimation\n#-------------------------------------------------------------------------------------------------------------------------------\nfrom sklearn.model_selection import train_test_split                # To split the data in training and testing part     \nfrom sklearn.linear_model import LogisticRegression                 # To create the Logistic Regression Model\nfrom sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier\nfrom sklearn.metrics import confusion_matrix,classification_report\nfrom sklearn.model_selection import cross_val_score\n#-------------------------------------------------------------------------------------------------------------------------------\nimport warnings                                                     # Importing warning to disable runtime warnings\nwarnings.filterwarnings(\"ignore\")                                   # Warnings will appear only once\n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:46:20.208799Z","iopub.execute_input":"2022-08-18T08:46:20.209235Z","iopub.status.idle":"2022-08-18T08:46:20.226338Z","shell.execute_reply.started":"2022-08-18T08:46:20.209202Z","shell.execute_reply":"2022-08-18T08:46:20.225028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/amex-default-prediction/train_data.csv',nrows=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:46:24.050997Z","iopub.execute_input":"2022-08-18T08:46:24.051435Z","iopub.status.idle":"2022-08-18T08:46:24.077320Z","shell.execute_reply.started":"2022-08-18T08:46:24.051401Z","shell.execute_reply":"2022-08-18T08:46:24.076134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info(verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:22:56.166711Z","iopub.execute_input":"2022-08-18T08:22:56.168201Z","iopub.status.idle":"2022-08-18T08:22:56.185521Z","shell.execute_reply.started":"2022-08-18T08:22:56.168132Z","shell.execute_reply":"2022-08-18T08:22:56.184372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"df.info(verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-10T07:55:57.947288Z","iopub.execute_input":"2022-08-10T07:55:57.947669Z","iopub.status.idle":"2022-08-10T07:55:57.989797Z","shell.execute_reply.started":"2022-08-10T07:55:57.947636Z","shell.execute_reply":"2022-08-10T07:55:57.988363Z"}}},{"cell_type":"code","source":"list_columns=df.columns.to_list()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:46:31.965648Z","iopub.execute_input":"2022-08-18T08:46:31.966056Z","iopub.status.idle":"2022-08-18T08:46:31.972628Z","shell.execute_reply.started":"2022-08-18T08:46:31.966024Z","shell.execute_reply":"2022-08-18T08:46:31.971094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"list_columns","metadata":{"execution":{"iopub.status.busy":"2022-08-11T15:43:36.832534Z","iopub.execute_input":"2022-08-11T15:43:36.833320Z","iopub.status.idle":"2022-08-11T15:43:36.848509Z","shell.execute_reply.started":"2022-08-11T15:43:36.833288Z","shell.execute_reply":"2022-08-11T15:43:36.847231Z"}}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"#List with balance variable columns\nbal_col=['B_1','B_10','B_11','B_12','B_13','B_14','B_15','B_16','B_17','B_18','B_19','B_2','B_20','B_21','B_22','B_23','B_24','B_25','B_26','B_27','B_28','B_29','B_3','B_30','B_31','B_32','B_33','B_36','B_37','B_38','B_39','B_4','B_40','B_41','B_42','B_5','B_6','B_7','B_8','B_9']","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:46:40.022518Z","iopub.execute_input":"2022-08-18T08:46:40.022959Z","iopub.status.idle":"2022-08-18T08:46:40.031113Z","shell.execute_reply.started":"2022-08-18T08:46:40.022926Z","shell.execute_reply":"2022-08-18T08:46:40.029612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#List with R variable columns\nR_col=['R_1','R_10','R_11','R_12','R_13','R_14','R_15','R_16','R_17','R_18','R_19','R_2','R_20','R_21',\n 'R_22','R_23','R_24','R_25','R_26','R_27','R_28','R_3','R_4','R_5','R_6','R_7','R_8','R_9']","metadata":{}},{"cell_type":"markdown","source":"#List with Deliquency columns\nDel_col=['D_102','D_103','D_104','D_105','D_106','D_107','D_108','D_109','D_110','D_111','D_112','D_113','D_114','D_115','D_116','D_117','D_118','D_119','D_120','D_121','D_122','D_123','D_124','D_125', 'D_126',\n 'D_127','D_128','D_129','D_130','D_131','D_132','D_133','D_134','D_135','D_136','D_137','D_138','D_139','D_140','D_141','D_142','D_143','D_144','D_145','D_39','D_41','D_42','D_43','D_44','D_45','D_46','D_47','D_48','D_49','D_50','D_51','D_52','D_53','D_54','D_55','D_56','D_58','D_59','D_60','D_61','D_62','D_63','D_64','D_65','D_66','D_68','D_69','D_70','D_71','D_72','D_73','D_74','D_75','D_76','D_77','D_78','D_79','D_80','D_81','D_82','D_83','D_84','D_86','D_87','D_88','D_89','D_91','D_92','D_93','D_94','D_96']\n","metadata":{}},{"cell_type":"markdown","source":"#List with spending columns\nSpend_col=['S_11','S_12','S_13','S_15','S_16','S_17','S_18','S_19','S_2','S_20','S_22','S_23','S_24','S_25','S_26','S_27','S_3','S_5','S_6','S_7','S_8','S_9']","metadata":{}},{"cell_type":"markdown","source":"#list with payment columns\nPay_col=['P_2','P_3','P_4']","metadata":{}},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:51:06.744714Z","iopub.execute_input":"2022-08-18T08:51:06.745953Z","iopub.status.idle":"2022-08-18T08:51:06.918515Z","shell.execute_reply.started":"2022-08-18T08:51:06.745906Z","shell.execute_reply":"2022-08-18T08:51:06.917082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bal=pd.read_csv('../input/amex-default-prediction/train_data.csv',usecols=bal_col,dtype='float16')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:51:11.232282Z","iopub.execute_input":"2022-08-18T08:51:11.233341Z","iopub.status.idle":"2022-08-18T08:53:42.642507Z","shell.execute_reply.started":"2022-08-18T08:51:11.233295Z","shell.execute_reply":"2022-08-18T08:53:42.641094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#calculate missing values\nmiss_value=df_bal.isnull().sum()\nper_miss=((miss_value/df_bal.index.size)*100)\nper_miss","metadata":{"execution":{"iopub.status.busy":"2022-08-11T15:47:48.511431Z","iopub.execute_input":"2022-08-11T15:47:48.512442Z","iopub.status.idle":"2022-08-11T15:47:49.596771Z","shell.execute_reply.started":"2022-08-11T15:47:48.512407Z","shell.execute_reply":"2022-08-11T15:47:49.594556Z"}}},{"cell_type":"code","source":"#Remove columns with more than 50% missing values","metadata":{"execution":{"iopub.status.busy":"2022-08-16T13:56:49.614117Z","iopub.execute_input":"2022-08-16T13:56:49.614656Z","iopub.status.idle":"2022-08-16T13:56:49.621722Z","shell.execute_reply.started":"2022-08-16T13:56:49.614599Z","shell.execute_reply":"2022-08-16T13:56:49.620528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bal.drop(['B_17','B_29','B_42','B_39'],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:56:46.977887Z","iopub.execute_input":"2022-08-18T08:56:46.978470Z","iopub.status.idle":"2022-08-18T08:56:48.303701Z","shell.execute_reply.started":"2022-08-18T08:56:46.978426Z","shell.execute_reply":"2022-08-18T08:56:48.302493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bal=df_bal.astype('float64')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:56:52.337168Z","iopub.execute_input":"2022-08-18T08:56:52.337703Z","iopub.status.idle":"2022-08-18T08:56:54.918252Z","shell.execute_reply.started":"2022-08-18T08:56:52.337665Z","shell.execute_reply":"2022-08-18T08:56:54.916752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bal.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:56:57.810832Z","iopub.execute_input":"2022-08-18T08:56:57.811920Z","iopub.status.idle":"2022-08-18T08:57:11.667287Z","shell.execute_reply.started":"2022-08-18T08:56:57.811875Z","shell.execute_reply":"2022-08-18T08:57:11.666098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:57:35.743570Z","iopub.execute_input":"2022-08-18T08:57:35.744050Z","iopub.status.idle":"2022-08-18T08:57:35.750707Z","shell.execute_reply.started":"2022-08-18T08:57:35.744006Z","shell.execute_reply":"2022-08-18T08:57:35.749539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Columns  with high range\n## B_4, B_5,B_6,B_9,B_10,B_12,B_13,B_14,B_15,B_21,B_24,B_25,B_26,B_28,B_40,B_41","metadata":{}},{"cell_type":"markdown","source":"col_box=['B_4', 'B_5','B_6','B_9','B_10','B_12','B_13','B_14','B_15','B_21','B_24','B_25','B_26','B_28','B_40','B_41']","metadata":{}},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:57:41.587434Z","iopub.execute_input":"2022-08-18T08:57:41.588361Z","iopub.status.idle":"2022-08-18T08:57:41.775744Z","shell.execute_reply.started":"2022-08-18T08:57:41.588306Z","shell.execute_reply":"2022-08-18T08:57:41.774426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_bal.fillna(method='bfill',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:57:44.064375Z","iopub.execute_input":"2022-08-18T08:57:44.065904Z","iopub.status.idle":"2022-08-18T08:57:44.695879Z","shell.execute_reply.started":"2022-08-18T08:57:44.065855Z","shell.execute_reply":"2022-08-18T08:57:44.694289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_bal=df_bal.astype('float16')","metadata":{"execution":{"iopub.status.busy":"2022-08-18T05:42:10.713138Z","iopub.execute_input":"2022-08-18T05:42:10.713592Z","iopub.status.idle":"2022-08-18T05:42:12.187705Z","shell.execute_reply.started":"2022-08-18T05:42:10.713554Z","shell.execute_reply":"2022-08-18T05:42:12.186343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-08-18T08:57:50.745439Z","iopub.execute_input":"2022-08-18T08:57:50.747414Z","iopub.status.idle":"2022-08-18T08:57:50.757549Z","shell.execute_reply.started":"2022-08-18T08:57:50.747347Z","shell.execute_reply":"2022-08-18T08:57:50.755967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try to normalize data with min max scaler\nfrom sklearn.preprocessing import RobustScaler\nimport numpy as np\n  \n# copy the data\ndf_sklearn = df_bal.copy()\n  \n# apply normalization techniques\n\ndf_sklearn = pd.DataFrame(RobustScaler().fit_transform(df_sklearn))\n  \n","metadata":{"execution":{"iopub.status.busy":"2022-08-18T09:36:11.073707Z","iopub.execute_input":"2022-08-18T09:36:11.074431Z","iopub.status.idle":"2022-08-18T09:36:22.934130Z","shell.execute_reply.started":"2022-08-18T09:36:11.074386Z","shell.execute_reply":"2022-08-18T09:36:22.932125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[20, 7])\n# Cut the window in 2 parts\nind=range(36)\nfor col ,i in zip(df_bal.columns,ind):\n   \n   f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={\"height_ratios\": (.15, .85)})\n   \n# Add a graph in each part\n   sns.boxplot(df_bal[col], ax=ax_box,color='blue')\n   sns.boxplot(df_sklearn[i], ax=ax_box,color='red')\n   sns.distplot(df_bal[col], ax=ax_hist,color='blue')\n   sns.distplot(df_sklearn[i], ax=ax_hist,color='red')\n   plt.show()\nplt.savefig('figure.png',dpi = 1200)\n# Remove x axis name for the boxplot\n   #ax_box.set(xlabel=col)","metadata":{"execution":{"iopub.status.busy":"2022-08-19T04:42:18.338522Z","iopub.execute_input":"2022-08-19T04:42:18.339355Z","iopub.status.idle":"2022-08-19T04:42:18.439529Z","shell.execute_reply.started":"2022-08-19T04:42:18.339250Z","shell.execute_reply":"2022-08-19T04:42:18.437714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sklearn.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-18T06:57:02.751711Z","iopub.execute_input":"2022-08-18T06:57:02.752144Z","iopub.status.idle":"2022-08-18T06:57:02.775449Z","shell.execute_reply.started":"2022-08-18T06:57:02.752107Z","shell.execute_reply":"2022-08-18T06:57:02.770506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Variable  distribution after using robust scaler\nplt.figure(figsize=[20, 7])\n# Cut the window in 2 parts\nfor col in df_sklearn.columns:\n   \n   f, (ax_box, ax_hist) = plt.subplots(2, sharex=True, gridspec_kw={\"height_ratios\": (.15, .85)})\n   \n# Add a graph in each part\n   sns.boxplot(df_sklearn[col], ax=ax_box)\n   sns.distplot(df_sklearn[col], ax=ax_hist)\n   plt.show()\nplt.savefig('figure_aftertransformation.png',dpi = 1200)\n# Remove x axis name for the boxplot\n   #ax_box.set(xlabel=col)","metadata":{"execution":{"iopub.status.busy":"2022-08-18T06:57:19.149544Z","iopub.execute_input":"2022-08-18T06:57:19.149963Z","iopub.status.idle":"2022-08-18T07:07:35.989788Z","shell.execute_reply.started":"2022-08-18T06:57:19.149928Z","shell.execute_reply":"2022-08-18T07:07:35.988288Z"},"trusted":true},"execution_count":null,"outputs":[]}]}