{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing\nimport matplotlib.pyplot as plt # visualize the data\nimport seaborn as sns # visualize the data\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_selection import VarianceThreshold\nfrom sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier\nfrom sklearn.metrics import roc_curve, roc_auc_score, accuracy_score, confusion_matrix\nfrom sklearn.preprocessing import StandardScaler, OrdinalEncoder, LabelEncoder, OneHotEncoder\nfrom sklearn.compose import ColumnTransformer\nimport pickle \nimport lightgbm as lgb\n\nimport warnings\n\nwarnings.simplefilter('ignore')\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n%matplotlib inline","metadata":{"id":"e1decbe1","executionInfo":{"status":"ok","timestamp":1671735632411,"user_tz":-330,"elapsed":1819,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T10:41:48.365085Z","iopub.execute_input":"2023-01-22T10:41:48.365726Z","iopub.status.idle":"2023-01-22T10:41:51.448677Z","shell.execute_reply.started":"2023-01-22T10:41:48.365690Z","shell.execute_reply":"2023-01-22T10:41:51.447987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{"id":"xctEUjdpeSiU"}},{"cell_type":"code","source":"df_train = pd.read_feather('/kaggle/input/amexfeather/train_data.ftr')\ndf_train.drop(['target'], axis = 1, inplace = True)\ndf_train","metadata":{"executionInfo":{"elapsed":22696,"status":"ok","timestamp":1671735655105,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"561882ce","outputId":"3a25630d-e60f-41e7-9cdd-7feefa255dd0","execution":{"iopub.status.busy":"2023-01-22T10:41:51.450027Z","iopub.execute_input":"2023-01-22T10:41:51.450266Z","iopub.status.idle":"2023-01-22T10:42:08.970688Z","shell.execute_reply.started":"2023-01-22T10:41:51.450237Z","shell.execute_reply":"2023-01-22T10:42:08.969857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_feather('/kaggle/input/amexfeather/test_data.ftr')\ndf_test","metadata":{"id":"LuFcRpm5Ll_W","executionInfo":{"status":"ok","timestamp":1671735655106,"user_tz":-330,"elapsed":5,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T10:42:08.971772Z","iopub.execute_input":"2023-01-22T10:42:08.972005Z","iopub.status.idle":"2023-01-22T10:42:38.706366Z","shell.execute_reply.started":"2023-01-22T10:42:08.971959Z","shell.execute_reply":"2023-01-22T10:42:38.705590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = pd.read_csv('/kaggle/input/amex-default-prediction/train_labels.csv')\ntarget","metadata":{"executionInfo":{"elapsed":454,"status":"ok","timestamp":1671735655556,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"8RK4uGUX1tYC","outputId":"66a4e070-6c74-476e-97af-774591558522","execution":{"iopub.status.busy":"2023-01-22T10:42:38.708161Z","iopub.execute_input":"2023-01-22T10:42:38.708383Z","iopub.status.idle":"2023-01-22T10:42:39.442159Z","shell.execute_reply.started":"2023-01-22T10:42:38.708357Z","shell.execute_reply":"2023-01-22T10:42:39.441350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{"id":"q9gpGEhd2Alp"}},{"cell_type":"code","source":"print(df_train.shape)\nprint(df_test.shape)\nprint(target.shape)","metadata":{"executionInfo":{"elapsed":6,"status":"ok","timestamp":1671735655557,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"WvgKZQ6WU25M","outputId":"bc562ff1-4cea-4390-b3cf-a52398f17069","execution":{"iopub.status.busy":"2023-01-22T10:42:39.443357Z","iopub.execute_input":"2023-01-22T10:42:39.443614Z","iopub.status.idle":"2023-01-22T10:42:39.448667Z","shell.execute_reply.started":"2023-01-22T10:42:39.443587Z","shell.execute_reply":"2023-01-22T10:42:39.447887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"executionInfo":{"elapsed":971,"status":"ok","timestamp":1671735656524,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"F6qecMxoEKF3","outputId":"0ad68c84-b746-4a0e-e4ed-2ace6d62e540","execution":{"iopub.status.busy":"2023-01-22T10:42:39.449722Z","iopub.execute_input":"2023-01-22T10:42:39.449947Z","iopub.status.idle":"2023-01-22T10:42:40.440775Z","shell.execute_reply.started":"2023-01-22T10:42:39.449920Z","shell.execute_reply":"2023-01-22T10:42:40.440016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info(verbose=True, null_counts=True)","metadata":{"id":"e5c8105d","execution":{"iopub.status.busy":"2023-01-22T10:42:40.441974Z","iopub.execute_input":"2023-01-22T10:42:40.442236Z","iopub.status.idle":"2023-01-22T10:42:44.551126Z","shell.execute_reply.started":"2023-01-22T10:42:40.442204Z","shell.execute_reply":"2023-01-22T10:42:44.550305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.select_dtypes(include = ['object', 'category']).columns","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:42:44.552334Z","iopub.execute_input":"2023-01-22T10:42:44.552785Z","iopub.status.idle":"2023-01-22T10:42:44.669582Z","shell.execute_reply.started":"2023-01-22T10:42:44.552748Z","shell.execute_reply":"2023-01-22T10:42:44.668753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nnum_cols = []\nfor col in df_train.columns:\n    if col not in categorical_cols+['customer_ID','S_2']:\n        num_cols.append(col)\n\nnum_cols = np.array(num_cols)\nprint(\"categorical cols: \", categorical_cols)\nprint(\"numerical cols: \", num_cols)","metadata":{"executionInfo":{"elapsed":22,"status":"ok","timestamp":1671735659201,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"8ebb57d7","outputId":"cf1db901-ed5c-4ed2-f62e-60c933889319","execution":{"iopub.status.busy":"2023-01-22T10:42:44.670663Z","iopub.execute_input":"2023-01-22T10:42:44.670892Z","iopub.status.idle":"2023-01-22T10:42:44.692769Z","shell.execute_reply.started":"2023-01-22T10:42:44.670864Z","shell.execute_reply":"2023-01-22T10:42:44.692083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[categorical_cols]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:42:44.695495Z","iopub.execute_input":"2023-01-22T10:42:44.695726Z","iopub.status.idle":"2023-01-22T10:42:44.741369Z","shell.execute_reply.started":"2023-01-22T10:42:44.695699Z","shell.execute_reply":"2023-01-22T10:42:44.740681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def denoise(df):\n    df['D_63'] = df['D_63'].apply(lambda t: {'CR': 0, 'XZ': 1, 'XM': 2, 'CO': 3, 'CL': 4, 'XL': 5}[t]).astype(np.int8)\n    df['D_64'] = df['D_64'].apply(lambda t: {'O': 0, 'U': 3, 'R': 2, '': -1, '-1': 1}[t]).astype(np.int8)\n\n    return df\n\ndef variance(df):\n    df_temp = df.drop(['customer_ID','S_2'], axis=1)\n\n    var_thres = VarianceThreshold(threshold = 0)\n    var_thres.fit(df_temp)\n\n    constant_columns = [column for column in df_temp.columns if column not in df_temp.columns[var_thres.get_support()]]\n\n    df.drop(constant_columns, axis=1, inplace = True)\n    \n    return df\n\ndef null_details(threshold : np.int8 = 50, cols: np.ndarray = df_train.columns, df: pd.DataFrame = df_train) -> pd.DataFrame:\n    null_cols = df[cols].isnull().sum().sort_values()\n    df_null = pd.DataFrame(null_cols[null_cols > 0])\n    df_null[1] = df[cols].isnull().mean()*100\n\n    return df_null[df_null[1] > threshold]\n\ndef remove_cols(df):\n    return null_details(80, df.columns).index\n\ndef select_best_indeces(group: pd.DataFrame):\n    return group.isnull().sum(axis = 1).sort_values().head(1).index[0]\n\ndef select_rows(df_grouped):\n    return df_grouped.apply(select_best_indeces).values\n\ndef one_hot_encoding(df, cols, is_drop = True):\n    for col in cols:\n        print('one hot encoding:', col)\n        dummies = pd.get_dummies(pd.Series(df[col]), prefix =' oneHot_%s'%col, drop_first = True)\n        df = pd.concat([df, dummies], axis = 1)\n    if is_drop:\n        df.drop(cols, axis = 1, inplace = True)\n        \n    return df\n\ndef ordinal_encoding(df, cols):\n    enc = OrdinalEncoder()\n    df[cols] = enc.fit_transform(df[cols])\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:42:44.742476Z","iopub.execute_input":"2023-01-22T10:42:44.743045Z","iopub.status.idle":"2023-01-22T10:42:44.882903Z","shell.execute_reply.started":"2023-01-22T10:42:44.743010Z","shell.execute_reply":"2023-01-22T10:42:44.882163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_details(0, categorical_cols)","metadata":{"executionInfo":{"elapsed":15,"status":"ok","timestamp":1671735659202,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"},"user_tz":-330},"id":"I49QcffdSk_T","outputId":"0a3759f5-85e9-47a4-abea-f5b55d5dbe40","execution":{"iopub.status.busy":"2023-01-22T10:42:44.884001Z","iopub.execute_input":"2023-01-22T10:42:44.884397Z","iopub.status.idle":"2023-01-22T10:42:45.055568Z","shell.execute_reply.started":"2023-01-22T10:42:44.884365Z","shell.execute_reply":"2023-01-22T10:42:45.054937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.set_option('display.max_rows', None)\nnull_details(0, num_cols)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:42:45.056518Z","iopub.execute_input":"2023-01-22T10:42:45.056745Z","iopub.status.idle":"2023-01-22T10:42:54.412884Z","shell.execute_reply.started":"2023-01-22T10:42:45.056717Z","shell.execute_reply":"2023-01-22T10:42:54.412237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D_cols = df_train.columns[pd.Series(df_train.columns).str.startswith('D_')]\nB_cols = df_train.columns[pd.Series(df_train.columns).str.startswith('B_')]\nS_cols = df_train.columns[pd.Series(df_train.columns).str.startswith('S_')]\nR_cols = df_train.columns[pd.Series(df_train.columns).str.startswith('R_')]\nP_cols = df_train.columns[pd.Series(df_train.columns).str.startswith('P_')]\n\nDict = {'Delinquency': len(D_cols), 'Spend': len(S_cols), 'Payment': len(P_cols), 'Balance': len(B_cols), 'Risk': len(R_cols),}\n\nplt.figure(figsize=(10,5))\nsns.barplot(x=list(Dict.keys()), y=list(Dict.values()));\nplt.legend()","metadata":{"id":"nZ4gyuGK6SNF","executionInfo":{"status":"ok","timestamp":1671735675284,"user_tz":-330,"elapsed":427,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T10:42:54.414030Z","iopub.execute_input":"2023-01-22T10:42:54.414285Z","iopub.status.idle":"2023-01-22T10:42:54.625113Z","shell.execute_reply.started":"2023-01-22T10:42:54.414254Z","shell.execute_reply":"2023-01-22T10:42:54.624448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_df = df_train.dropna(how='all').groupby(\"customer_ID\")","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:42:54.626025Z","iopub.execute_input":"2023-01-22T10:42:54.626219Z","iopub.status.idle":"2023-01-22T10:43:02.520700Z","shell.execute_reply.started":"2023-01-22T10:42:54.626194Z","shell.execute_reply":"2023-01-22T10:43:02.519954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_indices = select_rows(grouped_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:43:02.521731Z","iopub.execute_input":"2023-01-22T10:43:02.521944Z","iopub.status.idle":"2023-01-22T10:58:48.150464Z","shell.execute_reply.started":"2023-01-22T10:43:02.521918Z","shell.execute_reply":"2023-01-22T10:58:48.149364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.loc[selected_indices, :]\ndf_train","metadata":{"id":"XZxMeH7a5T9r","executionInfo":{"status":"ok","timestamp":1671736184108,"user_tz":-330,"elapsed":9,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"outputId":"978fce42-ec0f-4b65-afc6-0a7d8bb67b3a","execution":{"iopub.status.busy":"2023-01-22T10:58:48.151857Z","iopub.execute_input":"2023-01-22T10:58:48.152140Z","iopub.status.idle":"2023-01-22T10:58:48.829029Z","shell.execute_reply.started":"2023-01-22T10:58:48.152107Z","shell.execute_reply":"2023-01-22T10:58:48.828295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"id":"25e6VELnTxIM","executionInfo":{"status":"ok","timestamp":1671736189525,"user_tz":-330,"elapsed":3382,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"outputId":"8fef4b4c-42f3-4702-e114-83bf5aff44a9","execution":{"iopub.status.busy":"2023-01-22T10:58:48.830099Z","iopub.execute_input":"2023-01-22T10:58:48.830316Z","iopub.status.idle":"2023-01-22T10:59:00.135656Z","shell.execute_reply.started":"2023-01-22T10:58:48.830288Z","shell.execute_reply":"2023-01-22T10:59:00.134944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_details(0, df_train.columns, df_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:00.136851Z","iopub.execute_input":"2023-01-22T10:59:00.137257Z","iopub.status.idle":"2023-01-22T10:59:01.124700Z","shell.execute_reply.started":"2023-01-22T10:59:00.137221Z","shell.execute_reply":"2023-01-22T10:59:01.123750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = denoise(df_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:01.125763Z","iopub.execute_input":"2023-01-22T10:59:01.125966Z","iopub.status.idle":"2023-01-22T10:59:01.137298Z","shell.execute_reply.started":"2023-01-22T10:59:01.125940Z","shell.execute_reply":"2023-01-22T10:59:01.136546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"removeble_cols = list(remove_cols(df_train))\nremoveble_cols.append('S_2')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:01.138281Z","iopub.execute_input":"2023-01-22T10:59:01.138503Z","iopub.status.idle":"2023-01-22T10:59:12.032286Z","shell.execute_reply.started":"2023-01-22T10:59:01.138477Z","shell.execute_reply":"2023-01-22T10:59:12.031294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"removeble_cols","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:12.033400Z","iopub.execute_input":"2023-01-22T10:59:12.033658Z","iopub.status.idle":"2023-01-22T10:59:12.039212Z","shell.execute_reply.started":"2023-01-22T10:59:12.033628Z","shell.execute_reply":"2023-01-22T10:59:12.038586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df_train.iloc[:, 2:].columns:\n    if col not in removeble_cols:\n        if col in categorical_cols:\n            df_train[col] = df_train[col].fillna(df_train[col].mode()[0])\n        else:\n            df_train[col] = df_train[col].fillna(df_train[col].median())\n\nnull_details(0, df_train.columns, df_train)","metadata":{"id":"uRVkVz7TDAD6","executionInfo":{"status":"ok","timestamp":1671737128763,"user_tz":-330,"elapsed":1409,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"outputId":"9ecd0d91-46c7-4c58-d8b3-0240954d7f3b","execution":{"iopub.status.busy":"2023-01-22T10:59:12.040355Z","iopub.execute_input":"2023-01-22T10:59:12.040580Z","iopub.status.idle":"2023-01-22T10:59:15.188146Z","shell.execute_reply.started":"2023-01-22T10:59:12.040553Z","shell.execute_reply":"2023-01-22T10:59:15.187416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:15.189244Z","iopub.execute_input":"2023-01-22T10:59:15.189477Z","iopub.status.idle":"2023-01-22T10:59:15.319933Z","shell.execute_reply.started":"2023-01-22T10:59:15.189447Z","shell.execute_reply":"2023-01-22T10:59:15.319155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA on Test Data","metadata":{}},{"cell_type":"code","source":"nans = null_details(0, df_test.columns, df_test)\nprint(nans)\nprint(nans.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:15.321000Z","iopub.execute_input":"2023-01-22T10:59:15.321200Z","iopub.status.idle":"2023-01-22T10:59:37.670590Z","shell.execute_reply.started":"2023-01-22T10:59:15.321173Z","shell.execute_reply":"2023-01-22T10:59:37.669666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_df_test = df_test.dropna(how='all').groupby(\"customer_ID\")","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:37.671817Z","iopub.execute_input":"2023-01-22T10:59:37.672064Z","iopub.status.idle":"2023-01-22T10:59:54.194918Z","shell.execute_reply.started":"2023-01-22T10:59:37.672034Z","shell.execute_reply":"2023-01-22T10:59:54.194027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows_indices = select_rows(grouped_df_test)\nrows_indices","metadata":{"execution":{"iopub.status.busy":"2023-01-22T10:59:54.196073Z","iopub.execute_input":"2023-01-22T10:59:54.196285Z","iopub.status.idle":"2023-01-22T11:31:32.261303Z","shell.execute_reply.started":"2023-01-22T10:59:54.196257Z","shell.execute_reply":"2023-01-22T11:31:32.260588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = df_test.loc[rows_indices, :]\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:32.266954Z","iopub.execute_input":"2023-01-22T11:31:32.267386Z","iopub.status.idle":"2023-01-22T11:31:33.629552Z","shell.execute_reply.started":"2023-01-22T11:31:32.267353Z","shell.execute_reply":"2023-01-22T11:31:33.628819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_details(0, df_test.columns, df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:33.630517Z","iopub.execute_input":"2023-01-22T11:31:33.631057Z","iopub.status.idle":"2023-01-22T11:31:35.574262Z","shell.execute_reply.started":"2023-01-22T11:31:33.631026Z","shell.execute_reply":"2023-01-22T11:31:35.573481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = denoise(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:35.575406Z","iopub.execute_input":"2023-01-22T11:31:35.575669Z","iopub.status.idle":"2023-01-22T11:31:35.592105Z","shell.execute_reply.started":"2023-01-22T11:31:35.575637Z","shell.execute_reply":"2023-01-22T11:31:35.591519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df_test.iloc[:, 2:].columns:\n    if col not in removeble_cols:\n        if col in categorical_cols:\n            df_test[col] = df_test[col].fillna(df_test[col].mode()[0])\n        else:\n            df_test[col] = df_test[col].fillna(df_test[col].median())\n\nnull_details(0, df_test.columns, df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:35.592937Z","iopub.execute_input":"2023-01-22T11:31:35.593131Z","iopub.status.idle":"2023-01-22T11:31:42.073390Z","shell.execute_reply.started":"2023-01-22T11:31:35.593106Z","shell.execute_reply":"2023-01-22T11:31:42.072732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test['D_68'] = pd.Categorical(df_test['D_68'], categories=[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0], ordered=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:42.074418Z","iopub.execute_input":"2023-01-22T11:31:42.074939Z","iopub.status.idle":"2023-01-22T11:31:42.083832Z","shell.execute_reply.started":"2023-01-22T11:31:42.074903Z","shell.execute_reply":"2023-01-22T11:31:42.083276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:42.084779Z","iopub.execute_input":"2023-01-22T11:31:42.085012Z","iopub.status.idle":"2023-01-22T11:31:42.366667Z","shell.execute_reply.started":"2023-01-22T11:31:42.084983Z","shell.execute_reply":"2023-01-22T11:31:42.365930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handle Categorical Data","metadata":{}},{"cell_type":"code","source":"for col in ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68']:\n    le = LabelEncoder()\n    le.fit(df_train[col])\n    print('*****')\n    print(col)\n    print(le.classes_)\n    print(df_test[col].unique())\n    print(df_train[col].unique())\n    print('*****')\n    df_train[col] = le.transform(df_train[col])\n    df_test[col] = le.transform(df_test[col])\ndf_train[['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68']]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:42.367673Z","iopub.execute_input":"2023-01-22T11:31:42.367889Z","iopub.status.idle":"2023-01-22T11:31:43.144168Z","shell.execute_reply.started":"2023-01-22T11:31:42.367862Z","shell.execute_reply":"2023-01-22T11:31:43.143568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ct = ColumnTransformer([('oneHot', OneHotEncoder(drop='first'), ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_68'])], remainder = 'passthrough')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:43.145160Z","iopub.execute_input":"2023-01-22T11:31:43.145387Z","iopub.status.idle":"2023-01-22T11:31:43.149865Z","shell.execute_reply.started":"2023-01-22T11:31:43.145359Z","shell.execute_reply":"2023-01-22T11:31:43.149249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_matrix = ct.fit_transform(df_train)\ntest_matrix = ct.transform(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:31:43.150700Z","iopub.execute_input":"2023-01-22T11:31:43.150935Z","iopub.status.idle":"2023-01-22T11:32:19.787346Z","shell.execute_reply.started":"2023-01-22T11:31:43.150910Z","shell.execute_reply":"2023-01-22T11:32:19.786471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_features = ct.get_feature_names()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:32:19.788648Z","iopub.execute_input":"2023-01-22T11:32:19.788902Z","iopub.status.idle":"2023-01-22T11:32:19.793063Z","shell.execute_reply.started":"2023-01-22T11:32:19.788871Z","shell.execute_reply":"2023-01-22T11:32:19.792428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_matrix.dtype","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:32:19.793978Z","iopub.execute_input":"2023-01-22T11:32:19.794189Z","iopub.status.idle":"2023-01-22T11:32:19.805482Z","shell.execute_reply.started":"2023-01-22T11:32:19.794164Z","shell.execute_reply":"2023-01-22T11:32:19.804842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.DataFrame(train_matrix, columns = new_features)\ndf_train.set_index('customer_ID', drop = True, inplace = True)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:32:19.806363Z","iopub.execute_input":"2023-01-22T11:32:19.806558Z","iopub.status.idle":"2023-01-22T11:32:31.670310Z","shell.execute_reply.started":"2023-01-22T11:32:19.806534Z","shell.execute_reply":"2023-01-22T11:32:31.669245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.DataFrame(test_matrix, columns = new_features)\ndf_test.set_index('customer_ID', drop = True, inplace = True)\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:32:31.671457Z","iopub.execute_input":"2023-01-22T11:32:31.671661Z","iopub.status.idle":"2023-01-22T11:32:49.962145Z","shell.execute_reply.started":"2023-01-22T11:32:31.671635Z","shell.execute_reply":"2023-01-22T11:32:49.961308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Featuture Selection","metadata":{}},{"cell_type":"code","source":"df_train.loc[:, pd.Series(df_train.columns).str.startswith('oneHot').values] = df_train.loc[:, pd.Series(df_train.columns).str.startswith('oneHot').values].astype('int8')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:32:49.963482Z","iopub.execute_input":"2023-01-22T11:32:49.963701Z","iopub.status.idle":"2023-01-22T11:33:44.945911Z","shell.execute_reply.started":"2023-01-22T11:32:49.963675Z","shell.execute_reply":"2023-01-22T11:33:44.945035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[:, list(df_train.select_dtypes(include=['object']).columns)] = df_train.loc[:, list(df_train.select_dtypes(include=['object']).columns)].astype('float64')","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:33:44.947159Z","iopub.execute_input":"2023-01-22T11:33:44.947404Z","iopub.status.idle":"2023-01-22T11:36:22.938827Z","shell.execute_reply.started":"2023-01-22T11:33:44.947374Z","shell.execute_reply":"2023-01-22T11:36:22.938105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.dtypes.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:36:22.939921Z","iopub.execute_input":"2023-01-22T11:36:22.940155Z","iopub.status.idle":"2023-01-22T11:36:22.947766Z","shell.execute_reply.started":"2023-01-22T11:36:22.940127Z","shell.execute_reply":"2023-01-22T11:36:22.947225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cor_matrix = df_train.corr()\ncol_core = set()\n\nfor i in range(len(cor_matrix.columns)):\n    for j in range(i):\n        if(cor_matrix.iloc[i, j] > 0.9):\n            col_name = cor_matrix.columns[i]\n            col_core.add(col_name)\n            \ncol_core","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:36:22.948564Z","iopub.execute_input":"2023-01-22T11:36:22.948759Z","iopub.status.idle":"2023-01-22T11:37:05.945798Z","shell.execute_reply.started":"2023-01-22T11:36:22.948735Z","shell.execute_reply":"2023-01-22T11:37:05.945015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\nsns.heatmap(cor_matrix, cmap = plt.cm.Accent_r, annot = True)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:37:05.947038Z","iopub.execute_input":"2023-01-22T11:37:05.947254Z","iopub.status.idle":"2023-01-22T11:40:11.367045Z","shell.execute_reply.started":"2023-01-22T11:37:05.947228Z","shell.execute_reply":"2023-01-22T11:40:11.366371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dropable_cols = np.concatenate((removeble_cols, list(col_core)))\ndropable_cols","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:11.368106Z","iopub.execute_input":"2023-01-22T11:40:11.368307Z","iopub.status.idle":"2023-01-22T11:40:11.374272Z","shell.execute_reply.started":"2023-01-22T11:40:11.368280Z","shell.execute_reply":"2023-01-22T11:40:11.373654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.drop(dropable_cols, axis = 1, inplace = True)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:11.375110Z","iopub.execute_input":"2023-01-22T11:40:11.375315Z","iopub.status.idle":"2023-01-22T11:40:12.253758Z","shell.execute_reply.started":"2023-01-22T11:40:11.375290Z","shell.execute_reply":"2023-01-22T11:40:12.252983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.drop(dropable_cols, axis = 1, inplace = True)\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:12.254837Z","iopub.execute_input":"2023-01-22T11:40:12.255166Z","iopub.status.idle":"2023-01-22T11:40:25.515633Z","shell.execute_reply.started":"2023-01-22T11:40:12.255130Z","shell.execute_reply":"2023-01-22T11:40:25.514902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{"id":"zDZ2aqiuk7Jd"}},{"cell_type":"code","source":"target.set_index('customer_ID', drop = True, inplace = True)\nmerged_df = pd.concat([df_train, target], axis = 1)\nmerged_df","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:25.516669Z","iopub.execute_input":"2023-01-22T11:40:25.516898Z","iopub.status.idle":"2023-01-22T11:40:26.256306Z","shell.execute_reply.started":"2023-01-22T11:40:25.516853Z","shell.execute_reply":"2023-01-22T11:40:26.255677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nX = merged_df.drop('target', axis = 1)\ny = merged_df['target']\nX","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:26.257347Z","iopub.execute_input":"2023-01-22T11:40:26.257569Z","iopub.status.idle":"2023-01-22T11:40:26.711770Z","shell.execute_reply.started":"2023-01-22T11:40:26.257542Z","shell.execute_reply":"2023-01-22T11:40:26.711127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)","metadata":{"id":"c62fbef0","executionInfo":{"status":"ok","timestamp":1671737829484,"user_tz":-330,"elapsed":851,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T11:40:26.712727Z","iopub.execute_input":"2023-01-22T11:40:26.712931Z","iopub.status.idle":"2023-01-22T11:40:27.714473Z","shell.execute_reply.started":"2023-01-22T11:40:26.712906Z","shell.execute_reply":"2023-01-22T11:40:27.713688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_test_scaled = scaler.transform(X_test)\ndf_test_scaled = scaler.transform(df_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:27.715604Z","iopub.execute_input":"2023-01-22T11:40:27.715818Z","iopub.status.idle":"2023-01-22T11:40:39.172144Z","shell.execute_reply.started":"2023-01-22T11:40:27.715794Z","shell.execute_reply":"2023-01-22T11:40:39.171434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_scaled","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:39.173196Z","iopub.execute_input":"2023-01-22T11:40:39.173431Z","iopub.status.idle":"2023-01-22T11:40:39.179696Z","shell.execute_reply.started":"2023-01-22T11:40:39.173404Z","shell.execute_reply":"2023-01-22T11:40:39.179024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ran_clf = RandomForestClassifier(n_estimators = 10, max_depth = 2, random_state=0)\nada_clf = AdaBoostClassifier()\ngbm_clf = GradientBoostingClassifier()","metadata":{"id":"b495f6a8","executionInfo":{"status":"ok","timestamp":1671737829485,"user_tz":-330,"elapsed":3,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T11:40:39.180716Z","iopub.execute_input":"2023-01-22T11:40:39.180937Z","iopub.status.idle":"2023-01-22T11:40:39.189754Z","shell.execute_reply.started":"2023-01-22T11:40:39.180908Z","shell.execute_reply":"2023-01-22T11:40:39.189195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d_train = lgb.Dataset(data = X_train_scaled, label = y_train)\n\nparams = {'objective': 'binary','n_estimators': 1200,'metric': 'binary_logloss','boosting': 'gbdt','num_leaves': 90,'reg_lambda' : 50,'colsample_bytree': 0.19,'learning_rate': 0.03,'min_child_samples': 2400,'max_bins': 511,'seed': 42,'verbose': -1}\n\nlgb_clf = lgb.train(params, d_train, 100)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:40:39.190685Z","iopub.execute_input":"2023-01-22T11:40:39.190884Z","iopub.status.idle":"2023-01-22T11:42:07.155057Z","shell.execute_reply.started":"2023-01-22T11:40:39.190860Z","shell.execute_reply":"2023-01-22T11:42:07.154055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = [ran_clf,ada_clf,gbm_clf]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T11:42:07.156331Z","iopub.execute_input":"2023-01-22T11:42:07.156577Z","iopub.status.idle":"2023-01-22T11:42:07.160579Z","shell.execute_reply.started":"2023-01-22T11:42:07.156546Z","shell.execute_reply":"2023-01-22T11:42:07.159839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=[]\npred_prob = []\n\nfor model in [ran_clf,ada_clf,gbm_clf]:\n    model.fit(X_train_scaled, y_train)\n    pred.append(model.predict(X_test_scaled))\n    pred_prob.append(model.predict_proba(X_test_scaled))","metadata":{"id":"6ri0hNv3zQSH","executionInfo":{"status":"ok","timestamp":1671737917632,"user_tz":-330,"elapsed":5956,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"outputId":"2bfa8a03-6406-4e7f-e4fe-b5b49b0f705b","execution":{"iopub.status.busy":"2023-01-22T11:42:07.161571Z","iopub.execute_input":"2023-01-22T11:42:07.161755Z","iopub.status.idle":"2023-01-22T12:08:52.332805Z","shell.execute_reply.started":"2023-01-22T11:42:07.161731Z","shell.execute_reply":"2023-01-22T12:08:52.331816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicts = [pd.Series(x) for x in pred]\nprobabilities = [pd.DataFrame(x, columns = [0, 1]) for x in pred_prob]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:24:38.730665Z","iopub.execute_input":"2023-01-22T12:24:38.731362Z","iopub.status.idle":"2023-01-22T12:24:38.736318Z","shell.execute_reply.started":"2023-01-22T12:24:38.731329Z","shell.execute_reply":"2023-01-22T12:24:38.735658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_pred = lgb_clf.predict(X_test_scaled)\nlgb_pred","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:24:38.737497Z","iopub.execute_input":"2023-01-22T12:24:38.737693Z","iopub.status.idle":"2023-01-22T12:24:39.309912Z","shell.execute_reply.started":"2023-01-22T12:24:38.737670Z","shell.execute_reply":"2023-01-22T12:24:39.299133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicts.append(pd.Series(lgb_pred))\n# probabilities.append(pd.DataFrame(lgb_prob, columns = [0, 1]))","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:24:39.311077Z","iopub.execute_input":"2023-01-22T12:24:39.311573Z","iopub.status.idle":"2023-01-22T12:24:39.314757Z","shell.execute_reply.started":"2023-01-22T12:24:39.311543Z","shell.execute_reply":"2023-01-22T12:24:39.314179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = pd.concat(predicts, axis = 1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:24:39.316126Z","iopub.execute_input":"2023-01-22T12:24:39.316421Z","iopub.status.idle":"2023-01-22T12:24:39.335248Z","shell.execute_reply.started":"2023-01-22T12:24:39.316395Z","shell.execute_reply":"2023-01-22T12:24:39.334581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prob = pd.concat(probabilities, axis = 1)\ny_prob","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:24:39.336254Z","iopub.execute_input":"2023-01-22T12:24:39.336652Z","iopub.status.idle":"2023-01-22T12:24:39.354425Z","shell.execute_reply.started":"2023-01-22T12:24:39.336620Z","shell.execute_reply":"2023-01-22T12:24:39.353804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AMEX Metric","metadata":{"id":"FA7Hl7xrl9Dn"}},{"cell_type":"code","source":"def amex_metric(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n\n    def top_four_percent_captured(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        df = (pd.concat([y_true, y_pred], axis='columns')\n              .sort_values('prediction', ascending=False))\n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        four_pct_cutoff = int(0.04 * df['weight'].sum())\n        df['weight_cumsum'] = df['weight'].cumsum()\n        df_cutoff = df.loc[df['weight_cumsum'] <= four_pct_cutoff]\n        return (df_cutoff['target'] == 1).sum() / (df['target'] == 1).sum()\n\n    def weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        df = (pd.concat([y_true, y_pred], axis='columns')\n              .sort_values('prediction', ascending=False))\n        df['weight'] = df['target'].apply(lambda x: 20 if x==0 else 1)\n        df['random'] = (df['weight'] / df['weight'].sum()).cumsum()\n        total_pos = (df['target'] * df['weight']).sum()\n        df['cum_pos_found'] = (df['target'] * df['weight']).cumsum()\n        df['lorentz'] = df['cum_pos_found'] / total_pos\n        df['gini'] = (df['lorentz'] - df['random']) * df['weight']\n        return df['gini'].sum()\n\n    def normalized_weighted_gini(y_true: pd.DataFrame, y_pred: pd.DataFrame) -> float:\n        y_true_pred = y_true.rename(columns={'target': 'prediction'})\n        return weighted_gini(y_true, y_pred) / weighted_gini(y_true, y_true_pred)\n\n    g = normalized_weighted_gini(y_true, y_pred)\n    d = top_four_percent_captured(y_true, y_pred)\n\n    return 0.5 * (g + d)\n\ndef amex_metric_mod(y_true, y_pred):\n\n    labels     = np.transpose(np.array([y_true, y_pred]))\n    labels     = labels[labels[:, 1].argsort()[::-1]]\n    weights    = np.where(labels[:,0]==0, 20, 1)\n    cut_vals   = labels[np.cumsum(weights) <= int(0.04 * np.sum(weights))]\n    top_four   = np.sum(cut_vals[:,0]) / np.sum(labels[:,0])\n\n    gini = [0,0]\n    for i in [1,0]:\n        labels         = np.transpose(np.array([y_true, y_pred]))\n        labels         = labels[labels[:, i].argsort()[::-1]]\n        weight         = np.where(labels[:,0]==0, 20, 1)\n        weight_random  = np.cumsum(weight / np.sum(weight))\n        total_pos      = np.sum(labels[:, 0] *  weight)\n        cum_pos_found  = np.cumsum(labels[:, 0] * weight)\n        lorentz        = cum_pos_found / total_pos\n        gini[i]        = np.sum((lorentz - weight_random) * weight)\n\n    return 0.5 * (gini[1]/gini[0] + top_four)","metadata":{"id":"g5rVSoLvmASu","executionInfo":{"status":"ok","timestamp":1671737948109,"user_tz":-330,"elapsed":461,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T12:24:39.355411Z","iopub.execute_input":"2023-01-22T12:24:39.355629Z","iopub.status.idle":"2023-01-22T12:24:39.377441Z","shell.execute_reply.started":"2023-01-22T12:24:39.355601Z","shell.execute_reply":"2023-01-22T12:24:39.376820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Perfomance Matices","metadata":{"id":"8iAFWY3FnNZX"}},{"cell_type":"code","source":"def plot_roc_curve(fpr, tpr):\n    plt.plot(fpr, tpr, color='orange', label='ROC')\n    plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--', label='Default')\n    plt.xlabel('False Positive Rate')\n    plt.ylabel('True Positive Rate')\n    plt.title('Receiver Operating Characteristic (ROC) Curve')\n    plt.legend()\n    plt.show()","metadata":{"id":"pAsvoXRQnSwn","executionInfo":{"status":"ok","timestamp":1671737951195,"user_tz":-330,"elapsed":523,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T12:24:39.378341Z","iopub.execute_input":"2023-01-22T12:24:39.378552Z","iopub.status.idle":"2023-01-22T12:24:39.390587Z","shell.execute_reply.started":"2023-01-22T12:24:39.378522Z","shell.execute_reply":"2023-01-22T12:24:39.389932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.loc[:, [3]]","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:25:11.208066Z","iopub.execute_input":"2023-01-22T12:25:11.208604Z","iopub.status.idle":"2023-01-22T12:25:11.219855Z","shell.execute_reply.started":"2023-01-22T12:25:11.208572Z","shell.execute_reply":"2023-01-22T12:25:11.219203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgb_clf.params","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:41:00.198244Z","iopub.execute_input":"2023-01-22T12:41:00.198881Z","iopub.status.idle":"2023-01-22T12:41:00.203811Z","shell.execute_reply.started":"2023-01-22T12:41:00.198851Z","shell.execute_reply":"2023-01-22T12:41:00.203142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,model in enumerate(['RandomForestClassifier', 'AdaBoostClassifier', 'GradientBoostClassifier']):\n    print(f\"{model} Acuracy : {accuracy_score(y_test, y_pred.loc[:, [i]])}\")","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:27:12.199764Z","iopub.execute_input":"2023-01-22T12:27:12.200490Z","iopub.status.idle":"2023-01-22T12:27:12.225784Z","shell.execute_reply.started":"2023-01-22T12:27:12.200432Z","shell.execute_reply":"2023-01-22T12:27:12.225084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,model in zip([1,3,5],['RandomForestClassifier', 'AdaBoostClassifier', 'GradientBoostClassifier']):\n    print(f\"{model} Amex Metric : {amex_metric_mod(y_test.values, y_prob.iloc[:, [i]].values.flatten())}\")\nprint(f\"LightGBM Amex Metric : {amex_metric_mod(y_test.values,lgb_pred)}\")","metadata":{"id":"2dknOw36n-8w","executionInfo":{"status":"ok","timestamp":1671737952561,"user_tz":-330,"elapsed":918,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"outputId":"48f0890e-72fc-4367-819a-701737c6d0bf","execution":{"iopub.status.busy":"2023-01-22T12:31:28.772109Z","iopub.execute_input":"2023-01-22T12:31:28.772683Z","iopub.status.idle":"2023-01-22T12:31:28.893417Z","shell.execute_reply.started":"2023-01-22T12:31:28.772650Z","shell.execute_reply":"2023-01-22T12:31:28.892766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16, 9))\nfor model, name in zip(models, ['RandomForestClassifier', 'AdaBoostClassifier', 'GradientBoostClassifier']):\n    y_pred = model.predict_proba(X_test_scaled)[:,1]\n    fpr, tpr, _ = roc_curve(y_test, y_pred)\n    roc_auc = roc_auc_score(y_test, y_pred)\n    plt.plot(fpr, tpr, label='{} (AUC = {:.4f})'.format(name ,roc_auc))\nfpr, tpr, _ = roc_curve(y_test, lgb_pred)\nroc_auc = roc_auc_score(y_test, lgb_pred)\nplt.plot(fpr, tpr, label='{} (AUC = {:.4f})'.format(\"LightGBM\" ,roc_auc))\nplt.xlabel('False Positive Rate (FPR)')\nplt.ylabel('True Positive Rate (TPR)')\nplt.title('Receiver Operating Characteristic (ROC) Curve')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:31:42.379955Z","iopub.execute_input":"2023-01-22T12:31:42.380727Z","iopub.status.idle":"2023-01-22T12:31:45.094953Z","shell.execute_reply.started":"2023-01-22T12:31:42.380684Z","shell.execute_reply":"2023-01-22T12:31:45.094236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"id":"5TGhjiRqKMWU"}},{"cell_type":"code","source":"predict_prob = lgb_clf.predict(df_test_scaled)#[:, 1].reshape(-1,1).flatten()\npredict_prob","metadata":{"id":"HzsfIGZUKz29","executionInfo":{"status":"aborted","timestamp":1671736191177,"user_tz":-330,"elapsed":16,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predict_prob)","metadata":{"execution":{"iopub.status.busy":"2023-01-22T12:32:30.765790Z","iopub.execute_input":"2023-01-22T12:32:30.766354Z","iopub.status.idle":"2023-01-22T12:32:30.771017Z","shell.execute_reply.started":"2023-01-22T12:32:30.766321Z","shell.execute_reply":"2023-01-22T12:32:30.770257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_dataset = pd.read_csv('/kaggle/input/amex-default-prediction/sample_submission.csv')\noutput = pd.DataFrame({'customer_ID': df_test.index, 'prediction': predict_prob})\noutput.to_csv('/kaggle/working/my_submission_7.zip', index = False, compression = 'zip')","metadata":{"id":"AvTXhnebLQ4d","executionInfo":{"status":"aborted","timestamp":1671736191178,"user_tz":-330,"elapsed":16,"user":{"displayName":"Sathulakjan Thayaparan","userId":"05850266884036254021"}},"execution":{"iopub.status.busy":"2023-01-22T12:32:32.726317Z","iopub.execute_input":"2023-01-22T12:32:32.727078Z","iopub.status.idle":"2023-01-22T12:32:44.746661Z","shell.execute_reply.started":"2023-01-22T12:32:32.727040Z","shell.execute_reply":"2023-01-22T12:32:44.745645Z"},"trusted":true},"execution_count":null,"outputs":[]}]}