{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## **Import Necessary Library**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\nfrom lightgbm import LGBMClassifier, early_stopping, log_evaluation\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import  OrdinalEncoder\n\nimport gc","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:21:20.863782Z","iopub.execute_input":"2022-06-24T10:21:20.864269Z","iopub.status.idle":"2022-06-24T10:21:24.740342Z","shell.execute_reply.started":"2022-06-24T10:21:20.864188Z","shell.execute_reply":"2022-06-24T10:21:24.739558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CONFIG:\n    random_state = 69420\n    kaggle = True\n    path = '../input/amexfeather/'\n    local_path = ''","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:21:33.814708Z","iopub.execute_input":"2022-06-24T10:21:33.815149Z","iopub.status.idle":"2022-06-24T10:21:33.820417Z","shell.execute_reply.started":"2022-06-24T10:21:33.815111Z","shell.execute_reply":"2022-06-24T10:21:33.819703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Data Preprocessing**","metadata":{}},{"cell_type":"code","source":"%%time\n\ntrain = pd.read_feather(CONFIG.path + 'train_data.ftr')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:21:34.543303Z","iopub.execute_input":"2022-06-24T10:21:34.544092Z","iopub.status.idle":"2022-06-24T10:21:55.141201Z","shell.execute_reply.started":"2022-06-24T10:21:34.544055Z","shell.execute_reply":"2022-06-24T10:21:55.140429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:21:55.142935Z","iopub.execute_input":"2022-06-24T10:21:55.143318Z","iopub.status.idle":"2022-06-24T10:21:55.148824Z","shell.execute_reply.started":"2022-06-24T10:21:55.143281Z","shell.execute_reply":"2022-06-24T10:21:55.147953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\n# Only keep last statement month per customer\n\n# train_data =  (train\n#             .groupby('customer_ID')\n#             .tail(1)\n#             .drop(['S_2'], axis=1)\n#             .set_index('customer_ID', inplace=True)\n# #             .sort_index()\n#             )","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:22:04.409453Z","iopub.execute_input":"2022-06-24T10:22:04.409919Z","iopub.status.idle":"2022-06-24T10:22:04.415205Z","shell.execute_reply.started":"2022-06-24T10:22:04.409875Z","shell.execute_reply":"2022-06-24T10:22:04.414396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.groupby('customer_ID')\ntrain = train.tail(1)\ntrain = train.drop(['S_2'], axis=1)\ntrain.set_index('customer_ID', inplace=True)\n            ","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:22:05.315482Z","iopub.execute_input":"2022-06-24T10:22:05.316301Z","iopub.status.idle":"2022-06-24T10:22:07.749855Z","shell.execute_reply.started":"2022-06-24T10:22:05.316267Z","shell.execute_reply":"2022-06-24T10:22:07.749026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"reference: https://www.kaggle.com/competitions/amex-default-prediction/discussion/327094","metadata":{}},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:22:11.196363Z","iopub.execute_input":"2022-06-24T10:22:11.196743Z","iopub.status.idle":"2022-06-24T10:22:11.323046Z","shell.execute_reply.started":"2022-06-24T10:22:11.196712Z","shell.execute_reply":"2022-06-24T10:22:11.322093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:22:12.730749Z","iopub.execute_input":"2022-06-24T10:22:12.731472Z","iopub.status.idle":"2022-06-24T10:22:12.736927Z","shell.execute_reply.started":"2022-06-24T10:22:12.731434Z","shell.execute_reply":"2022-06-24T10:22:12.736140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_cols = train.columns.to_list()\n\ncat_features = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\n\nnum_features = [col for col in total_cols if col not in cat_features + [\"target\"]]","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:22:53.770535Z","iopub.execute_input":"2022-06-24T10:22:53.772428Z","iopub.status.idle":"2022-06-24T10:22:53.786912Z","shell.execute_reply.started":"2022-06-24T10:22:53.772350Z","shell.execute_reply":"2022-06-24T10:22:53.785861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train[cat_features + num_features]\ny = train['target']\n\nx.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:24:30.584256Z","iopub.execute_input":"2022-06-24T10:24:30.584665Z","iopub.status.idle":"2022-06-24T10:24:30.962398Z","shell.execute_reply.started":"2022-06-24T10:24:30.584627Z","shell.execute_reply":"2022-06-24T10:24:30.961485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Apply OrdinalEncoder","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:37:48.213230Z","iopub.execute_input":"2022-06-24T10:37:48.213809Z","iopub.status.idle":"2022-06-24T10:37:48.217939Z","shell.execute_reply.started":"2022-06-24T10:37:48.213777Z","shell.execute_reply":"2022-06-24T10:37:48.216658Z"}}},{"cell_type":"code","source":"%%time\n\nenc = OrdinalEncoder()\nx[cat_features] = enc.fit_transform(x[cat_features])\n_ = gc.collect","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:40:28.999109Z","iopub.execute_input":"2022-06-24T10:40:28.999454Z","iopub.status.idle":"2022-06-24T10:40:29.757473Z","shell.execute_reply.started":"2022-06-24T10:40:28.999426Z","shell.execute_reply":"2022-06-24T10:40:29.756556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Competition Metrix**","metadata":{}},{"cell_type":"code","source":"def amex_metrix(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)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T10:58:26.547827Z","iopub.execute_input":"2022-06-24T10:58:26.548306Z","iopub.status.idle":"2022-06-24T10:58:26.563171Z","shell.execute_reply.started":"2022-06-24T10:58:26.548274Z","shell.execute_reply":"2022-06-24T10:58:26.562301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Model Training**","metadata":{}},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:46:36.150431Z","iopub.execute_input":"2022-06-24T11:46:36.151131Z","iopub.status.idle":"2022-06-24T11:46:37.592761Z","shell.execute_reply.started":"2022-06-24T11:46:36.151093Z","shell.execute_reply":"2022-06-24T11:46:37.591834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape, y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:46:37.594451Z","iopub.execute_input":"2022-06-24T11:46:37.594905Z","iopub.status.idle":"2022-06-24T11:46:37.601901Z","shell.execute_reply.started":"2022-06-24T11:46:37.594866Z","shell.execute_reply":"2022-06-24T11:46:37.600827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = LGBMClassifier(\n    n_estimators=50000,\n    device='gpu',\n    random_state=CONFIG.random_state,\n    extra_trees=True\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:47:35.450937Z","iopub.execute_input":"2022-06-24T11:47:35.451379Z","iopub.status.idle":"2022-06-24T11:47:35.456689Z","shell.execute_reply.started":"2022-06-24T11:47:35.451341Z","shell.execute_reply":"2022-06-24T11:47:35.455712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmodel.fit(\n    X_train, y_train, \n    eval_set=[(X_test,y_test)],\n    callbacks=[early_stopping(50), log_evaluation(0)]\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:48:20.513775Z","iopub.execute_input":"2022-06-24T11:48:20.514456Z","iopub.status.idle":"2022-06-24T11:48:58.397958Z","shell.execute_reply.started":"2022-06-24T11:48:20.514420Z","shell.execute_reply":"2022-06-24T11:48:58.397340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = pd.DataFrame(y_test.copy(deep=True))\ny_pred = y_pred.rename(columns={'target':'prediction'})\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:49:01.414289Z","iopub.execute_input":"2022-06-24T11:49:01.414810Z","iopub.status.idle":"2022-06-24T11:49:01.433392Z","shell.execute_reply.started":"2022-06-24T11:49:01.414765Z","shell.execute_reply":"2022-06-24T11:49:01.432640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ny_pred[\"prediction\"] = model.predict_proba(X_test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:49:13.958318Z","iopub.execute_input":"2022-06-24T11:49:13.958714Z","iopub.status.idle":"2022-06-24T11:49:18.396945Z","shell.execute_reply.started":"2022-06-24T11:49:13.958682Z","shell.execute_reply":"2022-06-24T11:49:18.396200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:49:20.368369Z","iopub.execute_input":"2022-06-24T11:49:20.368761Z","iopub.status.idle":"2022-06-24T11:49:20.378971Z","shell.execute_reply.started":"2022-06-24T11:49:20.368730Z","shell.execute_reply":"2022-06-24T11:49:20.378176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = pd.DataFrame(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:49:52.177458Z","iopub.execute_input":"2022-06-24T11:49:52.178324Z","iopub.status.idle":"2022-06-24T11:49:52.182670Z","shell.execute_reply.started":"2022-06-24T11:49:52.178288Z","shell.execute_reply":"2022-06-24T11:49:52.181765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\namex_metrix(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:50:32.080505Z","iopub.execute_input":"2022-06-24T11:50:32.080978Z","iopub.status.idle":"2022-06-24T11:50:32.554884Z","shell.execute_reply.started":"2022-06-24T11:50:32.080939Z","shell.execute_reply":"2022-06-24T11:50:32.553967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Submission**","metadata":{}},{"cell_type":"code","source":"del train, x, y, X_test, X_train, y_train, y_test, y_pred\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:51:08.944158Z","iopub.execute_input":"2022-06-24T11:51:08.944514Z","iopub.status.idle":"2022-06-24T11:51:09.257159Z","shell.execute_reply.started":"2022-06-24T11:51:08.944484Z","shell.execute_reply":"2022-06-24T11:51:09.256356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest = pd.read_feather(CONFIG.path+'test_data.ftr')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:51:24.407173Z","iopub.execute_input":"2022-06-24T11:51:24.407529Z","iopub.status.idle":"2022-06-24T11:52:09.175296Z","shell.execute_reply.started":"2022-06-24T11:51:24.407499Z","shell.execute_reply":"2022-06-24T11:52:09.174516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.groupby('customer_ID')\ntest = test.tail(1)\ntest = test.drop(['S_2'], axis=1)\ntest.set_index('customer_ID', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:52:29.929430Z","iopub.execute_input":"2022-06-24T11:52:29.930092Z","iopub.status.idle":"2022-06-24T11:52:34.696354Z","shell.execute_reply.started":"2022-06-24T11:52:29.930055Z","shell.execute_reply":"2022-06-24T11:52:34.695522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:52:36.389364Z","iopub.execute_input":"2022-06-24T11:52:36.389739Z","iopub.status.idle":"2022-06-24T11:52:36.671623Z","shell.execute_reply.started":"2022-06-24T11:52:36.389706Z","shell.execute_reply":"2022-06-24T11:52:36.670649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest[cat_features] = enc.transform(test[cat_features])\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:52:55.935944Z","iopub.execute_input":"2022-06-24T11:52:55.936316Z","iopub.status.idle":"2022-06-24T11:52:57.609870Z","shell.execute_reply.started":"2022-06-24T11:52:55.936285Z","shell.execute_reply":"2022-06-24T11:52:57.608682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"prediction\"] = model.predict_proba(test[cat_features + num_features])[:,1]\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:53:20.312133Z","iopub.execute_input":"2022-06-24T11:53:20.312496Z","iopub.status.idle":"2022-06-24T11:53:47.849070Z","shell.execute_reply.started":"2022-06-24T11:53:20.312467Z","shell.execute_reply":"2022-06-24T11:53:47.848293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"prediction\"].to_csv(\"submission.csv\", index=True)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-24T11:53:54.639958Z","iopub.execute_input":"2022-06-24T11:53:54.640387Z","iopub.status.idle":"2022-06-24T11:53:59.010440Z","shell.execute_reply.started":"2022-06-24T11:53:54.640351Z","shell.execute_reply":"2022-06-24T11:53:59.009621Z"},"trusted":true},"execution_count":null,"outputs":[]}]}