{"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 os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:49.663500Z","iopub.execute_input":"2022-07-27T18:46:49.663936Z","iopub.status.idle":"2022-07-27T18:46:49.672493Z","shell.execute_reply.started":"2022-07-27T18:46:49.663906Z","shell.execute_reply":"2022-07-27T18:46:49.671255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing libraries\n\n# for data manipulation\nimport pandas as pd\nimport numpy as np\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# for data visualization  \nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:49.674066Z","iopub.execute_input":"2022-07-27T18:46:49.674777Z","iopub.status.idle":"2022-07-27T18:46:49.687106Z","shell.execute_reply.started":"2022-07-27T18:46:49.674732Z","shell.execute_reply":"2022-07-27T18:46:49.685823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing training data\n\ntrain = pd.read_csv(\"/kaggle/input/credit-default-prediction-ai-big-data/train.csv\", index_col = 'Id')\n\ntrain.dropna(how = 'all')\n\nprint(\"Train Data has been read\")\n\n# Printing transpose of dataset to view all columns\ntrain.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:49.689404Z","iopub.execute_input":"2022-07-27T18:46:49.689836Z","iopub.status.idle":"2022-07-27T18:46:50.027211Z","shell.execute_reply.started":"2022-07-27T18:46:49.689804Z","shell.execute_reply":"2022-07-27T18:46:50.026273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing test data\n\ntest = pd.read_csv(\"/kaggle/input/credit-default-prediction-ai-big-data/test.csv\", index_col = 'Id')\n\ntest.dropna(how = 'all')\nprint(\"Train Data has been read\")\n\n# Printing transpose of dataset to view all columns\ntest.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.029485Z","iopub.execute_input":"2022-07-27T18:46:50.029793Z","iopub.status.idle":"2022-07-27T18:46:50.164782Z","shell.execute_reply.started":"2022-07-27T18:46:50.029763Z","shell.execute_reply":"2022-07-27T18:46:50.163911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Performing Analysis of the dataset","metadata":{}},{"cell_type":"code","source":"# Info about the train dataset\n\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.166166Z","iopub.execute_input":"2022-07-27T18:46:50.166475Z","iopub.status.idle":"2022-07-27T18:46:50.183507Z","shell.execute_reply.started":"2022-07-27T18:46:50.166436Z","shell.execute_reply":"2022-07-27T18:46:50.182378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stats about the train dataset\n\ntrain.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.185466Z","iopub.execute_input":"2022-07-27T18:46:50.185805Z","iopub.status.idle":"2022-07-27T18:46:50.233632Z","shell.execute_reply.started":"2022-07-27T18:46:50.185772Z","shell.execute_reply":"2022-07-27T18:46:50.232611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Info about the test dataset\n\ntest.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.235063Z","iopub.execute_input":"2022-07-27T18:46:50.235431Z","iopub.status.idle":"2022-07-27T18:46:50.247524Z","shell.execute_reply.started":"2022-07-27T18:46:50.235395Z","shell.execute_reply":"2022-07-27T18:46:50.246535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stats about the test dataset\n\ntest.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.248840Z","iopub.execute_input":"2022-07-27T18:46:50.249151Z","iopub.status.idle":"2022-07-27T18:46:50.293073Z","shell.execute_reply.started":"2022-07-27T18:46:50.249120Z","shell.execute_reply":"2022-07-27T18:46:50.292233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Changing Names of columns ::","metadata":{}},{"cell_type":"code","source":"#Deleting spaces between names and interchanging with underscore\n\nnew_cols = [str(i).lower().replace(\" \", \"_\") for i in (list(train.columns))]\nnew_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.294505Z","iopub.execute_input":"2022-07-27T18:46:50.294795Z","iopub.status.idle":"2022-07-27T18:46:50.301367Z","shell.execute_reply.started":"2022-07-27T18:46:50.294766Z","shell.execute_reply":"2022-07-27T18:46:50.300587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assigning new names to columns\n\ntrain.columns = new_cols\ntest.columns = new_cols[:-1]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.302291Z","iopub.execute_input":"2022-07-27T18:46:50.302548Z","iopub.status.idle":"2022-07-27T18:46:50.314169Z","shell.execute_reply.started":"2022-07-27T18:46:50.302523Z","shell.execute_reply":"2022-07-27T18:46:50.313311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing transpose of dataset to view all columns\n\ntrain.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.316706Z","iopub.execute_input":"2022-07-27T18:46:50.317197Z","iopub.status.idle":"2022-07-27T18:46:50.634852Z","shell.execute_reply.started":"2022-07-27T18:46:50.317160Z","shell.execute_reply":"2022-07-27T18:46:50.634104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Printing transpose of dataset to view all columns\n\ntest.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:50.636259Z","iopub.execute_input":"2022-07-27T18:46:50.636706Z","iopub.status.idle":"2022-07-27T18:46:51.100005Z","shell.execute_reply.started":"2022-07-27T18:46:50.636661Z","shell.execute_reply":"2022-07-27T18:46:51.099095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Since renewable energy has no value hence analyzing it\n\ncategories_count = pd.DataFrame({\"train\": train.purpose.value_counts(dropna = False),\n                                 \"test\": test.purpose.value_counts(dropna = False)}).reset_index()\ncategories_count = categories_count.rename(columns={\"index\": \"features\"})\ncategories_count","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.101331Z","iopub.execute_input":"2022-07-27T18:46:51.101638Z","iopub.status.idle":"2022-07-27T18:46:51.121348Z","shell.execute_reply.started":"2022-07-27T18:46:51.101609Z","shell.execute_reply":"2022-07-27T18:46:51.120119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Since Renewable energy category is missing in test data so after converting it in dummy variable it will create a mismatch in modelling. Since number of columns in dummy variable will be one less . Hence dropping it now.","metadata":{}},{"cell_type":"code","source":"train = train[train.purpose != 'renewable energy']\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.123019Z","iopub.execute_input":"2022-07-27T18:46:51.123475Z","iopub.status.idle":"2022-07-27T18:46:51.164696Z","shell.execute_reply.started":"2022-07-27T18:46:51.123436Z","shell.execute_reply":"2022-07-27T18:46:51.163547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataframe with dataset info","metadata":{}},{"cell_type":"code","source":"# Creating a dataframe that contains various values to corresponding columns\n\nmask = train.isnull()                  # calculate total null values\ntotal = mask.sum()                    # calculate the sum\npercent = 100 * mask.mean()           # calculate the percent missing values\ndtype = train.dtypes                   # getting the data types of the columns\nunique = train.nunique()               # getting the all unique values of the columns\n\n\n# creating a dataframe\n\nnull_train = pd.concat([total, percent, dtype, unique], \n                       axis = 1, \n                       keys = ['Total Count', 'Percent Missing', 'dtype', 'Unique Values'])\n\nnull_train.sort_values(by = 'Total Count', ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.166818Z","iopub.execute_input":"2022-07-27T18:46:51.167151Z","iopub.status.idle":"2022-07-27T18:46:51.200036Z","shell.execute_reply.started":"2022-07-27T18:46:51.167122Z","shell.execute_reply":"2022-07-27T18:46:51.198983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mask1 = test.isnull()                          # calculate total null values\ntotal1 = mask1.sum()                           # calculate the sum\npercent1 = 100 * mask1.mean()                  # calculate the percent missing values\ndtype1 = test.dtypes                           # getting the data types of the columns\nunique1 = test.nunique()                       # getting the all unique values of the columns\n\nnull_test = pd.concat([total1, percent1, dtype1, unique1], \n                      axis = 1, \n                      keys = ['Total Count', 'Percent Missing', 'dtype', 'Unique Values'])\n\n\n# Soritng the values by count\n\nnull_test.sort_values(by = 'Total Count', ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.201611Z","iopub.execute_input":"2022-07-27T18:46:51.202062Z","iopub.status.idle":"2022-07-27T18:46:51.229818Z","shell.execute_reply.started":"2022-07-27T18:46:51.202031Z","shell.execute_reply":"2022-07-27T18:46:51.228894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization ::","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.231405Z","iopub.execute_input":"2022-07-27T18:46:51.231882Z","iopub.status.idle":"2022-07-27T18:46:51.237588Z","shell.execute_reply.started":"2022-07-27T18:46:51.231847Z","shell.execute_reply":"2022-07-27T18:46:51.236549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,10))\nsns.heatmap(train.corr(), annot = True, vmax = 1, vmin = -1, square = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:51.239083Z","iopub.execute_input":"2022-07-27T18:46:51.239400Z","iopub.status.idle":"2022-07-27T18:46:52.101327Z","shell.execute_reply.started":"2022-07-27T18:46:51.239340Z","shell.execute_reply":"2022-07-27T18:46:52.100166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Selecting columns with missing values and observing their relationships with other features","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(y = train.months_since_last_delinquent, x = train.bankruptcies, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:52.102666Z","iopub.execute_input":"2022-07-27T18:46:52.102967Z","iopub.status.idle":"2022-07-27T18:46:52.390022Z","shell.execute_reply.started":"2022-07-27T18:46:52.102938Z","shell.execute_reply":"2022-07-27T18:46:52.388827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(y = train.months_since_last_delinquent, x = train.number_of_credit_problems, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:52.391938Z","iopub.execute_input":"2022-07-27T18:46:52.392635Z","iopub.status.idle":"2022-07-27T18:46:52.766809Z","shell.execute_reply.started":"2022-07-27T18:46:52.392585Z","shell.execute_reply":"2022-07-27T18:46:52.765740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(y = train.months_since_last_delinquent, x = train.purpose, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:52.768146Z","iopub.execute_input":"2022-07-27T18:46:52.768541Z","iopub.status.idle":"2022-07-27T18:46:53.330975Z","shell.execute_reply.started":"2022-07-27T18:46:52.768483Z","shell.execute_reply":"2022-07-27T18:46:53.329826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Observation :: \n        \n- Months increase with increase in bankrupcies\n- So both are highly dependent to each other\n- We can try to fill the missing months with respective bankrupcies\n- Since bankruptcies also has missing values hence filling it with mode value","metadata":{}},{"cell_type":"markdown","source":"## Printing all unique values present in corresponding columns","metadata":{}},{"cell_type":"code","source":"categories_count = pd.DataFrame({\"tax_liens\": train.tax_liens.value_counts(dropna = False),\n                                 \"bankruptcies\": train.bankruptcies.value_counts(dropna = False)}\n                               ).sort_index(ascending=True).reset_index()\ncategories_count = categories_count.rename(columns={\"index\": \"bankrupcies_count\"})\ncategories_count","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.332964Z","iopub.execute_input":"2022-07-27T18:46:53.333413Z","iopub.status.idle":"2022-07-27T18:46:53.354372Z","shell.execute_reply.started":"2022-07-27T18:46:53.333376Z","shell.execute_reply":"2022-07-27T18:46:53.352936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.purpose.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.355823Z","iopub.execute_input":"2022-07-27T18:46:53.356612Z","iopub.status.idle":"2022-07-27T18:46:53.366255Z","shell.execute_reply.started":"2022-07-27T18:46:53.356571Z","shell.execute_reply":"2022-07-27T18:46:53.365125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.number_of_credit_problems.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.367518Z","iopub.execute_input":"2022-07-27T18:46:53.367804Z","iopub.status.idle":"2022-07-27T18:46:53.381699Z","shell.execute_reply.started":"2022-07-27T18:46:53.367776Z","shell.execute_reply":"2022-07-27T18:46:53.380357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Observation ::\n\n- number_of_credit_problems, tax_liens, bankruptcies are abnormally distributed hence we can't use these columns in filling missing values.\n- purpose has nearly normally distributed values hence it can be used.","metadata":{}},{"cell_type":"markdown","source":"---\n# Missing Values Imputation ::","metadata":{}},{"cell_type":"markdown","source":"## Filling missing values of 'bankruptcies' column","metadata":{}},{"cell_type":"code","source":"train.bankruptcies.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.383507Z","iopub.execute_input":"2022-07-27T18:46:53.383789Z","iopub.status.idle":"2022-07-27T18:46:53.395844Z","shell.execute_reply.started":"2022-07-27T18:46:53.383762Z","shell.execute_reply":"2022-07-27T18:46:53.394826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.stats import mode\n\n# Filling with mode value\n\ntrain.bankruptcies =  train.bankruptcies.agg(lambda x : x.fillna(value = 2.0))\n\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', train.bankruptcies.isna().sum())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.397456Z","iopub.execute_input":"2022-07-27T18:46:53.397885Z","iopub.status.idle":"2022-07-27T18:46:53.409803Z","shell.execute_reply.started":"2022-07-27T18:46:53.397841Z","shell.execute_reply":"2022-07-27T18:46:53.408664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.bankruptcies.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.416554Z","iopub.execute_input":"2022-07-27T18:46:53.416883Z","iopub.status.idle":"2022-07-27T18:46:53.424865Z","shell.execute_reply.started":"2022-07-27T18:46:53.416855Z","shell.execute_reply":"2022-07-27T18:46:53.423778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling missing values of 'months_since_last_delinquent' column","metadata":{}},{"cell_type":"code","source":"# Checking values before the filled values \n\ntrain['months_since_last_delinquent'].value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.428505Z","iopub.execute_input":"2022-07-27T18:46:53.429009Z","iopub.status.idle":"2022-07-27T18:46:53.444943Z","shell.execute_reply.started":"2022-07-27T18:46:53.428954Z","shell.execute_reply":"2022-07-27T18:46:53.444122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.446058Z","iopub.execute_input":"2022-07-27T18:46:53.446489Z","iopub.status.idle":"2022-07-27T18:46:53.461885Z","shell.execute_reply.started":"2022-07-27T18:46:53.446456Z","shell.execute_reply":"2022-07-27T18:46:53.460855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filling missing values\n\ngood_cust_df = train[train.credit_default == 0] \nbad_cust_df = train[train.credit_default == 1]\n\n# Setting missing as zero as these are good customers\n# good_cust_df.months_since_last_delinquent = good_cust_df.months_since_last_delinquent.fillna(0)\n# bad_cust_df.months_since_last_delinquent = bad_cust_df.months_since_last_delinquent.fillna(0) \n\ngood_cust_df.months_since_last_delinquent = good_cust_df.months_since_last_delinquent.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:20:51.641206Z","iopub.execute_input":"2022-07-27T19:20:51.641575Z","iopub.status.idle":"2022-07-27T19:20:51.680779Z","shell.execute_reply.started":"2022-07-27T19:20:51.641539Z","shell.execute_reply":"2022-07-27T19:20:51.680062Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df.tax_liens.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:48:02.932145Z","iopub.execute_input":"2022-07-27T18:48:02.932480Z","iopub.status.idle":"2022-07-27T18:48:02.942725Z","shell.execute_reply.started":"2022-07-27T18:48:02.932452Z","shell.execute_reply":"2022-07-27T18:48:02.941554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df1[(bad_cust_df1.months_since_last_delinquent == 0)][\"home_ownership\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:18:41.944974Z","iopub.execute_input":"2022-07-27T19:18:41.945533Z","iopub.status.idle":"2022-07-27T19:18:41.956504Z","shell.execute_reply.started":"2022-07-27T19:18:41.945483Z","shell.execute_reply":"2022-07-27T19:18:41.955625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df[(bad_cust_df.home_ownership == \"Rent\")][\"months_since_last_delinquent\"].value_counts().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:23:57.922479Z","iopub.execute_input":"2022-07-27T19:23:57.923234Z","iopub.status.idle":"2022-07-27T19:23:57.935127Z","shell.execute_reply.started":"2022-07-27T19:23:57.923181Z","shell.execute_reply":"2022-07-27T19:23:57.934136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df[(bad_cust_df.home_ownership == \"Own Home\")][\"months_since_last_delinquent\"].value_counts().sort_values()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:25:35.882473Z","iopub.execute_input":"2022-07-27T19:25:35.882854Z","iopub.status.idle":"2022-07-27T19:25:35.894377Z","shell.execute_reply.started":"2022-07-27T19:25:35.882812Z","shell.execute_reply":"2022-07-27T19:25:35.893258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df1[(bad_cust_df1.months_since_last_delinquent == 0)][\"years_in_current_job\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:08:27.300278Z","iopub.execute_input":"2022-07-27T19:08:27.300784Z","iopub.status.idle":"2022-07-27T19:08:27.311978Z","shell.execute_reply.started":"2022-07-27T19:08:27.300733Z","shell.execute_reply":"2022-07-27T19:08:27.311156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df1[(bad_cust_df1.months_since_last_delinquent == 0)][\"term\"].value_counts(dropna=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:12:45.395274Z","iopub.execute_input":"2022-07-27T19:12:45.395818Z","iopub.status.idle":"2022-07-27T19:12:45.407600Z","shell.execute_reply.started":"2022-07-27T19:12:45.395776Z","shell.execute_reply":"2022-07-27T19:12:45.406140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bad_cust_df.credit_score.value_counts(dropna=False)\nbad_cust_df.credit_score.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:14:36.365608Z","iopub.execute_input":"2022-07-27T19:14:36.365948Z","iopub.status.idle":"2022-07-27T19:14:36.376132Z","shell.execute_reply.started":"2022-07-27T19:14:36.365920Z","shell.execute_reply":"2022-07-27T19:14:36.375316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad_cust_df","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:21:26.284738Z","iopub.execute_input":"2022-07-27T19:21:26.285107Z","iopub.status.idle":"2022-07-27T19:21:26.321870Z","shell.execute_reply.started":"2022-07-27T19:21:26.285071Z","shell.execute_reply":"2022-07-27T19:21:26.321136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# bad_cust_df.months_since_last_delinquent.value_counts(dropna=False)\n# bad_cust_df.years_in_current_job.value_counts(dropna=False)\n# a = pd.DataFrame(bad_cust_df.groupby([\"home_ownership\", \"years_in_current_job\"]).agg())\na = bad_cust_df.groupby([\"home_ownership\", \"years_in_current_job\"]).agg({\"months_since_last_delinquent\" : \"mean\"})\na","metadata":{"execution":{"iopub.status.busy":"2022-07-27T19:01:51.841156Z","iopub.execute_input":"2022-07-27T19:01:51.841644Z","iopub.status.idle":"2022-07-27T19:01:51.860568Z","shell.execute_reply.started":"2022-07-27T19:01:51.841612Z","shell.execute_reply":"2022-07-27T19:01:51.859540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling 'years_in_current_job'  column ::","metadata":{}},{"cell_type":"code","source":"print('Total NaN values present before filling :: ', train.years_in_current_job.isna().sum())\n\n# Displaying total unique values present \n\ntrain.years_in_current_job.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.494504Z","iopub.status.idle":"2022-07-27T18:46:53.495100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Since NaN values present hence filling it using ffill and bfill \n\ntrain.years_in_current_job.fillna(method = 'ffill', inplace = True)\ntrain.years_in_current_job.fillna(method = 'bfill', inplace = True)\n\n\n# Checking for NaN values present\nprint('Total NaN values present after filling :: ', train.years_in_current_job.isna().sum())\n\ntrain.years_in_current_job.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.496077Z","iopub.status.idle":"2022-07-27T18:46:53.496654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling missing values of Annual Income column\n\n- Annual Income and Monthly debt are highly correlated :: 0.58\n- Annual Income and Current credti balance also related :: 0.38\n- So considering both columns to fill missing values of annual income column","metadata":{}},{"cell_type":"markdown","source":"## Visualizing all three column : annual_income, current_credit_balance, monthly_debt","metadata":{}},{"cell_type":"code","source":"# Visualizing to see its relationship with other columns\n\nplt.figure(figsize = (20,8))\nsns.barplot(x = train.number_of_credit_problems, y = train.annual_income, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.497444Z","iopub.status.idle":"2022-07-27T18:46:53.497957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nsns.barplot(x = train.purpose, y = train.annual_income, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.498732Z","iopub.status.idle":"2022-07-27T18:46:53.499273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Again visualizing \n\nplt.figure(figsize = (20,8))\nsns.barplot(x = train.years_in_current_job, y = train.annual_income, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.500308Z","iopub.status.idle":"2022-07-27T18:46:53.500974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nsns.barplot(x = train.number_of_open_accounts, y = train.annual_income, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.501949Z","iopub.status.idle":"2022-07-27T18:46:53.502573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling 'annual_income' column ::","metadata":{}},{"cell_type":"code","source":"# Displaying total unique values\n\ntrain.annual_income.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.503470Z","iopub.status.idle":"2022-07-27T18:46:53.504088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val = train.groupby(['purpose','years_in_current_job'])['annual_income']\nval.agg([np.mean, np.median, np.size])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.504972Z","iopub.status.idle":"2022-07-27T18:46:53.505595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tranforming/applying mean values to corresponding rows with missing values\n\ntrain.annual_income = val.transform(lambda x : x.fillna(x.median()))\ntrain.annual_income = train.annual_income.fillna(method = 'ffill')\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', train.annual_income.isna().sum())\n\n#Displaying values\n\ntrain.annual_income.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.506494Z","iopub.status.idle":"2022-07-27T18:46:53.507100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nsns.boxplot(train.annual_income, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.507976Z","iopub.status.idle":"2022-07-27T18:46:53.508600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling 'credit_score' column ::","metadata":{}},{"cell_type":"code","source":"# Displaying total unique values\n\ntrain.credit_score.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.509485Z","iopub.status.idle":"2022-07-27T18:46:53.510096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Visualizing to see its relationship with other categorial features","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = train.tax_liens, y = 'credit_score', data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.510969Z","iopub.status.idle":"2022-07-27T18:46:53.511598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = train.term, y = 'credit_score', hue = train.years_in_current_job,data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.512483Z","iopub.status.idle":"2022-07-27T18:46:53.513108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = 'years_in_current_job', y = 'credit_score', hue = train.tax_liens,data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.514011Z","iopub.status.idle":"2022-07-27T18:46:53.514613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = 'home_ownership', y = 'credit_score', data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.515482Z","iopub.status.idle":"2022-07-27T18:46:53.516065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = train.bankruptcies, y = 'credit_score',data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.516910Z","iopub.status.idle":"2022-07-27T18:46:53.517474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,8))\nsns.boxplot(x = train.credit_score, y = train.years_in_current_job \t, data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.518359Z","iopub.status.idle":"2022-07-27T18:46:53.518926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (17,7))\nsns.barplot(x = train.number_of_credit_problems, y = 'credit_score', data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.519835Z","iopub.status.idle":"2022-07-27T18:46:53.520279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.credit_score.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.521307Z","iopub.status.idle":"2022-07-27T18:46:53.521733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_score2 = train.groupby('purpose')['credit_score']\n\nmean_score2.agg(np.median)             # Since value are close so using this","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.522921Z","iopub.status.idle":"2022-07-27T18:46:53.523385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.credit_score = pd.Series(mean_score2.transform(lambda x : x.fillna(value = x.median())))\n\n# Since one category has no values in barchart hence using ffill to fill it\n#train.credit_score.fillna(method = 'ffill', inplace = True)\n\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', train.credit_score.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.524563Z","iopub.status.idle":"2022-07-27T18:46:53.525000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying values\n\ntrain.credit_score.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.526186Z","iopub.status.idle":"2022-07-27T18:46:53.526621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking for NaN values\n\ntrain.agg(lambda x : x.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.527599Z","iopub.status.idle":"2022-07-27T18:46:53.528045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_int_cols = list(train.select_dtypes(include = np.number).columns)\ntrain_obj_cols = list(train.select_dtypes(include = np.object).columns) ","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.529210Z","iopub.status.idle":"2022-07-27T18:46:53.529636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization for Outliers ::","metadata":{}},{"cell_type":"code","source":"# Plotting all int/float columns boxplot\n\nplt.figure(figsize = (20,15))\nsns.boxplot(data = train[train_int_cols], orient = 'h')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.530806Z","iopub.status.idle":"2022-07-27T18:46:53.531266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Maimum Open credit has some outliers hence treating it first","metadata":{}},{"cell_type":"markdown","source":"- Since the some values seems to be an outlier hence taking log of all values\n- Since some values in column are zero hence adding 1 to every value since log(1) == 0\n- Also taking first those columns that have big values in respective columns","metadata":{}},{"cell_type":"markdown","source":"### Since target variable is also present in integer columns hence dropping it before transfoming the values. ","metadata":{}},{"cell_type":"code","source":"train_int_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.532655Z","iopub.status.idle":"2022-07-27T18:46:53.533095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping last item\ntrain_int_cols_temp = train_int_cols[ :-1]\ntrain_int_cols_temp\n\ntrain[train_int_cols_temp]= train[train_int_cols_temp].transform(lambda x : np.log(x + 1))\ntrain[train_int_cols_temp].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.534314Z","iopub.status.idle":"2022-07-27T18:46:53.534748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting all int/float columns boxplot\n\nplt.figure(figsize = (20,15))\nsns.boxplot(data = train[train_int_cols], orient = 'h')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.536030Z","iopub.status.idle":"2022-07-27T18:46:53.536462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = ['annual_income', 'maximum_open_credit', 'current_loan_amount', 'current_credit_balance', 'monthly_debt']\n\ntrain[cols].plot(kind = 'box', figsize = (20,15))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.537474Z","iopub.status.idle":"2022-07-27T18:46:53.537904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train_int_cols].describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.539101Z","iopub.status.idle":"2022-07-27T18:46:53.539531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Seems that all values have been taken care of. Now proceeding further","metadata":{}},{"cell_type":"code","source":"train[train_obj_cols].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.540457Z","iopub.status.idle":"2022-07-27T18:46:53.540888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy = pd.get_dummies(train[train_obj_cols])\ndummy.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.542186Z","iopub.status.idle":"2022-07-27T18:46:53.542613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_final = pd.concat([train[train_int_cols], dummy], axis = 1)\ntrain_final","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.543845Z","iopub.status.idle":"2022-07-27T18:46:53.544306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Making Models ::","metadata":{}},{"cell_type":"code","source":"x = train_final.drop('credit_default', axis = 1)\ny = train_final.credit_default","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.545370Z","iopub.status.idle":"2022-07-27T18:46:53.545805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 100)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.546889Z","iopub.status.idle":"2022-07-27T18:46:53.547344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble  import RandomForestClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.548565Z","iopub.status.idle":"2022-07-27T18:46:53.549002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_dt = DecisionTreeClassifier(random_state = 10)\nmodel_rf = RandomForestClassifier(random_state = 11)\nmodel_knn = KNeighborsClassifier(n_neighbors = 10, n_jobs = -1)\n#model_lr = LogisticRegression(random_state = 12)\nmodel_nb = GaussianNB()\nmodel_svc = SVC(random_state = 14)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.550262Z","iopub.status.idle":"2022-07-27T18:46:53.550694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_final.credit_default.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.551802Z","iopub.status.idle":"2022-07-27T18:46:53.552240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, classification_report\n\nscore_values = []\ndef modelling(models, x_train, y_train, x_test, y_test):\n    for model in models:\n        model.fit(x_train, y_train)\n        predict = model.predict(x_test)\n        score = accuracy_score(y_test, predict)\n        score_values.append(score)\n        print(f\"Accurace score of {model} is :: \", score)\n        print(f\"Classification Report is :: \\n\")\n        print(classification_report(y_test, predict))\n        print('*' * 90)\n\n        \n# using the function\n\nmodels = [model_dt, model_knn, model_nb, model_rf, model_svc]\nmodelling(models, x_train, y_train, x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.553475Z","iopub.status.idle":"2022-07-27T18:46:53.553907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Observation ::\n\n- SVC and RandomForest have same accuracy = 77%\n- naive Bayes have accuracy = 75%\n- Knn have accuracy = 76%\n- Decision Tree have accuracy = 69%","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score, roc_curve\n\n# calculating log probabilities of models used\n\nprob_dt = model_dt.predict_proba(x_test)\nprob_rf = model_rf.predict_proba(x_test)\nprob_knn = model_knn.predict_proba(x_test)\nprob_nb = model_nb.predict_proba(x_test)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.555150Z","iopub.status.idle":"2022-07-27T18:46:53.555582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Keep Probabilities of the positive class only.\n\nprob_dt = prob_dt[:, 1]\nprob_rf = prob_rf[:, 1]\nprob_knn = prob_knn[:, 1]\nprob_nb = prob_nb[:, 1]","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.556737Z","iopub.status.idle":"2022-07-27T18:46:53.557185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculating the auc score and displaying it\n\nauc_dt = roc_auc_score(y_test, prob_dt)\nauc_rf = roc_auc_score(y_test, prob_rf)\nauc_knn = roc_auc_score(y_test, prob_knn)\nauc_nb = roc_auc_score(y_test, prob_nb)\n\nprint('ROC- AUC Score for Decision Tree is :: %0.3f'%auc_dt)\nprint('ROC- AUC Score for Random Forest is :: %0.3f'%auc_rf)\nprint('ROC- AUC Score for KNN is :: %0.3f'%auc_knn)\nprint('ROC- AUC Score for Niave Bayes is :: %0.3f'%auc_nb)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.558300Z","iopub.status.idle":"2022-07-27T18:46:53.558717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Getting the ROC Curve.\n\nfpr_dt, tpr_dt, threshold_dt = roc_curve(y_test, prob_dt)\nfpr_rf, tpr_rf, threshold_rf = roc_curve(y_test, prob_rf)\nfpr_knn, tpr_knn, threshold_knn = roc_curve(y_test, prob_knn)\nfpr_nb, tpr_nb, threshold_nb = roc_curve(y_test, prob_nb)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.559807Z","iopub.status.idle":"2022-07-27T18:46:53.560241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting the ROC Curve for all models \n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nplt.figure(figsize = (15,7))\nplt.plot(fpr_dt, tpr_dt, linewidth = 2, linestyle = 'dotted', label = 'Decision Tree')\nplt.plot(fpr_rf, tpr_rf, linewidth = 2, linestyle = 'dashdot', label = 'Random Forest')\nplt.plot(fpr_knn, tpr_knn, linewidth = 2, linestyle = 'dashed', label = 'K-Neighbours')\nplt.plot(fpr_nb, tpr_nb, linewidth = 2, linestyle = '-', label = 'Naive Bayes')\n\nplt.title('Receiver Operating Characteristic Curve (ROC AUC) Curve', fontsize = 25)\nplt.xlabel('False Positive Rate', fontsize = 20)\nplt.ylabel('True Positive Rate', fontsize = 20)\nplt.legend(fontsize = 16)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.561310Z","iopub.status.idle":"2022-07-27T18:46:53.562209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Performing actions on Test Data ::","metadata":{}},{"cell_type":"markdown","source":"---\n# Missing Values Imputation ::","metadata":{}},{"cell_type":"markdown","source":"## Filling missing values of 'bankruptcies' column","metadata":{}},{"cell_type":"code","source":"from scipy.stats import mode\n\n# Filling with mode value\n\ntest.bankruptcies =  test.bankruptcies.agg(lambda x : x.fillna(value = mode(x).mode[0]))\n\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', test.bankruptcies.isna().sum())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.563296Z","iopub.status.idle":"2022-07-27T18:46:53.563760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling missing values of 'months_since_last_delinquent' column","metadata":{}},{"cell_type":"code","source":"# Checking values before the filled values \n\ntest['months_since_last_delinquent'].value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.564814Z","iopub.status.idle":"2022-07-27T18:46:53.565264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grouping months_since_last_delinquent columns as per  'purpose', 'home_ownership'\n\nmean_score_t = test.groupby(['purpose', 'home_ownership'])['months_since_last_delinquent']\nmean_score_t.agg([np.median])","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.566511Z","iopub.status.idle":"2022-07-27T18:46:53.566955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.loc[: , 'months_since_last_delinquent'] = mean_score_t.transform(lambda x : x.fillna(x.median()))\n\n#Using ffill to fill any left missing value\n\ntest.months_since_last_delinquent = test.months_since_last_delinquent.fillna(method = 'ffill')\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', test.months_since_last_delinquent.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.568313Z","iopub.status.idle":"2022-07-27T18:46:53.568757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling 'years_in_current_job'  column ::","metadata":{}},{"cell_type":"code","source":"print('Total NaN values present before filling :: ', test.years_in_current_job.isna().sum())\n\n# Displaying total unique values present \n\ntest.years_in_current_job.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.569883Z","iopub.status.idle":"2022-07-27T18:46:53.570345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Since NaN values present hence filling it using ffill and bfill \n\ntest.years_in_current_job.fillna(method = 'ffill', inplace = True)\ntest.years_in_current_job.fillna(method = 'bfill', inplace = True)\n\n\n# Checking for NaN values present\nprint('Total NaN values present after filling :: ', test.years_in_current_job.isna().sum())\n\ntest.years_in_current_job.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.571495Z","iopub.status.idle":"2022-07-27T18:46:53.571930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling missing values of Annual Income column\n\n- Annual Income and Monthly debt are highly correlated :: 0.58\n- Annual Income and Current credti balance also related :: 0.38\n- So considering both columns to fill missing values of annual income column","metadata":{}},{"cell_type":"markdown","source":"## Filling 'annual_income' column ::","metadata":{}},{"cell_type":"code","source":"# Displaying total unique values\n\ntest.annual_income.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.573082Z","iopub.status.idle":"2022-07-27T18:46:53.573509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tranforming/applying mean values to corresponding rows with missing values\n\ntest.annual_income = test.groupby('years_in_current_job')['annual_income'].transform(lambda x : x.fillna(x.mean()))\n\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', test.annual_income.isna().sum())\n\n#Displaying values\n\ntest.annual_income.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.574725Z","iopub.status.idle":"2022-07-27T18:46:53.575192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling 'credit_score' column ::","metadata":{}},{"cell_type":"code","source":"# Displaying total unique values\n\ntest.credit_score.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.576569Z","iopub.status.idle":"2022-07-27T18:46:53.577015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_score_t2 = test.groupby('purpose')['credit_score']\n\nmean_score_t2.agg(np.median)             # Since value are close so using this","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.578123Z","iopub.status.idle":"2022-07-27T18:46:53.578553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.credit_score = mean_score_t2.transform(lambda x : x.fillna(value = x.median()))\n\n# Since one category has no values in barchart hence using ffill to fill it\n#train.credit_score.fillna(method = 'ffill', inplace = True)\n\n# Checking for NaN values present\n\nprint('Total NaN values present after filling :: ', test.credit_score.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.579601Z","iopub.status.idle":"2022-07-27T18:46:53.580045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying values\n\ntest.credit_score.value_counts(dropna = False)","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.581352Z","iopub.status.idle":"2022-07-27T18:46:53.581803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking for NaN values\n\ntest.agg(lambda x : x.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.582968Z","iopub.status.idle":"2022-07-27T18:46:53.583422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_int_cols = list(test.select_dtypes(include = np.number).columns)\ntest_obj_cols = list(test.select_dtypes(include = np.object).columns) ","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.584696Z","iopub.status.idle":"2022-07-27T18:46:53.585160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Visualization for Outliers ::","metadata":{}},{"cell_type":"code","source":"# Plotting all int/float columns boxplot\n\nplt.figure(figsize = (20,15))\nsns.boxplot(data = test[test_int_cols], orient = 'h')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.586487Z","iopub.status.idle":"2022-07-27T18:46:53.586924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Maimum Open credit has some outliers hence treating it first","metadata":{}},{"cell_type":"markdown","source":"- Since the some values seems to be an outlier hence taking log of all values\n- Since some values in column are zero hence adding 1 to every value since log(1) == 0\n- Also taking first those columns that have big values in respective columns","metadata":{}},{"cell_type":"markdown","source":"### Since target variable is also present in integer columns hence dropping it before transfoming the values. ","metadata":{}},{"cell_type":"code","source":"test_int_cols","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.588615Z","iopub.status.idle":"2022-07-27T18:46:53.589086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[test_int_cols]= test[test_int_cols].transform(lambda x : np.log(x + 1))\ntest[test_int_cols].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.590193Z","iopub.status.idle":"2022-07-27T18:46:53.590652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting all int/float columns boxplot\n\nplt.figure(figsize = (20,15))\nsns.boxplot(data = test[test_int_cols], orient = 'h')","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.591736Z","iopub.status.idle":"2022-07-27T18:46:53.592199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[test_int_cols].describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.593320Z","iopub.status.idle":"2022-07-27T18:46:53.593758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- Seems that all values have been taken care of. Now proceeding further","metadata":{}},{"cell_type":"code","source":"test[test_obj_cols].isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.594812Z","iopub.status.idle":"2022-07-27T18:46:53.595256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dummy_t = pd.get_dummies(test[test_obj_cols])\ndummy_t.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.596505Z","iopub.status.idle":"2022-07-27T18:46:53.596937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_final = pd.concat([test[test_int_cols], dummy_t], axis = 1)\ntest_final.T","metadata":{"execution":{"iopub.status.busy":"2022-07-27T18:46:53.598126Z","iopub.status.idle":"2022-07-27T18:46:53.598568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"prediction_dt = model_dt.predict(test_final)\nprediction_knn = model_knn.predict(test_final)\nprediction_nb = model_nb.predict(test_final)\nprediction_rf = model_rf.predict(test_final)\nprediction_svc = model_svc.predict(test_final)","metadata":{}},{"cell_type":"raw","source":"submission_result_dt = pd.DataFrame({'Id': test_final.index, 'Credit Default' : prediction_dt})\nsubmission_result_dt.to_csv('submission_result_dt.csv', index=False)\n\nsubmission_result_knn = pd.DataFrame({'Id': test_final.index, 'Credit Default' : prediction_knn})\nsubmission_result_knn.to_csv('submission_result_knn.csv', index=False)\n\nsubmission_result_nb = pd.DataFrame({'Id': test_final.index, 'Credit Default' : prediction_nb})\nsubmission_result_knn.to_csv('submission_result_nb.csv', index=False)\n\nsubmission_result_rf = pd.DataFrame({'Id': test_final.index, 'Credit Default' : prediction_rf})\nsubmission_result_knn.to_csv('submission_result_rf.csv', index=False)\n\nsubmission_result_svc = pd.DataFrame({'Id': test_final.index, 'Credit Default' : prediction_svc})\nsubmission_result_knn.to_csv('submission_result_svc.csv', index=False)","metadata":{}}]}