{"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\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:39:37.337457Z","iopub.execute_input":"2022-08-02T10:39:37.338729Z","iopub.status.idle":"2022-08-02T10:39:37.942185Z","shell.execute_reply.started":"2022-08-02T10:39:37.338583Z","shell.execute_reply":"2022-08-02T10:39:37.940920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_parquet('../input/amex-parquet/train_data.parquet')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:39:49.512098Z","iopub.execute_input":"2022-08-02T10:39:49.513300Z","iopub.status.idle":"2022-08-02T10:40:36.437569Z","shell.execute_reply.started":"2022-08-02T10:39:49.513228Z","shell.execute_reply":"2022-08-02T10:40:36.431221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:40:51.461105Z","iopub.execute_input":"2022-08-02T10:40:51.461577Z","iopub.status.idle":"2022-08-02T10:40:51.469539Z","shell.execute_reply.started":"2022-08-02T10:40:51.461541Z","shell.execute_reply":"2022-08-02T10:40:51.468546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.dropna(axis=1, thresh=int(0.80 * len(train)))\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:40:53.001624Z","iopub.execute_input":"2022-08-02T10:40:53.002662Z","iopub.status.idle":"2022-08-02T10:41:00.981158Z","shell.execute_reply.started":"2022-08-02T10:40:53.002600Z","shell.execute_reply":"2022-08-02T10:41:00.980063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# There are multiple transactions. Lets take only the latest transaction from each customer.\n# Latest transaction may have missing values, we will perform forward fill for those missing values.\n# We do a backfill if the first row happens to be Na\n\ntrain=train.set_index(['customer_ID'])\ntrain=train.ffill().bfill()\ntrain=train.reset_index()\ntrain=train.groupby('customer_ID').tail(1)\ntrain=train.set_index(['customer_ID'])\n\n# Drop date column since it is no longer relevant\n\ntrain.drop(['S_2'],axis=1,inplace=True)\n\n# Check for number of rows\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:41:03.934103Z","iopub.execute_input":"2022-08-02T10:41:03.935460Z","iopub.status.idle":"2022-08-02T10:41:34.599673Z","shell.execute_reply.started":"2022-08-02T10:41:03.935410Z","shell.execute_reply":"2022-08-02T10:41:34.598184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Identify columns which are not numeric\ntrain.select_dtypes(['object']).columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:41:42.026611Z","iopub.execute_input":"2022-08-02T10:41:42.027045Z","iopub.status.idle":"2022-08-02T10:41:42.050095Z","shell.execute_reply.started":"2022-08-02T10:41:42.027012Z","shell.execute_reply":"2022-08-02T10:41:42.048493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Perform one-hot encoding for D_63 and D_64\ntrain_D63 = pd.get_dummies(train[['D_63']])\ntrain = pd.concat([train, train_D63], axis=1)\ntrain = train.drop(['D_63'], axis=1)\n\ntrain_D64 = pd.get_dummies(train[['D_64']])\ntrain = pd.concat([train, train_D64], axis=1)\ntrain = train.drop(['D_64'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:41:45.505866Z","iopub.execute_input":"2022-08-02T10:41:45.506349Z","iopub.status.idle":"2022-08-02T10:41:46.766946Z","shell.execute_reply.started":"2022-08-02T10:41:45.506307Z","shell.execute_reply":"2022-08-02T10:41:46.765678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We still have large number of columns, lets remove columns with low correlation(<0.4) to the target\ntrain.drop(train.columns[train.corrwith(train['target']).abs()<0.4],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:09.515303Z","iopub.execute_input":"2022-08-02T10:42:09.516577Z","iopub.status.idle":"2022-08-02T10:42:10.542928Z","shell.execute_reply.started":"2022-08-02T10:42:09.516527Z","shell.execute_reply":"2022-08-02T10:42:10.541740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:14.593384Z","iopub.execute_input":"2022-08-02T10:42:14.593812Z","iopub.status.idle":"2022-08-02T10:42:14.601412Z","shell.execute_reply.started":"2022-08-02T10:42:14.593779Z","shell.execute_reply":"2022-08-02T10:42:14.600338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_counts = train['target'].value_counts()\nplt.figure(figsize = (8,6))\nsns.barplot(label_counts.index, label_counts.values, alpha = 0.9)\nplt.xticks(rotation = 'vertical')\nplt.xlabel('Class', fontsize =12)\nplt.ylabel('Counts', fontsize = 12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:17.941408Z","iopub.execute_input":"2022-08-02T10:42:17.941863Z","iopub.status.idle":"2022-08-02T10:42:18.181810Z","shell.execute_reply.started":"2022-08-02T10:42:17.941826Z","shell.execute_reply":"2022-08-02T10:42:18.180605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['target']\nX = train.drop(['target'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:22.565918Z","iopub.execute_input":"2022-08-02T10:42:22.566434Z","iopub.status.idle":"2022-08-02T10:42:22.598521Z","shell.execute_reply.started":"2022-08-02T10:42:22.566391Z","shell.execute_reply":"2022-08-02T10:42:22.597434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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-02T10:42:32.381951Z","iopub.execute_input":"2022-08-02T10:42:32.382891Z","iopub.status.idle":"2022-08-02T10:42:32.732510Z","shell.execute_reply.started":"2022-08-02T10:42:32.382844Z","shell.execute_reply":"2022-08-02T10:42:32.731184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression \nmodel = LogisticRegression()\n%time model.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:36.023174Z","iopub.execute_input":"2022-08-02T10:42:36.024269Z","iopub.status.idle":"2022-08-02T10:42:43.818298Z","shell.execute_reply.started":"2022-08-02T10:42:36.024207Z","shell.execute_reply":"2022-08-02T10:42:43.816727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:57.130201Z","iopub.execute_input":"2022-08-02T10:42:57.130645Z","iopub.status.idle":"2022-08-02T10:42:57.155003Z","shell.execute_reply.started":"2022-08-02T10:42:57.130603Z","shell.execute_reply":"2022-08-02T10:42:57.153220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,confusion_matrix\nprint(\"Accuracy Score \",accuracy_score(predict,y_test))\nconfusion_matrix(predict,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:42:58.973235Z","iopub.execute_input":"2022-08-02T10:42:58.973777Z","iopub.status.idle":"2022-08-02T10:42:59.041767Z","shell.execute_reply.started":"2022-08-02T10:42:58.973737Z","shell.execute_reply":"2022-08-02T10:42:59.040846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Random Forest Classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nclf = RandomForestClassifier()\n%time clf.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:43:12.273677Z","iopub.execute_input":"2022-08-02T10:43:12.274118Z","iopub.status.idle":"2022-08-02T10:51:21.690711Z","shell.execute_reply.started":"2022-08-02T10:43:12.274083Z","shell.execute_reply":"2022-08-02T10:51:21.689374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:52:48.217748Z","iopub.execute_input":"2022-08-02T10:52:48.218232Z","iopub.status.idle":"2022-08-02T10:52:51.739166Z","shell.execute_reply.started":"2022-08-02T10:52:48.218197Z","shell.execute_reply":"2022-08-02T10:52:51.737805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy Score \",accuracy_score(predict,y_test))\nconfusion_matrix(predict,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:52:57.951976Z","iopub.execute_input":"2022-08-02T10:52:57.952445Z","iopub.status.idle":"2022-08-02T10:52:57.989127Z","shell.execute_reply.started":"2022-08-02T10:52:57.952406Z","shell.execute_reply":"2022-08-02T10:52:57.987984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# XGBoost","metadata":{}},{"cell_type":"code","source":"import xgboost as xg\nxgb = xg.XGBClassifier(n_estimators = 10, seed = 123)\n%time xgb.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:53:03.938543Z","iopub.execute_input":"2022-08-02T10:53:03.939314Z","iopub.status.idle":"2022-08-02T10:53:16.920313Z","shell.execute_reply.started":"2022-08-02T10:53:03.939274Z","shell.execute_reply":"2022-08-02T10:53:16.919030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = xgb.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:53:21.329944Z","iopub.execute_input":"2022-08-02T10:53:21.331452Z","iopub.status.idle":"2022-08-02T10:53:21.368220Z","shell.execute_reply.started":"2022-08-02T10:53:21.331396Z","shell.execute_reply":"2022-08-02T10:53:21.367186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy Score \",accuracy_score(predict,y_test))\nconfusion_matrix(predict,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:53:32.770653Z","iopub.execute_input":"2022-08-02T10:53:32.771087Z","iopub.status.idle":"2022-08-02T10:53:32.806728Z","shell.execute_reply.started":"2022-08-02T10:53:32.771053Z","shell.execute_reply":"2022-08-02T10:53:32.805772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Gradient Boosting Classifier","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import GradientBoostingClassifier\nXB = GradientBoostingClassifier(max_depth = 10,n_estimators=20,verbose=True)\n%time XB.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:53:37.249447Z","iopub.execute_input":"2022-08-02T10:53:37.249891Z","iopub.status.idle":"2022-08-02T10:58:47.114752Z","shell.execute_reply.started":"2022-08-02T10:53:37.249854Z","shell.execute_reply":"2022-08-02T10:58:47.113300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = XB.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:59:07.518064Z","iopub.execute_input":"2022-08-02T10:59:07.518512Z","iopub.status.idle":"2022-08-02T10:59:07.668217Z","shell.execute_reply.started":"2022-08-02T10:59:07.518477Z","shell.execute_reply":"2022-08-02T10:59:07.666952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy Score \",accuracy_score(predict,y_test))\nconfusion_matrix(predict,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:59:14.282011Z","iopub.execute_input":"2022-08-02T10:59:14.282533Z","iopub.status.idle":"2022-08-02T10:59:14.318657Z","shell.execute_reply.started":"2022-08-02T10:59:14.282492Z","shell.execute_reply":"2022-08-02T10:59:14.317802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Data ","metadata":{}},{"cell_type":"code","source":"list_of_columns=list(train.columns)\nlist_of_columns.remove('target')\nlist_of_columns.append('customer_ID')\n\ntest = pd.read_parquet('../input/amex-parquet/test_data.parquet',columns=list_of_columns)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:59:30.319448Z","iopub.execute_input":"2022-08-02T10:59:30.319905Z","iopub.status.idle":"2022-08-02T10:59:48.104724Z","shell.execute_reply.started":"2022-08-02T10:59:30.319870Z","shell.execute_reply":"2022-08-02T10:59:48.103387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=test.set_index(['customer_ID'])\ntest=test.ffill().bfill()\ntest=test.reset_index()\ntest=test.groupby('customer_ID').tail(1)\ntest=test.set_index(['customer_ID'])","metadata":{"execution":{"iopub.status.busy":"2022-08-02T10:59:54.700466Z","iopub.execute_input":"2022-08-02T10:59:54.700896Z","iopub.status.idle":"2022-08-02T11:00:00.899005Z","shell.execute_reply.started":"2022-08-02T10:59:54.700863Z","shell.execute_reply":"2022-08-02T11:00:00.897799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-02T11:00:04.766731Z","iopub.execute_input":"2022-08-02T11:00:04.767497Z","iopub.status.idle":"2022-08-02T11:00:04.777227Z","shell.execute_reply.started":"2022-08-02T11:00:04.767430Z","shell.execute_reply":"2022-08-02T11:00:04.775834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction = clf.predict_proba(test) ","metadata":{"execution":{"iopub.status.busy":"2022-08-02T11:00:07.550566Z","iopub.execute_input":"2022-08-02T11:00:07.551035Z","iopub.status.idle":"2022-08-02T11:00:40.464032Z","shell.execute_reply.started":"2022-08-02T11:00:07.550994Z","shell.execute_reply":"2022-08-02T11:00:40.462778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retrieve the probability of default\ny_predict_final = test_prediction[:,1]\n\n# Reset index of test\ntest = test.reset_index()\n\n# Merge the prediction and customer_ID into submission dataframe\nsubmission = pd.DataFrame({\"customer_ID\":test.customer_ID,\"prediction\":y_predict_final})\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-02T11:02:14.258995Z","iopub.execute_input":"2022-08-02T11:02:14.259492Z","iopub.status.idle":"2022-08-02T11:02:17.303529Z","shell.execute_reply.started":"2022-08-02T11:02:14.259457Z","shell.execute_reply":"2022-08-02T11:02:17.302504Z"},"trusted":true},"execution_count":null,"outputs":[]}]}