{"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-24T16:34:18.450221Z","iopub.execute_input":"2022-08-24T16:34:18.450670Z","iopub.status.idle":"2022-08-24T16:34:19.358161Z","shell.execute_reply.started":"2022-08-24T16:34:18.450586Z","shell.execute_reply":"2022-08-24T16:34:19.357242Z"},"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-24T16:34:23.207254Z","iopub.execute_input":"2022-08-24T16:34:23.207861Z","iopub.status.idle":"2022-08-24T16:34:47.934890Z","shell.execute_reply.started":"2022-08-24T16:34:23.207814Z","shell.execute_reply":"2022-08-24T16:34:47.933890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-24T16:34:47.936727Z","iopub.execute_input":"2022-08-24T16:34:47.937065Z","iopub.status.idle":"2022-08-24T16:34:47.946498Z","shell.execute_reply.started":"2022-08-24T16:34:47.937031Z","shell.execute_reply":"2022-08-24T16:34:47.945375Z"},"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-24T16:34:47.947985Z","iopub.execute_input":"2022-08-24T16:34:47.948686Z","iopub.status.idle":"2022-08-24T16:34:48.729053Z","shell.execute_reply.started":"2022-08-24T16:34:47.948651Z","shell.execute_reply":"2022-08-24T16:34:48.728035Z"},"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-24T16:34:48.731509Z","iopub.execute_input":"2022-08-24T16:34:48.732713Z","iopub.status.idle":"2022-08-24T16:34:49.191241Z","shell.execute_reply.started":"2022-08-24T16:34:48.732674Z","shell.execute_reply":"2022-08-24T16:34:49.190269Z"},"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, 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-24T16:34:49.192845Z","iopub.execute_input":"2022-08-24T16:34:49.193210Z","iopub.status.idle":"2022-08-24T16:34:51.046567Z","shell.execute_reply.started":"2022-08-24T16:34:49.193173Z","shell.execute_reply":"2022-08-24T16:34:51.045468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head(5).T","metadata":{"execution":{"iopub.status.busy":"2022-08-24T16:34:51.047883Z","iopub.execute_input":"2022-08-24T16:34:51.048811Z","iopub.status.idle":"2022-08-24T16:34:51.073943Z","shell.execute_reply.started":"2022-08-24T16:34:51.048772Z","shell.execute_reply":"2022-08-24T16:34:51.073104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xname = X.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-24T16:34:51.076248Z","iopub.execute_input":"2022-08-24T16:34:51.077231Z","iopub.status.idle":"2022-08-24T16:34:51.082182Z","shell.execute_reply.started":"2022-08-24T16:34:51.077195Z","shell.execute_reply":"2022-08-24T16:34:51.081125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import StandardScaler\n# sc = StandardScaler()\n# X_train = sc.fit_transform(X_train)\n# X_test = sc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-23T13:45:06.484492Z","iopub.execute_input":"2022-08-23T13:45:06.484892Z","iopub.status.idle":"2022-08-23T13:45:06.489487Z","shell.execute_reply.started":"2022-08-23T13:45:06.484852Z","shell.execute_reply":"2022-08-23T13:45:06.488431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-08-24T16:34:51.083874Z","iopub.execute_input":"2022-08-24T16:34:51.084264Z","iopub.status.idle":"2022-08-24T16:34:51.091581Z","shell.execute_reply.started":"2022-08-24T16:34:51.084211Z","shell.execute_reply":"2022-08-24T16:34:51.090698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\nmodel = lgb.LGBMClassifier(boosting_type='goss', max_depth=20, random_state=0, n_estimators=200, learning_rate=0.09, num_leaves=500)","metadata":{"execution":{"iopub.status.busy":"2022-08-24T17:48:18.507082Z","iopub.execute_input":"2022-08-24T17:48:18.507460Z","iopub.status.idle":"2022-08-24T17:48:18.512911Z","shell.execute_reply.started":"2022-08-24T17:48:18.507411Z","shell.execute_reply":"2022-08-24T17:48:18.511675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-24T17:48:19.315963Z","iopub.execute_input":"2022-08-24T17:48:19.316574Z","iopub.status.idle":"2022-08-24T17:50:46.861260Z","shell.execute_reply.started":"2022-08-24T17:48:19.316539Z","shell.execute_reply":"2022-08-24T17:50:46.860414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-24T17:50:46.863464Z","iopub.execute_input":"2022-08-24T17:50:46.864145Z","iopub.status.idle":"2022-08-24T17:50:59.600346Z","shell.execute_reply.started":"2022-08-24T17:50:46.864107Z","shell.execute_reply":"2022-08-24T17:50:59.599356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-24T17:50:59.601754Z","iopub.execute_input":"2022-08-24T17:50:59.602327Z","iopub.status.idle":"2022-08-24T17:51:03.255516Z","shell.execute_reply.started":"2022-08-24T17:50:59.602289Z","shell.execute_reply":"2022-08-24T17:51:03.254406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submissions","metadata":{}},{"cell_type":"code","source":"import 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]\npaths\n\nfor df in dfs:\n    df['prediction'] = np.clip(df['prediction'], 0, 1)","metadata":{"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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop(\"S_2\", axis=1)\nX = X[Xname]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = model.predict_proba(X)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit['prediction'] = (submit['prediction'])*(0.99)+(Y_pred[:,0])*(0.01)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('submission.csv', index=None)    ","metadata":{"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":[]}]}