{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-21T12:26:01.445290Z","iopub.execute_input":"2022-08-21T12:26:01.446087Z","iopub.status.idle":"2022-08-21T12:26:01.493711Z","shell.execute_reply.started":"2022-08-21T12:26:01.445930Z","shell.execute_reply":"2022-08-21T12:26:01.492289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Import Packages","metadata":{}},{"cell_type":"code","source":"import os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\nimport os\nimport lightgbm\nimport matplotlib.pyplot as plt\nimport seaborn as sbn\nimport numpy as np\nimport pandas as pd\nimport warnings\nfrom xgboost import XGBClassifier\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc, RocCurveDisplay, accuracy_score\nimport shap\nimport itertools","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:26:01.496062Z","iopub.execute_input":"2022-08-21T12:26:01.497156Z","iopub.status.idle":"2022-08-21T12:26:07.341041Z","shell.execute_reply.started":"2022-08-21T12:26:01.497109Z","shell.execute_reply":"2022-08-21T12:26:07.339701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Load Files","metadata":{}},{"cell_type":"code","source":"train = pd.read_parquet(\"/kaggle/input/amex-data-integer-dtypes-parquet-format/train.parquet\").groupby('customer_ID').tail(4)\ntest = pd.read_parquet(\"/kaggle/input/amex-data-integer-dtypes-parquet-format/test.parquet\").groupby('customer_ID').tail(4)\ntrain_labels = pd.read_csv(\"../input/amex-default-prediction/train_labels.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:26:07.343473Z","iopub.execute_input":"2022-08-21T12:26:07.345248Z","iopub.status.idle":"2022-08-21T12:27:14.768345Z","shell.execute_reply.started":"2022-08-21T12:26:07.345196Z","shell.execute_reply":"2022-08-21T12:27:14.767112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shapes\ntrain.shape, test.shape, train_labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:14.772013Z","iopub.execute_input":"2022-08-21T12:27:14.772803Z","iopub.status.idle":"2022-08-21T12:27:14.780018Z","shell.execute_reply.started":"2022-08-21T12:27:14.772755Z","shell.execute_reply":"2022-08-21T12:27:14.778794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Missing values having more than 40%\ncolumns = train.columns[(train.isna().sum()/len(train))*100>40]","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:14.781803Z","iopub.execute_input":"2022-08-21T12:27:14.783045Z","iopub.status.idle":"2022-08-21T12:27:15.450006Z","shell.execute_reply.started":"2022-08-21T12:27:14.782998Z","shell.execute_reply":"2022-08-21T12:27:15.449170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Drop the missing columns having more than 40%\ntrain = train.drop(columns, axis=1)\ntest = test.drop(columns, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:15.451250Z","iopub.execute_input":"2022-08-21T12:27:15.452197Z","iopub.status.idle":"2022-08-21T12:27:16.953090Z","shell.execute_reply.started":"2022-08-21T12:27:15.452152Z","shell.execute_reply":"2022-08-21T12:27:16.948525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#fill the missing values\ntrain = train.bfill(axis='rows').ffill(axis='rows')\ntest = test.bfill(axis='rows').ffill(axis='rows')","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:16.954600Z","iopub.execute_input":"2022-08-21T12:27:16.955665Z","iopub.status.idle":"2022-08-21T12:27:21.931194Z","shell.execute_reply.started":"2022-08-21T12:27:16.955627Z","shell.execute_reply":"2022-08-21T12:27:21.930046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.reset_index(inplace=True)\ntest.reset_index(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:21.932640Z","iopub.execute_input":"2022-08-21T12:27:21.933086Z","iopub.status.idle":"2022-08-21T12:27:21.948678Z","shell.execute_reply.started":"2022-08-21T12:27:21.933041Z","shell.execute_reply":"2022-08-21T12:27:21.947796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train =train.groupby('customer_ID').tail(1)\ntest = test.groupby('customer_ID').tail(1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:21.949776Z","iopub.execute_input":"2022-08-21T12:27:21.950725Z","iopub.status.idle":"2022-08-21T12:27:25.378591Z","shell.execute_reply.started":"2022-08-21T12:27:21.950668Z","shell.execute_reply":"2022-08-21T12:27:25.377410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.reset_index(drop=True, inplace=True)\ntest.reset_index(drop=True, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:25.382203Z","iopub.execute_input":"2022-08-21T12:27:25.383027Z","iopub.status.idle":"2022-08-21T12:27:25.387666Z","shell.execute_reply.started":"2022-08-21T12:27:25.382991Z","shell.execute_reply":"2022-08-21T12:27:25.386483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shape\ntrain.shape, train_labels.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:25.389450Z","iopub.execute_input":"2022-08-21T12:27:25.389915Z","iopub.status.idle":"2022-08-21T12:27:25.400831Z","shell.execute_reply.started":"2022-08-21T12:27:25.389872Z","shell.execute_reply":"2022-08-21T12:27:25.399717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Type Conversion\nobj_col = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\nfor col in obj_col:\n    train[col]=train[col].astype('int').astype('str')\n    test[col]=test[col].astype('int').astype('str')\n    print(train[col].unique())\n    print(test[col].unique())","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:25.402519Z","iopub.execute_input":"2022-08-21T12:27:25.403234Z","iopub.status.idle":"2022-08-21T12:27:35.545280Z","shell.execute_reply.started":"2022-08-21T12:27:25.403181Z","shell.execute_reply":"2022-08-21T12:27:35.544434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Data Preparation","metadata":{}},{"cell_type":"code","source":"train = train.merge(train_labels, how='inner', on=\"customer_ID\")","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:35.546654Z","iopub.execute_input":"2022-08-21T12:27:35.547523Z","iopub.status.idle":"2022-08-21T12:27:45.651620Z","shell.execute_reply.started":"2022-08-21T12:27:35.547489Z","shell.execute_reply":"2022-08-21T12:27:45.650292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:45.652810Z","iopub.execute_input":"2022-08-21T12:27:45.653120Z","iopub.status.idle":"2022-08-21T12:27:45.681266Z","shell.execute_reply.started":"2022-08-21T12:27:45.653093Z","shell.execute_reply":"2022-08-21T12:27:45.680149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test.copy()\ntrain = train.drop(['index','customer_ID', 'S_2'], axis=1)\ntest = test.drop(['index','customer_ID', 'S_2'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:45.683033Z","iopub.execute_input":"2022-08-21T12:27:45.683762Z","iopub.status.idle":"2022-08-21T12:27:48.127657Z","shell.execute_reply.started":"2022-08-21T12:27:45.683719Z","shell.execute_reply":"2022-08-21T12:27:48.126817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#one hot Encoding for categorical features\nobj_col = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']\ntrain = pd.get_dummies(train, columns=obj_col, drop_first=True)\ntest = pd.get_dummies(test, columns=obj_col, drop_first=True)\ntrain.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:48.129158Z","iopub.execute_input":"2022-08-21T12:27:48.129517Z","iopub.status.idle":"2022-08-21T12:27:50.815698Z","shell.execute_reply.started":"2022-08-21T12:27:48.129486Z","shell.execute_reply":"2022-08-21T12:27:50.814397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Features=train.loc[:, test.columns]\ntarget = train['target']","metadata":{"execution":{"iopub.status.busy":"2022-08-21T12:27:50.817614Z","iopub.execute_input":"2022-08-21T12:27:50.818342Z","iopub.status.idle":"2022-08-21T12:27:51.033404Z","shell.execute_reply.started":"2022-08-21T12:27:50.818294Z","shell.execute_reply":"2022-08-21T12:27:51.032282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5. Model Building and Evaluation","metadata":{}},{"cell_type":"markdown","source":"**Model 1: XGBOOST Classifier**","metadata":{}},{"cell_type":"code","source":"XGB = XGBClassifier(n_estimators=300, max_depth=6, learning_rate=0.1).fit(Features, target)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:07.971201Z","iopub.execute_input":"2022-08-22T12:19:07.971746Z","iopub.status.idle":"2022-08-22T12:19:07.994116Z","shell.execute_reply.started":"2022-08-22T12:19:07.971707Z","shell.execute_reply":"2022-08-22T12:19:07.992578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.mean(cross_val_score(XGB, Features, target, scoring='accuracy', cv=5))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:10.600308Z","iopub.execute_input":"2022-08-22T12:19:10.601015Z","iopub.status.idle":"2022-08-22T12:19:10.625908Z","shell.execute_reply.started":"2022-08-22T12:19:10.600938Z","shell.execute_reply":"2022-08-22T12:19:10.624116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accuracy**","metadata":{}},{"cell_type":"code","source":"y_pred = XGB.predict(Features)\naccuracy_score(target, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:14.518243Z","iopub.execute_input":"2022-08-22T12:19:14.519287Z","iopub.status.idle":"2022-08-22T12:19:14.542535Z","shell.execute_reply.started":"2022-08-22T12:19:14.519237Z","shell.execute_reply":"2022-08-22T12:19:14.540759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Confusion Matrix**","metadata":{}},{"cell_type":"code","source":"cm=confusion_matrix(target, y_pred)\nplt.figure(figsize=(13,5))\nplt.title(\"Confusion Matrix\")\nplt.imshow(cm, alpha=0.5, cmap='PuBu')\nfor i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n    plt.text(j, i, cm[i, j], horizontalalignment=\"center\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:17.512770Z","iopub.execute_input":"2022-08-22T12:19:17.513368Z","iopub.status.idle":"2022-08-22T12:19:17.537528Z","shell.execute_reply.started":"2022-08-22T12:19:17.513319Z","shell.execute_reply":"2022-08-22T12:19:17.535716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ROC AUC Curve**","metadata":{}},{"cell_type":"code","source":"#AUC (ROC curve)\nfpr, tpr, thresholds = roc_curve(target, y_pred)\nroc_auc = auc(fpr, tpr)\ndisplay = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc, estimator_name=XGB)\ndisplay.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:20.411978Z","iopub.execute_input":"2022-08-22T12:19:20.412370Z","iopub.status.idle":"2022-08-22T12:19:20.431664Z","shell.execute_reply.started":"2022-08-22T12:19:20.412339Z","shell.execute_reply":"2022-08-22T12:19:20.429403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Classification Report**","metadata":{}},{"cell_type":"code","source":"print(classification_report(target, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:19:23.303081Z","iopub.execute_input":"2022-08-22T12:19:23.303559Z","iopub.status.idle":"2022-08-22T12:19:23.329031Z","shell.execute_reply.started":"2022-08-22T12:19:23.303519Z","shell.execute_reply":"2022-08-22T12:19:23.326661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**SHAP Summary Plot**","metadata":{}},{"cell_type":"code","source":"explainer = shap.TreeExplainer(XGB)\nshap_values = explainer.shap_values(Features)\nshap.summary_plot(shap_values, Features)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:18:58.941579Z","iopub.execute_input":"2022-08-22T12:18:58.942100Z","iopub.status.idle":"2022-08-22T12:18:58.960717Z","shell.execute_reply.started":"2022-08-22T12:18:58.942056Z","shell.execute_reply":"2022-08-22T12:18:58.958732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6. Submission to Kaggle","metadata":{}},{"cell_type":"code","source":"test_data['prediction']=XGB.predict_proba(test)[:,1]\ntest_data[['customer_ID','prediction']].to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T12:18:49.956930Z","iopub.execute_input":"2022-08-22T12:18:49.957664Z","iopub.status.idle":"2022-08-22T12:18:50.058659Z","shell.execute_reply.started":"2022-08-22T12:18:49.957485Z","shell.execute_reply":"2022-08-22T12:18:50.057128Z"},"trusted":true},"execution_count":null,"outputs":[]}]}