{"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)\nimport matplotlib.pyplot as plt, gc, os\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-12-22T16:03:16.972547Z","iopub.execute_input":"2022-12-22T16:03:16.973135Z","iopub.status.idle":"2022-12-22T16:03:16.993509Z","shell.execute_reply.started":"2022-12-22T16:03:16.973093Z","shell.execute_reply":"2022-12-22T16:03:16.992552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-22T16:03:16.995627Z","iopub.execute_input":"2022-12-22T16:03:16.996297Z","iopub.status.idle":"2022-12-22T16:03:17.002961Z","shell.execute_reply.started":"2022-12-22T16:03:16.996258Z","shell.execute_reply":"2022-12-22T16:03:17.002054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-22T16:03:17.004688Z","iopub.execute_input":"2022-12-22T16:03:17.005448Z","iopub.status.idle":"2022-12-22T16:04:12.724443Z","shell.execute_reply.started":"2022-12-22T16:03:17.005401Z","shell.execute_reply":"2022-12-22T16:04:12.723625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shapes\ntrain.shape, test.shape, train_labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-22T16:04:12.725809Z","iopub.execute_input":"2022-12-22T16:04:12.726443Z","iopub.status.idle":"2022-12-22T16:04:12.734237Z","shell.execute_reply.started":"2022-12-22T16:04:12.726409Z","shell.execute_reply":"2022-12-22T16:04:12.732887Z"},"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-12-22T16:04:12.737374Z","iopub.execute_input":"2022-12-22T16:04:12.737956Z","iopub.status.idle":"2022-12-22T16:04:13.431095Z","shell.execute_reply.started":"2022-12-22T16:04:12.737918Z","shell.execute_reply":"2022-12-22T16:04:13.430061Z"},"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-12-22T16:04:13.432384Z","iopub.execute_input":"2022-12-22T16:04:13.432745Z","iopub.status.idle":"2022-12-22T16:04:14.946676Z","shell.execute_reply.started":"2022-12-22T16:04:13.432715Z","shell.execute_reply":"2022-12-22T16:04:14.945739Z"},"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-12-22T16:04:14.947941Z","iopub.execute_input":"2022-12-22T16:04:14.948437Z","iopub.status.idle":"2022-12-22T16:04:20.098487Z","shell.execute_reply.started":"2022-12-22T16:04:14.948407Z","shell.execute_reply":"2022-12-22T16:04:20.097568Z"},"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-12-22T16:04:20.099824Z","iopub.execute_input":"2022-12-22T16:04:20.100208Z","iopub.status.idle":"2022-12-22T16:04:20.116460Z","shell.execute_reply.started":"2022-12-22T16:04:20.100150Z","shell.execute_reply":"2022-12-22T16:04:20.115466Z"},"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-12-22T16:04:20.117773Z","iopub.execute_input":"2022-12-22T16:04:20.118882Z","iopub.status.idle":"2022-12-22T16:04:23.606504Z","shell.execute_reply.started":"2022-12-22T16:04:20.118848Z","shell.execute_reply":"2022-12-22T16:04:23.605521Z"},"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-12-22T16:04:23.607733Z","iopub.execute_input":"2022-12-22T16:04:23.608065Z","iopub.status.idle":"2022-12-22T16:04:23.613301Z","shell.execute_reply.started":"2022-12-22T16:04:23.608034Z","shell.execute_reply":"2022-12-22T16:04:23.612254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shape\ntrain.shape, train_labels.shape, test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-22T16:04:23.614521Z","iopub.execute_input":"2022-12-22T16:04:23.614973Z","iopub.status.idle":"2022-12-22T16:04:23.626897Z","shell.execute_reply.started":"2022-12-22T16:04:23.614932Z","shell.execute_reply":"2022-12-22T16:04:23.625836Z"},"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-12-22T16:04:23.628662Z","iopub.execute_input":"2022-12-22T16:04:23.629005Z","iopub.status.idle":"2022-12-22T16:04:33.717350Z","shell.execute_reply.started":"2022-12-22T16:04:23.628953Z","shell.execute_reply":"2022-12-22T16:04:33.715989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Preparation**","metadata":{}},{"cell_type":"code","source":"train = train.merge(train_labels, how='inner', on=\"customer_ID\")","metadata":{"execution":{"iopub.status.busy":"2022-12-22T16:04:33.719619Z","iopub.execute_input":"2022-12-22T16:04:33.720087Z","iopub.status.idle":"2022-12-22T16:04:43.666325Z","shell.execute_reply.started":"2022-12-22T16:04:33.720041Z","shell.execute_reply":"2022-12-22T16:04:43.664934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-22T16:04:43.671426Z","iopub.execute_input":"2022-12-22T16:04:43.671799Z","iopub.status.idle":"2022-12-22T16:04:43.699431Z","shell.execute_reply.started":"2022-12-22T16:04:43.671768Z","shell.execute_reply":"2022-12-22T16:04:43.698270Z"},"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-12-22T16:04:43.700897Z","iopub.execute_input":"2022-12-22T16:04:43.701256Z","iopub.status.idle":"2022-12-22T16:04:46.358258Z","shell.execute_reply.started":"2022-12-22T16:04:43.701221Z","shell.execute_reply":"2022-12-22T16:04:46.357229Z"},"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-12-22T16:04:46.359776Z","iopub.execute_input":"2022-12-22T16:04:46.360761Z","iopub.status.idle":"2022-12-22T16:04:49.096494Z","shell.execute_reply.started":"2022-12-22T16:04:46.360714Z","shell.execute_reply":"2022-12-22T16:04:49.095265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Features=train.loc[:, test.columns]\ntarget = train['target']","metadata":{"execution":{"iopub.status.busy":"2022-12-22T16:04:49.098356Z","iopub.execute_input":"2022-12-22T16:04:49.099077Z","iopub.status.idle":"2022-12-22T16:04:49.346246Z","shell.execute_reply.started":"2022-12-22T16:04:49.099031Z","shell.execute_reply":"2022-12-22T16:04:49.344978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Building and Evaluation**","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-12-22T16:04:49.347887Z","iopub.execute_input":"2022-12-22T16:04:49.350426Z","iopub.status.idle":"2022-12-22T16:28:52.070742Z","shell.execute_reply.started":"2022-12-22T16:04:49.350382Z","shell.execute_reply":"2022-12-22T16:28:52.069345Z"},"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-12-22T16:28:52.072724Z","iopub.execute_input":"2022-12-22T16:28:52.073077Z","iopub.status.idle":"2022-12-22T17:11:19.687321Z","shell.execute_reply.started":"2022-12-22T16:28:52.073046Z","shell.execute_reply":"2022-12-22T17:11:19.682645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = XGB.predict(Features)\naccuracy_score(target, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-12-22T17:11:19.690432Z","iopub.status.idle":"2022-12-22T17:11:19.691716Z","shell.execute_reply.started":"2022-12-22T17:11:19.691294Z","shell.execute_reply":"2022-12-22T17:11:19.691336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-22T17:11:19.693419Z","iopub.status.idle":"2022-12-22T17:11:19.694070Z","shell.execute_reply.started":"2022-12-22T17:11:19.693742Z","shell.execute_reply":"2022-12-22T17:11:19.693773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-22T17:11:19.696412Z","iopub.status.idle":"2022-12-22T17:11:19.697098Z","shell.execute_reply.started":"2022-12-22T17:11:19.696883Z","shell.execute_reply":"2022-12-22T17:11:19.696905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(target, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-12-22T17:11:19.698162Z","iopub.status.idle":"2022-12-22T17:11:19.698886Z","shell.execute_reply.started":"2022-12-22T17:11:19.698648Z","shell.execute_reply":"2022-12-22T17:11:19.698671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12-22T17:11:19.700812Z","iopub.status.idle":"2022-12-22T17:11:19.701483Z","shell.execute_reply.started":"2022-12-22T17:11:19.701261Z","shell.execute_reply":"2022-12-22T17:11:19.701284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**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-12-22T17:11:19.703003Z","iopub.status.idle":"2022-12-22T17:11:19.704106Z","shell.execute_reply.started":"2022-12-22T17:11:19.703771Z","shell.execute_reply":"2022-12-22T17:11:19.703811Z"},"trusted":true},"execution_count":null,"outputs":[]}]}