{"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 packages\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:32:44.780275Z","iopub.execute_input":"2022-08-12T10:32:44.781144Z","iopub.status.idle":"2022-08-12T10:32:44.789271Z","shell.execute_reply.started":"2022-08-12T10:32:44.781071Z","shell.execute_reply":"2022-08-12T10:32:44.787172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading the train data\ntrain_data = pd.read_parquet('../input/amex-parquet/train_data.parquet')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:32:44.803417Z","iopub.execute_input":"2022-08-12T10:32:44.804162Z","iopub.status.idle":"2022-08-12T10:33:24.404002Z","shell.execute_reply.started":"2022-08-12T10:32:44.804118Z","shell.execute_reply":"2022-08-12T10:33:24.402770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:24.408402Z","iopub.execute_input":"2022-08-12T10:33:24.408806Z","iopub.status.idle":"2022-08-12T10:33:24.416843Z","shell.execute_reply.started":"2022-08-12T10:33:24.408772Z","shell.execute_reply":"2022-08-12T10:33:24.415507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_rows', 1000)\npd.set_option('display.max_columns', 1000)\npd.set_option('display.width', 1000)\ntrain_data.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:24.418254Z","iopub.execute_input":"2022-08-12T10:33:24.419493Z","iopub.status.idle":"2022-08-12T10:33:27.224713Z","shell.execute_reply.started":"2022-08-12T10:33:24.419450Z","shell.execute_reply":"2022-08-12T10:33:27.223602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing libraries\nimport matplotlib.pyplot as plt\nimport seaborn\n\n# declaring data\ndata = train_data['target'].value_counts()\nkeys = [0,1]\nexplode = [0, 0.1]\n# plotting data on chart\nplt.pie(data, labels=keys,explode=explode,autopct='%.0f%%')\n\n# displaying chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:27.227521Z","iopub.execute_input":"2022-08-12T10:33:27.227934Z","iopub.status.idle":"2022-08-12T10:33:28.589341Z","shell.execute_reply.started":"2022-08-12T10:33:27.227889Z","shell.execute_reply":"2022-08-12T10:33:28.587382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**25% is default and remaining 75% is non default** - Only 25 % of credit card holders are not paybacking their credit amount in 120 days and 75% of customers are following their credits due perfectly","metadata":{}},{"cell_type":"code","source":"train_data.select_dtypes(['object'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:28.592628Z","iopub.execute_input":"2022-08-12T10:33:28.593964Z","iopub.status.idle":"2022-08-12T10:33:28.914867Z","shell.execute_reply.started":"2022-08-12T10:33:28.593883Z","shell.execute_reply":"2022-08-12T10:33:28.913553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"D_63\"].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:28.916417Z","iopub.execute_input":"2022-08-12T10:33:28.917121Z","iopub.status.idle":"2022-08-12T10:33:29.318636Z","shell.execute_reply.started":"2022-08-12T10:33:28.917055Z","shell.execute_reply":"2022-08-12T10:33:29.317117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['D_64'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:29.320216Z","iopub.execute_input":"2022-08-12T10:33:29.320595Z","iopub.status.idle":"2022-08-12T10:33:29.572019Z","shell.execute_reply.started":"2022-08-12T10:33:29.320561Z","shell.execute_reply":"2022-08-12T10:33:29.571009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"print(train_data['D_63'].isna().sum())\nprint(train_data['D_64'].isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:29.573855Z","iopub.execute_input":"2022-08-12T10:33:29.574255Z","iopub.status.idle":"2022-08-12T10:33:30.107554Z","shell.execute_reply.started":"2022-08-12T10:33:29.574222Z","shell.execute_reply":"2022-08-12T10:33:30.106156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\"D_64\" coulmn contains negative values,First remove the negative values and then concentrate on encoding","metadata":{}},{"cell_type":"code","source":"train = train_data.dropna(axis=1, thresh=int(0.80 * len(train_data)))\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:33:56.137010Z","iopub.execute_input":"2022-08-12T10:33:56.137556Z","iopub.status.idle":"2022-08-12T10:34:01.259511Z","shell.execute_reply.started":"2022-08-12T10:33:56.137513Z","shell.execute_reply":"2022-08-12T10:34:01.258008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=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-12T10:34:32.666143Z","iopub.execute_input":"2022-08-12T10:34:32.666781Z","iopub.status.idle":"2022-08-12T10:34:57.470715Z","shell.execute_reply.started":"2022-08-12T10:34:32.666727Z","shell.execute_reply":"2022-08-12T10:34:57.469189Z"},"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-12T10:35:32.084731Z","iopub.execute_input":"2022-08-12T10:35:32.085370Z","iopub.status.idle":"2022-08-12T10:35:33.524974Z","shell.execute_reply.started":"2022-08-12T10:35:32.085317Z","shell.execute_reply":"2022-08-12T10:35:33.523771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train1=train.corr().abs()>0.4","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:48:58.467842Z","iopub.execute_input":"2022-08-12T10:48:58.468383Z","iopub.status.idle":"2022-08-12T10:49:31.895210Z","shell.execute_reply.started":"2022-08-12T10:48:58.468341Z","shell.execute_reply":"2022-08-12T10:49:31.894117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.drop(train.columns[train.corrwith(train['target']).abs()<0.4],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:50:41.774136Z","iopub.execute_input":"2022-08-12T10:50:41.774769Z","iopub.status.idle":"2022-08-12T10:50:43.206122Z","shell.execute_reply.started":"2022-08-12T10:50:41.774717Z","shell.execute_reply":"2022-08-12T10:50:43.204020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:50:52.934409Z","iopub.execute_input":"2022-08-12T10:50:52.935006Z","iopub.status.idle":"2022-08-12T10:50:52.943278Z","shell.execute_reply.started":"2022-08-12T10:50:52.934953Z","shell.execute_reply":"2022-08-12T10:50:52.941786Z"},"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-12T10:51:07.737993Z","iopub.execute_input":"2022-08-12T10:51:07.739107Z","iopub.status.idle":"2022-08-12T10:51:07.783843Z","shell.execute_reply.started":"2022-08-12T10:51:07.739031Z","shell.execute_reply":"2022-08-12T10:51:07.782178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.feature_selection import mutual_info_classif\nimport matplotlib.pyplot as plt\n%matplotlib inline\nf = plt.figure()\nf.set_figwidth(10)\nf.set_figheight(15)\nimportances= mutual_info_classif(X,y)\nfeatureimp=pd.Series(importances)\nfeatureimp.plot(kind='barh',color=\"teal\")","metadata":{"execution":{"iopub.status.busy":"2022-08-12T10:57:09.733770Z","iopub.execute_input":"2022-08-12T10:57:09.734632Z","iopub.status.idle":"2022-08-12T10:58:55.328595Z","shell.execute_reply.started":"2022-08-12T10:57:09.734586Z","shell.execute_reply":"2022-08-12T10:58:55.327207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looks like almost all the features giving some importances towards the results,So we will include select all the parameters based on mutual_info technique ","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBClassifier\nmodel=XGBClassifier(objective='binary:logistic')\nmodel.fit(X,y)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:00:31.428753Z","iopub.execute_input":"2022-08-12T11:00:31.430276Z","iopub.status.idle":"2022-08-12T11:03:20.657403Z","shell.execute_reply.started":"2022-08-12T11:00:31.430208Z","shell.execute_reply":"2022-08-12T11:03:20.656138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-12T11:05:26.227965Z","iopub.execute_input":"2022-08-12T11:05:26.229148Z","iopub.status.idle":"2022-08-12T11:05:49.240880Z","shell.execute_reply.started":"2022-08-12T11:05:26.229075Z","shell.execute_reply":"2022-08-12T11:05:49.236238Z"},"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-12T11:05:55.533197Z","iopub.execute_input":"2022-08-12T11:05:55.533872Z","iopub.status.idle":"2022-08-12T11:06:04.144126Z","shell.execute_reply.started":"2022-08-12T11:05:55.533812Z","shell.execute_reply":"2022-08-12T11:06:04.142553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_prediction = model.predict_proba(test) ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T11:08:12.814937Z","iopub.execute_input":"2022-08-12T11:08:12.815562Z","iopub.status.idle":"2022-08-12T11:08:13.877189Z","shell.execute_reply.started":"2022-08-12T11:08:12.815510Z","shell.execute_reply":"2022-08-12T11:08:13.876009Z"},"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-12T11:08:22.691081Z","iopub.execute_input":"2022-08-12T11:08:22.691717Z","iopub.status.idle":"2022-08-12T11:08:26.256032Z","shell.execute_reply.started":"2022-08-12T11:08:22.691654Z","shell.execute_reply":"2022-08-12T11:08:26.254849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}