{"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\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom lightgbm import LGBMClassifier, early_stopping, log_evaluation\n\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-08T14:47:32.998495Z","iopub.execute_input":"2022-06-08T14:47:32.998940Z","iopub.status.idle":"2022-06-08T14:47:35.166918Z","shell.execute_reply.started":"2022-06-08T14:47:32.998853Z","shell.execute_reply":"2022-06-08T14:47:35.165806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntrain = pd.read_feather('../input/amexfeather/train_data.ftr')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:47:35.168638Z","iopub.execute_input":"2022-06-08T14:47:35.169477Z","iopub.status.idle":"2022-06-08T14:47:57.758723Z","shell.execute_reply.started":"2022-06-08T14:47:35.169432Z","shell.execute_reply":"2022-06-08T14:47:57.757883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:47:57.759672Z","iopub.execute_input":"2022-06-08T14:47:57.759954Z","iopub.status.idle":"2022-06-08T14:47:57.765505Z","shell.execute_reply.started":"2022-06-08T14:47:57.759930Z","shell.execute_reply":"2022-06-08T14:47:57.764589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train =  (train\n            .groupby('customer_ID')\n            .tail(1)\n            .set_index('customer_ID', drop=True)\n            .sort_index()\n            .drop(['S_2'], axis='columns'))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:47:57.767432Z","iopub.execute_input":"2022-06-08T14:47:57.767740Z","iopub.status.idle":"2022-06-08T14:48:00.661820Z","shell.execute_reply.started":"2022-06-08T14:47:57.767712Z","shell.execute_reply":"2022-06-08T14:48:00.661074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:00.662808Z","iopub.execute_input":"2022-06-08T14:48:00.663461Z","iopub.status.idle":"2022-06-08T14:48:00.669046Z","shell.execute_reply.started":"2022-06-08T14:48:00.663430Z","shell.execute_reply":"2022-06-08T14:48:00.668108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:00.670054Z","iopub.execute_input":"2022-06-08T14:48:00.670436Z","iopub.status.idle":"2022-06-08T14:48:00.797919Z","shell.execute_reply.started":"2022-06-08T14:48:00.670406Z","shell.execute_reply":"2022-06-08T14:48:00.796840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = train.columns.to_list()\ncategory_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nnumerical_cols = [col for col in cols if col not in category_cols + ['target']]","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:00.799024Z","iopub.execute_input":"2022-06-08T14:48:00.799508Z","iopub.status.idle":"2022-06-08T14:48:00.814553Z","shell.execute_reply.started":"2022-06-08T14:48:00.799478Z","shell.execute_reply":"2022-06-08T14:48:00.813870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train[category_cols + numerical_cols]\ny = train['target']\n\nX.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:00.815496Z","iopub.execute_input":"2022-06-08T14:48:00.816598Z","iopub.status.idle":"2022-06-08T14:48:01.121011Z","shell.execute_reply.started":"2022-06-08T14:48:00.816557Z","shell.execute_reply":"2022-06-08T14:48:01.120142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\n\nenc = OrdinalEncoder()\nX[category_cols] = enc.fit_transform(X[category_cols])\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:01.122302Z","iopub.execute_input":"2022-06-08T14:48:01.122934Z","iopub.status.idle":"2022-06-08T14:48:02.002686Z","shell.execute_reply.started":"2022-06-08T14:48:01.122892Z","shell.execute_reply":"2022-06-08T14:48:02.001622Z"},"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, stratify=y)\nX_train.shape, X_test.shape, y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:46:13.033740Z","iopub.execute_input":"2022-06-08T14:46:13.034181Z","iopub.status.idle":"2022-06-08T14:46:14.515924Z","shell.execute_reply.started":"2022-06-08T14:46:13.034137Z","shell.execute_reply":"2022-06-08T14:46:14.514859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\n\nxgb_cl = xgb.XGBClassifier()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:46:14.518684Z","iopub.execute_input":"2022-06-08T14:46:14.519158Z","iopub.status.idle":"2022-06-08T14:46:14.524048Z","shell.execute_reply.started":"2022-06-08T14:46:14.519113Z","shell.execute_reply":"2022-06-08T14:46:14.522890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit\neval_set = [(X_test, y_test)]\nxgb_cl.fit(X_train, y_train,early_stopping_rounds=50, eval_metric=\"logloss\", eval_set=eval_set, verbose=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:37:30.646312Z","iopub.execute_input":"2022-06-08T14:37:30.646721Z","iopub.status.idle":"2022-06-08T14:43:07.603649Z","shell.execute_reply.started":"2022-06-08T14:37:30.646688Z","shell.execute_reply":"2022-06-08T14:43:07.599242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nfilename = 'Xgboost.sav'\npickle.dump(xgb_cl, open(filename, 'wb'))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:43:43.429149Z","iopub.execute_input":"2022-06-08T14:43:43.430894Z","iopub.status.idle":"2022-06-08T14:43:43.445529Z","shell.execute_reply.started":"2022-06-08T14:43:43.430815Z","shell.execute_reply":"2022-06-08T14:43:43.444712Z"},"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-08T14:45:32.869113Z","iopub.execute_input":"2022-06-08T14:45:32.869499Z","iopub.status.idle":"2022-06-08T14:45:32.889932Z","shell.execute_reply.started":"2022-06-08T14:45:32.869468Z","shell.execute_reply":"2022-06-08T14:45:32.888724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred['prediction'] = xgb_cl.predict_proba(X_test)[:,1]\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:45:34.130870Z","iopub.execute_input":"2022-06-08T14:45:34.131542Z","iopub.status.idle":"2022-06-08T14:45:34.154497Z","shell.execute_reply.started":"2022-06-08T14:45:34.131505Z","shell.execute_reply":"2022-06-08T14:45:34.153228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = pd.DataFrame(y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:45:01.384170Z","iopub.execute_input":"2022-06-08T14:45:01.384598Z","iopub.status.idle":"2022-06-08T14:45:01.405854Z","shell.execute_reply.started":"2022-06-08T14:45:01.384565Z","shell.execute_reply":"2022-06-08T14:45:01.404561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:44:55.729750Z","iopub.execute_input":"2022-06-08T14:44:55.730616Z","iopub.status.idle":"2022-06-08T14:44:55.746813Z","shell.execute_reply.started":"2022-06-08T14:44:55.730576Z","shell.execute_reply":"2022-06-08T14:44:55.745739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\namex_metric(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:44:56.227918Z","iopub.execute_input":"2022-06-08T14:44:56.228348Z","iopub.status.idle":"2022-06-08T14:44:56.249555Z","shell.execute_reply.started":"2022-06-08T14:44:56.228308Z","shell.execute_reply":"2022-06-08T14:44:56.248590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08T14:44:09.368551Z","iopub.execute_input":"2022-06-08T14:44:09.368979Z","iopub.status.idle":"2022-06-08T14:44:09.576620Z","shell.execute_reply.started":"2022-06-08T14:44:09.368942Z","shell.execute_reply":"2022-06-08T14:44:09.575715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntest = pd.read_feather('../input/amexfeather/test_data.ftr')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:16.242830Z","iopub.execute_input":"2022-06-08T14:48:16.243213Z","iopub.status.idle":"2022-06-08T14:48:59.458777Z","shell.execute_reply.started":"2022-06-08T14:48:16.243182Z","shell.execute_reply":"2022-06-08T14:48:59.457808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test =  (\n    test\n    .groupby('customer_ID')\n    .tail(1)\n    .set_index('customer_ID', drop=True)\n    .sort_index()\n    .drop(['S_2'], axis='columns')\n)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:48:59.460497Z","iopub.execute_input":"2022-06-08T14:48:59.463019Z","iopub.status.idle":"2022-06-08T14:49:05.258231Z","shell.execute_reply.started":"2022-06-08T14:48:59.462986Z","shell.execute_reply":"2022-06-08T14:49:05.257289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest[category_cols] = enc.transform(test[category_cols])\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:49:05.259412Z","iopub.execute_input":"2022-06-08T14:49:05.259987Z","iopub.status.idle":"2022-06-08T14:49:06.578143Z","shell.execute_reply.started":"2022-06-08T14:49:05.259954Z","shell.execute_reply":"2022-06-08T14:49:06.577440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nloaded_model = pickle.load(open('../input/american-express-credit-xgboost-model/Xgboost.sav', 'rb'))","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:49:21.235476Z","iopub.execute_input":"2022-06-08T14:49:21.235913Z","iopub.status.idle":"2022-06-08T14:49:21.412718Z","shell.execute_reply.started":"2022-06-08T14:49:21.235880Z","shell.execute_reply":"2022-06-08T14:49:21.411868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict\ntest[\"prediction\"] = loaded_model.predict_proba(test)[:,1]","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:49:22.308821Z","iopub.execute_input":"2022-06-08T14:49:22.309623Z","iopub.status.idle":"2022-06-08T14:49:25.216039Z","shell.execute_reply.started":"2022-06-08T14:49:22.309576Z","shell.execute_reply":"2022-06-08T14:49:25.214898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"prediction\"].to_csv(\"submission.csv\", index=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-08T14:49:27.511069Z","iopub.execute_input":"2022-06-08T14:49:27.512083Z","iopub.status.idle":"2022-06-08T14:49:29.740038Z","shell.execute_reply.started":"2022-06-08T14:49:27.512041Z","shell.execute_reply":"2022-06-08T14:49:29.739272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}