{"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":"# If it is useful, please vote","metadata":{}},{"cell_type":"markdown","source":"Add features referring to the following\nhttps://www.kaggle.com/code/manavtrivedi/tuffline-plotly-amex?scriptVersionId=102868130","metadata":{}},{"cell_type":"markdown","source":"# **Import**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n%matplotlib inline\nimport random\n\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:28:36.075812Z","iopub.execute_input":"2022-08-17T18:28:36.076336Z","iopub.status.idle":"2022-08-17T18:28:36.838828Z","shell.execute_reply.started":"2022-08-17T18:28:36.076239Z","shell.execute_reply":"2022-08-17T18:28:36.837911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_feather('../input/amexfeather/train_data.ftr')\ndf_train = df_train.groupby('customer_ID').tail(1).set_index('customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:30:33.385245Z","iopub.execute_input":"2022-08-17T18:30:33.385647Z","iopub.status.idle":"2022-08-17T18:30:58.993810Z","shell.execute_reply.started":"2022-08-17T18:30:33.385616Z","shell.execute_reply":"2022-08-17T18:30:58.992135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:32:09.980697Z","iopub.execute_input":"2022-08-17T18:32:09.981323Z","iopub.status.idle":"2022-08-17T18:32:09.993840Z","shell.execute_reply.started":"2022-08-17T18:32:09.981275Z","shell.execute_reply":"2022-08-17T18:32:09.992876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.dropna(axis=1, thresh=int(0.80 * len(df_train)))\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:39:27.951435Z","iopub.execute_input":"2022-08-17T18:39:27.951839Z","iopub.status.idle":"2022-08-17T18:39:29.167233Z","shell.execute_reply.started":"2022-08-17T18:39:27.951800Z","shell.execute_reply":"2022-08-17T18:39:29.166128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Feature**","metadata":{}},{"cell_type":"code","source":"df_train[\"c_PD_239\"]=df_train[\"D_39\"]/(df_train[\"P_2\"]*(-1)+0.0001)\ndf_train[\"c_PB_29\"]=df_train[\"P_2\"]*(-1)/(df_train[\"B_9\"]*(1)+0.0001)\ndf_train[\"c_PR_21\"]=df_train[\"P_2\"]*(-1)/(df_train[\"R_1\"]+0.0001)\n\ndf_train[\"c_BBBB\"]=(df_train[\"B_9\"]+0.001)/(df_train[\"B_23\"]+df_train[\"B_3\"]+0.0001)\ndf_train[\"c_BBBB1\"]=(df_train[\"B_33\"]*(-1))+(df_train[\"B_18\"]*(-1)+df_train[\"S_25\"]*(1)+0.0001)\ndf_train[\"c_BBBB2\"]=(df_train[\"B_19\"]+df_train[\"B_20\"]+df_train[\"B_4\"]+0.0001)\n\ndf_train[\"c_RRR0\"]=(df_train[\"R_3\"]+0.001)/(df_train[\"R_2\"]+df_train[\"R_4\"]+0.0001)\ndf_train[\"c_RRR1\"]=(df_train[\"D_62\"]+0.001)/(df_train[\"D_112\"]+df_train[\"R_27\"]+0.0001)\n\ndf_train[\"c_PD_348\"]=df_train[\"D_48\"]/(df_train[\"P_3\"]+0.0001)\ndf_train[\"c_PD_355\"]=df_train[\"D_55\"]/(df_train[\"P_3\"]+0.0001)\n\ndf_train[\"c_PD_439\"]=df_train[\"D_39\"]/(df_train[\"P_4\"]+0.0001)\ndf_train[\"c_PB_49\"]=df_train[\"B_9\"]/(df_train[\"P_4\"]+0.0001)\ndf_train[\"c_PR_41\"]=df_train[\"R_1\"]/(df_train[\"P_4\"]+0.0001)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:45:03.682935Z","iopub.execute_input":"2022-08-17T18:45:03.684804Z","iopub.status.idle":"2022-08-17T18:45:04.058845Z","shell.execute_reply.started":"2022-08-17T18:45:03.684738Z","shell.execute_reply":"2022-08-17T18:45:04.057551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model&Predict**","metadata":{}},{"cell_type":"code","source":"y = df_train['target']\nX = df_train.drop(['target'],axis=1).drop(\"S_2\", axis=1)\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=26,stratify=y)\n\nprint(\"X_train Training Data Size :\",X_train.shape[0])\nprint(\"X_test Testing Data Size   :\",X_test.shape[0])","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:58:30.873645Z","iopub.execute_input":"2022-08-17T18:58:30.874239Z","iopub.status.idle":"2022-08-17T18:58:33.912582Z","shell.execute_reply.started":"2022-08-17T18:58:30.874202Z","shell.execute_reply":"2022-08-17T18:58:33.911230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xname = X.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:58:34.628132Z","iopub.execute_input":"2022-08-17T18:58:34.629016Z","iopub.status.idle":"2022-08-17T18:58:34.635303Z","shell.execute_reply.started":"2022-08-17T18:58:34.628967Z","shell.execute_reply":"2022-08-17T18:58:34.633786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:58:37.508593Z","iopub.execute_input":"2022-08-17T18:58:37.509469Z","iopub.status.idle":"2022-08-17T18:58:37.515575Z","shell.execute_reply.started":"2022-08-17T18:58:37.509423Z","shell.execute_reply":"2022-08-17T18:58:37.514282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nmodel = lgb.LGBMClassifier(boosting_type='goss', max_depth=5, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:58:56.874082Z","iopub.execute_input":"2022-08-17T18:58:56.875228Z","iopub.status.idle":"2022-08-17T18:58:58.007855Z","shell.execute_reply.started":"2022-08-17T18:58:56.875185Z","shell.execute_reply":"2022-08-17T18:58:58.006175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T18:58:59.268851Z","iopub.execute_input":"2022-08-17T18:58:59.269548Z","iopub.status.idle":"2022-08-17T18:59:17.787731Z","shell.execute_reply.started":"2022-08-17T18:58:59.269511Z","shell.execute_reply":"2022-08-17T18:59:17.786426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submissions","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport glob\nfrom scipy.stats import rankdata\n\npaths = [x for x in glob.glob('../input/*/*.csv') if 'amex-default-prediction' not in x]\ndfs = [pd.read_csv(x) for x in paths]\ndfs = [x.sort_values(by='customer_ID') for x in dfs]\n\npaths = [x for x in glob.glob('../input/*/*.csv') if 'amex-default-prediction' not in x]\nprint(paths)\n\nfor df in dfs:\n    df['prediction'] = np.clip(df['prediction'], 0, 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:10:26.667807Z","iopub.execute_input":"2022-08-17T19:10:26.668261Z","iopub.status.idle":"2022-08-17T19:10:32.932219Z","shell.execute_reply.started":"2022-08-17T19:10:26.668224Z","shell.execute_reply":"2022-08-17T19:10:32.931109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths = [x for x in glob.glob('../input/*/*.csv') if 'amex-default-prediction' not in x]\ndfs = [pd.read_csv(x) for x in paths]\ndfs = [x.sort_values(by='customer_ID') for x in dfs]","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:10:58.467561Z","iopub.execute_input":"2022-08-17T19:10:58.467996Z","iopub.status.idle":"2022-08-17T19:11:04.425345Z","shell.execute_reply.started":"2022-08-17T19:10:58.467952Z","shell.execute_reply":"2022-08-17T19:11:04.424110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weights = [0.52, 0.85, 0.97, 0.57, 1.02, 0.8]# [0.52, 0.87, 0.95, 0.57, 1, 0.8]","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:11:19.546708Z","iopub.execute_input":"2022-08-17T19:11:19.547548Z","iopub.status.idle":"2022-08-17T19:11:19.553289Z","shell.execute_reply.started":"2022-08-17T19:11:19.547508Z","shell.execute_reply":"2022-08-17T19:11:19.552144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv('../input/amex-default-prediction/sample_submission.csv')\nsubmit['prediction'] = 0\n\nfor df, weight in zip(dfs, weights):\n    submit['prediction'] += (df['prediction'] * weight)\n    \nsubmit['prediction'] /= np.sum(weights)\n\nsubmit.to_csv('mean_submission.csv', index=None)\n\n \nsubmit = pd.read_csv('../input/amex-default-prediction/sample_submission.csv')\nsubmit['prediction'] = 0\n\nfor df, weight in zip(dfs, weights):\n    submit['prediction'] += (rankdata(df['prediction'])/df.shape[0]) * weight\n    \nsubmit['prediction'] /= 4","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:12:39.168799Z","iopub.execute_input":"2022-08-17T19:12:39.169298Z","iopub.status.idle":"2022-08-17T19:12:46.422729Z","shell.execute_reply.started":"2022-08-17T19:12:39.169259Z","shell.execute_reply":"2022-08-17T19:12:46.421585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_feather('../input/amexfeather/test_data.ftr')\ndf_train = test.groupby('customer_ID').tail(1).set_index('customer_ID')","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:12:46.424527Z","iopub.execute_input":"2022-08-17T19:12:46.425124Z","iopub.status.idle":"2022-08-17T19:13:55.516962Z","shell.execute_reply.started":"2022-08-17T19:12:46.425082Z","shell.execute_reply":"2022-08-17T19:13:55.515831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:13:55.518341Z","iopub.execute_input":"2022-08-17T19:13:55.518687Z","iopub.status.idle":"2022-08-17T19:13:55.527145Z","shell.execute_reply.started":"2022-08-17T19:13:55.518656Z","shell.execute_reply":"2022-08-17T19:13:55.525933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.dropna(axis=1, thresh=int(0.80 * len(df_train)))\n\ndf_train[\"c_PD_239\"]=df_train[\"D_39\"]/(df_train[\"P_2\"]*(-1)+0.0001)\ndf_train[\"c_PB_29\"]=df_train[\"P_2\"]*(-1)/(df_train[\"B_9\"]*(1)+0.0001)\ndf_train[\"c_PR_21\"]=df_train[\"P_2\"]*(-1)/(df_train[\"R_1\"]+0.0001)\n\ndf_train[\"c_BBBB\"]=(df_train[\"B_9\"]+0.001)/(df_train[\"B_23\"]+df_train[\"B_3\"]+0.0001)\ndf_train[\"c_BBBB1\"]=(df_train[\"B_33\"]*(-1))+(df_train[\"B_18\"]*(-1)+df_train[\"S_25\"]*(1)+0.0001)\ndf_train[\"c_BBBB2\"]=(df_train[\"B_19\"]+df_train[\"B_20\"]+df_train[\"B_4\"]+0.0001)\n\ndf_train[\"c_RRR0\"]=(df_train[\"R_3\"]+0.001)/(df_train[\"R_2\"]+df_train[\"R_4\"]+0.0001)\ndf_train[\"c_RRR1\"]=(df_train[\"D_62\"]+0.001)/(df_train[\"D_112\"]+df_train[\"R_27\"]+0.0001)\n\ndf_train[\"c_PD_348\"]=df_train[\"D_48\"]/(df_train[\"P_3\"]+0.0001)\ndf_train[\"c_PD_355\"]=df_train[\"D_55\"]/(df_train[\"P_3\"]+0.0001)\n\ndf_train[\"c_PD_439\"]=df_train[\"D_39\"]/(df_train[\"P_4\"]+0.0001)\ndf_train[\"c_PB_49\"]=df_train[\"B_9\"]/(df_train[\"P_4\"]+0.0001)\ndf_train[\"c_PR_41\"]=df_train[\"R_1\"]/(df_train[\"P_4\"]+0.0001)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:13:55.529817Z","iopub.execute_input":"2022-08-17T19:13:55.530889Z","iopub.status.idle":"2022-08-17T19:13:57.912818Z","shell.execute_reply.started":"2022-08-17T19:13:55.530848Z","shell.execute_reply":"2022-08-17T19:13:57.911511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop(\"S_2\", axis=1)\nX = X[Xname]","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:13:57.914283Z","iopub.execute_input":"2022-08-17T19:13:57.914642Z","iopub.status.idle":"2022-08-17T19:13:59.261877Z","shell.execute_reply.started":"2022-08-17T19:13:57.914609Z","shell.execute_reply":"2022-08-17T19:13:59.260782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict_proba(X)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:13:59.263426Z","iopub.execute_input":"2022-08-17T19:13:59.263761Z","iopub.status.idle":"2022-08-17T19:14:04.171953Z","shell.execute_reply.started":"2022-08-17T19:13:59.263732Z","shell.execute_reply":"2022-08-17T19:14:04.170587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit['prediction'] = (submit['prediction'])*(0.98)+(Y_pred[:,0])*(0.02)","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:14:04.173583Z","iopub.execute_input":"2022-08-17T19:14:04.174423Z","iopub.status.idle":"2022-08-17T19:14:04.190595Z","shell.execute_reply.started":"2022-08-17T19:14:04.174383Z","shell.execute_reply":"2022-08-17T19:14:04.189394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('submission.csv', index=None)    ","metadata":{"execution":{"iopub.status.busy":"2022-08-17T19:14:04.192344Z","iopub.execute_input":"2022-08-17T19:14:04.193235Z","iopub.status.idle":"2022-08-17T19:14:07.372850Z","shell.execute_reply.started":"2022-08-17T19:14:04.193192Z","shell.execute_reply":"2022-08-17T19:14:07.371518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}