{"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":"2023-02-05T11:13:44.282193Z","iopub.execute_input":"2023-02-05T11:13:44.283257Z","iopub.status.idle":"2023-02-05T11:13:44.325059Z","shell.execute_reply.started":"2023-02-05T11:13:44.282613Z","shell.execute_reply":"2023-02-05T11:13:44.324061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/amex-default-prediction/train_data.csv', nrows = 200000)\n#df_test = pd.read_csv('/kaggle/input/amex-default-prediction/test_data.csv', nrows = 200000)\ndf_train_labels = pd.read_csv('/kaggle/input/amex-default-prediction/train_labels.csv', nrows = 200000)\n#df_submission = pd.read_csv('/kaggle/input/amex-default-prediction/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:14:04.127754Z","iopub.execute_input":"2023-02-05T11:14:04.128241Z","iopub.status.idle":"2023-02-05T11:14:20.571701Z","shell.execute_reply.started":"2023-02-05T11:14:04.128204Z","shell.execute_reply":"2023-02-05T11:14:20.570617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.merge(df_train, df_train_labels, how=\"inner\", on=[\"customer_ID\"])","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:14:40.003877Z","iopub.execute_input":"2023-02-05T11:14:40.005033Z","iopub.status.idle":"2023-02-05T11:14:40.829158Z","shell.execute_reply.started":"2023-02-05T11:14:40.004990Z","shell.execute_reply":"2023-02-05T11:14:40.827902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:14:44.855341Z","iopub.execute_input":"2023-02-05T11:14:44.856108Z","iopub.status.idle":"2023-02-05T11:14:44.893513Z","shell.execute_reply.started":"2023-02-05T11:14:44.856066Z","shell.execute_reply":"2023-02-05T11:14:44.892471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.tail()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:14:49.048419Z","iopub.execute_input":"2023-02-05T11:14:49.048865Z","iopub.status.idle":"2023-02-05T11:14:49.075707Z","shell.execute_reply.started":"2023-02-05T11:14:49.048803Z","shell.execute_reply":"2023-02-05T11:14:49.074656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:14:51.859375Z","iopub.execute_input":"2023-02-05T11:14:51.859772Z","iopub.status.idle":"2023-02-05T11:14:51.868136Z","shell.execute_reply.started":"2023-02-05T11:14:51.859741Z","shell.execute_reply":"2023-02-05T11:14:51.866828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:13.741101Z","iopub.execute_input":"2023-02-05T11:16:13.741621Z","iopub.status.idle":"2023-02-05T11:16:13.750233Z","shell.execute_reply.started":"2023-02-05T11:16:13.741574Z","shell.execute_reply":"2023-02-05T11:16:13.748981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:16.634326Z","iopub.execute_input":"2023-02-05T11:16:16.634777Z","iopub.status.idle":"2023-02-05T11:16:20.860631Z","shell.execute_reply.started":"2023-02-05T11:16:16.634740Z","shell.execute_reply":"2023-02-05T11:16:20.859448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:23.275180Z","iopub.execute_input":"2023-02-05T11:16:23.275635Z","iopub.status.idle":"2023-02-05T11:16:23.396642Z","shell.execute_reply.started":"2023-02-05T11:16:23.275600Z","shell.execute_reply":"2023-02-05T11:16:23.395279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nan_cols = [i for i in df_train.columns if df_train[i].isnull().any()]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:26.481969Z","iopub.execute_input":"2023-02-05T11:16:26.482366Z","iopub.status.idle":"2023-02-05T11:16:26.587586Z","shell.execute_reply.started":"2023-02-05T11:16:26.482335Z","shell.execute_reply":"2023-02-05T11:16:26.586581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nan_cols","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:33.022761Z","iopub.execute_input":"2023-02-05T11:16:33.023179Z","iopub.status.idle":"2023-02-05T11:16:33.032991Z","shell.execute_reply.started":"2023-02-05T11:16:33.023146Z","shell.execute_reply":"2023-02-05T11:16:33.031523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:39.106928Z","iopub.execute_input":"2023-02-05T11:16:39.107324Z","iopub.status.idle":"2023-02-05T11:16:39.127409Z","shell.execute_reply.started":"2023-02-05T11:16:39.107294Z","shell.execute_reply":"2023-02-05T11:16:39.126106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:16:45.024700Z","iopub.execute_input":"2023-02-05T11:16:45.025241Z","iopub.status.idle":"2023-02-05T11:16:47.463254Z","shell.execute_reply.started":"2023-02-05T11:16:45.025192Z","shell.execute_reply":"2023-02-05T11:16:47.461950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.nunique()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:06.076570Z","iopub.execute_input":"2023-02-05T11:17:06.076997Z","iopub.status.idle":"2023-02-05T11:17:08.016966Z","shell.execute_reply.started":"2023-02-05T11:17:06.076966Z","shell.execute_reply":"2023-02-05T11:17:08.015711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_cols = ['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:14.167877Z","iopub.execute_input":"2023-02-05T11:17:14.168258Z","iopub.status.idle":"2023-02-05T11:17:14.173572Z","shell.execute_reply.started":"2023-02-05T11:17:14.168228Z","shell.execute_reply":"2023-02-05T11:17:14.172385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:17.801054Z","iopub.execute_input":"2023-02-05T11:17:17.801956Z","iopub.status.idle":"2023-02-05T11:17:18.960159Z","shell.execute_reply.started":"2023-02-05T11:17:17.801905Z","shell.execute_reply":"2023-02-05T11:17:18.959087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_cat = df_train[['B_30', 'B_38', 'D_114', 'D_116', 'D_117', 'D_120', 'D_126', 'D_63', 'D_64', 'D_66', 'D_68']]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:21.550872Z","iopub.execute_input":"2023-02-05T11:17:21.551620Z","iopub.status.idle":"2023-02-05T11:17:21.565134Z","shell.execute_reply.started":"2023-02-05T11:17:21.551572Z","shell.execute_reply":"2023-02-05T11:17:21.563603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df_train_cat.columns:\n    print(i)\n    print(df_train_cat[i].unique())\n    print(df_train_cat[i].value_counts())\n    print('\\n')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:27.923482Z","iopub.execute_input":"2023-02-05T11:17:27.924593Z","iopub.status.idle":"2023-02-05T11:17:28.031872Z","shell.execute_reply.started":"2023-02-05T11:17:27.924552Z","shell.execute_reply":"2023-02-05T11:17:28.030603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:33.639417Z","iopub.execute_input":"2023-02-05T11:17:33.639863Z","iopub.status.idle":"2023-02-05T11:17:33.644804Z","shell.execute_reply.started":"2023-02-05T11:17:33.639804Z","shell.execute_reply":"2023-02-05T11:17:33.643443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df_train_cat.columns:\n    plt.figure(figsize=(15,6))\n    sns.countplot(df_train_cat[i], data = df_train_cat, palette = 'hls')\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:36.880233Z","iopub.execute_input":"2023-02-05T11:17:36.880620Z","iopub.status.idle":"2023-02-05T11:17:40.013005Z","shell.execute_reply.started":"2023-02-05T11:17:36.880590Z","shell.execute_reply":"2023-02-05T11:17:40.012000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df_train_cat.columns:\n    plt.figure(figsize=(20,8))\n    df_train_cat[i].value_counts().plot(kind = 'pie',autopct='%1.1f%%', startangle=90)\n    plt.xticks(rotation = 90)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:45.706569Z","iopub.execute_input":"2023-02-05T11:17:45.707626Z","iopub.status.idle":"2023-02-05T11:17:47.194002Z","shell.execute_reply.started":"2023-02-05T11:17:45.707586Z","shell.execute_reply":"2023-02-05T11:17:47.192844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sum(df_train.isna().sum())","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:17:57.116570Z","iopub.execute_input":"2023-02-05T11:17:57.117003Z","iopub.status.idle":"2023-02-05T11:17:57.234809Z","shell.execute_reply.started":"2023-02-05T11:17:57.116967Z","shell.execute_reply":"2023-02-05T11:17:57.233396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"custom_colors = [\"#ffd670\",\"#70d6ff\",\"#ff4d6d\",\"#8338ec\",\"#90cf8e\"]\ncustomPalette = sns.set_palette(sns.color_palette(custom_colors))\nsns.palplot(sns.color_palette(custom_colors),size=1.2)\nplt.tick_params(axis='both', labelsize=0, length = 0)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:13.796370Z","iopub.execute_input":"2023-02-05T11:19:13.796789Z","iopub.status.idle":"2023-02-05T11:19:13.883056Z","shell.execute_reply.started":"2023-02-05T11:19:13.796757Z","shell.execute_reply":"2023-02-05T11:19:13.880925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"background_color = 'white'\nmissing = pd.DataFrame(columns = ['% Missing values'],data = df_train.isnull().sum()/len(df_train))\nfig = plt.figure(figsize = (20, 60),facecolor=background_color)\ngs = fig.add_gridspec(1, 2)\ngs.update(wspace = 0.5, hspace = 0.5)\nax0 = fig.add_subplot(gs[0, 0])\nfor s in [\"right\", \"top\",\"bottom\",\"left\"]:\n    ax0.spines[s].set_visible(False)\nsns.heatmap(missing,cbar = False,annot = True,fmt =\".2%\", linewidths = 2,cmap = custom_colors,vmax = 1, ax = ax0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:16.246884Z","iopub.execute_input":"2023-02-05T11:19:16.247292Z","iopub.status.idle":"2023-02-05T11:19:19.495572Z","shell.execute_reply.started":"2023-02-05T11:19:16.247258Z","shell.execute_reply":"2023-02-05T11:19:19.494337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.groupby('customer_ID').tail(1).set_index('customer_ID')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:30.050758Z","iopub.execute_input":"2023-02-05T11:19:30.052003Z","iopub.status.idle":"2023-02-05T11:19:30.144533Z","shell.execute_reply.started":"2023-02-05T11:19:30.051951Z","shell.execute_reply":"2023-02-05T11:19:30.143637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_Delinquency = [c for c in df_train.columns if c.startswith('D_')]\nfeat_Spend = [c for c in df_train.columns if c.startswith('S_')]\nfeat_Payment = [c for c in df_train.columns if c.startswith('P_')]\nfeat_Balance = [c for c in df_train.columns if c.startswith('B_')]\nfeat_Risk = [c for c in df_train.columns if c.startswith('R_')]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:52.921124Z","iopub.execute_input":"2023-02-05T11:19:52.921736Z","iopub.status.idle":"2023-02-05T11:19:52.928619Z","shell.execute_reply.started":"2023-02-05T11:19:52.921702Z","shell.execute_reply":"2023-02-05T11:19:52.927739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Total number of Delinquency variables: {len(feat_Delinquency)}')\nprint(f'Total number of Spend variables: {len(feat_Spend)}')\nprint(f'Total number of Payment variables: {len(feat_Payment)}')\nprint(f'Total number of Balance variables: {len(feat_Balance)}')\nprint(f'Total number of Risk variables: {len(feat_Risk)}')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:55.935462Z","iopub.execute_input":"2023-02-05T11:19:55.936095Z","iopub.status.idle":"2023-02-05T11:19:55.941453Z","shell.execute_reply.started":"2023-02-05T11:19:55.936061Z","shell.execute_reply":"2023-02-05T11:19:55.940657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=['Delinquency', 'Spend','Payment','Balance','Risk']\nvalues= [len(feat_Delinquency), len(feat_Spend),len(feat_Payment), len(feat_Balance),len(feat_Risk)]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:19:59.740330Z","iopub.execute_input":"2023-02-05T11:19:59.741058Z","iopub.status.idle":"2023-02-05T11:19:59.747498Z","shell.execute_reply.started":"2023-02-05T11:19:59.741008Z","shell.execute_reply":"2023-02-05T11:19:59.746239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.graph_objects as go\nimport plotly.express as px\nfrom itertools import cycle","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:20:38.840782Z","iopub.execute_input":"2023-02-05T11:20:38.841290Z","iopub.status.idle":"2023-02-05T11:20:38.847121Z","shell.execute_reply.started":"2023-02-05T11:20:38.841186Z","shell.execute_reply":"2023-02-05T11:20:38.845763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig_1 = go.Figure()\nfig_1.add_trace(go.Pie(values = values,labels = labels,hole = 0.6, \n                     hoverinfo ='label+percent'))\nfig_1.update_traces(textfont_size = 12, hoverinfo ='label+percent',textinfo ='label', \n                  showlegend = False,marker = dict(colors =[\"#70d6ff\",\"#ff9770\"]),\n                  title = dict(text = 'Feature Distribution'))  \nfig_1.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:25.367629Z","iopub.execute_input":"2023-02-05T11:23:25.368068Z","iopub.status.idle":"2023-02-05T11:23:25.383736Z","shell.execute_reply.started":"2023-02-05T11:23:25.368036Z","shell.execute_reply":"2023-02-05T11:23:25.382470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['target'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:29.248556Z","iopub.execute_input":"2023-02-05T11:23:29.249003Z","iopub.status.idle":"2023-02-05T11:23:29.257950Z","shell.execute_reply.started":"2023-02-05T11:23:29.248964Z","shell.execute_reply":"2023-02-05T11:23:29.256742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:31.376341Z","iopub.execute_input":"2023-02-05T11:23:31.377627Z","iopub.status.idle":"2023-02-05T11:23:31.392656Z","shell.execute_reply.started":"2023-02-05T11:23:31.377560Z","shell.execute_reply":"2023-02-05T11:23:31.390932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\nsns.countplot(df_train['target'], data = df_train, palette = 'hls')\nplt.xticks(rotation = 90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:33.720593Z","iopub.execute_input":"2023-02-05T11:23:33.721449Z","iopub.status.idle":"2023-02-05T11:23:33.930537Z","shell.execute_reply.started":"2023-02-05T11:23:33.721390Z","shell.execute_reply":"2023-02-05T11:23:33.929237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\ndf_train['target'].value_counts().plot(kind = 'pie',autopct='%1.1f%%', startangle=90)\nplt.xticks(rotation = 90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:36.104991Z","iopub.execute_input":"2023-02-05T11:23:36.105708Z","iopub.status.idle":"2023-02-05T11:23:36.237495Z","shell.execute_reply.started":"2023-02-05T11:23:36.105663Z","shell.execute_reply":"2023-02-05T11:23:36.236051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_class = pd.DataFrame({'count': df_train.target.value_counts(),\n                             'percentage': df_train['target'].value_counts() / df_train.shape[0] * 100\n})","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:39.492702Z","iopub.execute_input":"2023-02-05T11:23:39.493143Z","iopub.status.idle":"2023-02-05T11:23:39.502292Z","shell.execute_reply.started":"2023-02-05T11:23:39.493110Z","shell.execute_reply":"2023-02-05T11:23:39.500916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_class ","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:41.451206Z","iopub.execute_input":"2023-02-05T11:23:41.451679Z","iopub.status.idle":"2023-02-05T11:23:41.463154Z","shell.execute_reply.started":"2023-02-05T11:23:41.451645Z","shell.execute_reply":"2023-02-05T11:23:41.461878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure()\nfig.add_trace(go.Pie(values = target_class['count'],labels = target_class.index,hole = 0.6, \n                     hoverinfo ='label+percent'))\nfig.update_traces(textfont_size = 12, hoverinfo ='label+percent',textinfo ='label', \n                  showlegend = False,marker = dict(colors =[\"#90cf8e\",\"#ff70a6\"]),\n                  title = dict(text = 'Target Distribution'))  \nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:43.742571Z","iopub.execute_input":"2023-02-05T11:23:43.742991Z","iopub.status.idle":"2023-02-05T11:23:43.759983Z","shell.execute_reply.started":"2023-02-05T11:23:43.742957Z","shell.execute_reply":"2023-02-05T11:23:43.758614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stat_plot = df_train.reset_index().groupby('S_2')['customer_ID'].nunique().reset_index()\nfig = go.Figure()\nfig.add_trace(go.Scatter(x = stat_plot['S_2'], y = stat_plot['customer_ID']))\nfig.update_layout(title=\"Customer Statements\", width = 800, height = 600,xaxis_title ='Statement Date',\n                  paper_bgcolor='rgb(0,0,0,0)',plot_bgcolor='rgb(0,0,0,0)') \nfig['data'][0]['line']['color']=\"#ff9770\"\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:23:49.290208Z","iopub.execute_input":"2023-02-05T11:23:49.290709Z","iopub.status.idle":"2023-02-05T11:23:49.391080Z","shell.execute_reply.started":"2023-02-05T11:23:49.290672Z","shell.execute_reply":"2023-02-05T11:23:49.389603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:25:48.374754Z","iopub.execute_input":"2023-02-05T11:25:48.375284Z","iopub.status.idle":"2023-02-05T11:25:48.380952Z","shell.execute_reply.started":"2023-02-05T11:25:48.375244Z","shell.execute_reply":"2023-02-05T11:25:48.379644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:25:51.986016Z","iopub.execute_input":"2023-02-05T11:25:51.986746Z","iopub.status.idle":"2023-02-05T11:25:52.142691Z","shell.execute_reply.started":"2023-02-05T11:25:51.986697Z","shell.execute_reply":"2023-02-05T11:25:52.141452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.drop('S_2', axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:25:55.675182Z","iopub.execute_input":"2023-02-05T11:25:55.675780Z","iopub.status.idle":"2023-02-05T11:25:55.700383Z","shell.execute_reply.started":"2023-02-05T11:25:55.675749Z","shell.execute_reply":"2023-02-05T11:25:55.699224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del_cols = [c for c in df_train.columns if (c.startswith(('D','t'))) & (c not in cat_cols)]\ndf_del = df_train[del_cols]\nspd_cols = [c for c in df_train.columns if (c.startswith(('S','t'))) & (c not in cat_cols)]\ndf_spd = df_train[spd_cols]\npay_cols = [c for c in df_train.columns if (c.startswith(('P','t'))) & (c not in cat_cols)]\ndf_pay = df_train[pay_cols]\nbal_cols = [c for c in df_train.columns if (c.startswith(('B','t'))) & (c not in cat_cols)]\ndf_bal = df_train[bal_cols]\nris_cols = [c for c in df_train.columns if (c.startswith(('R','t'))) & (c not in cat_cols)]\ndf_ris = df_train[ris_cols]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:26:11.621350Z","iopub.execute_input":"2023-02-05T11:26:11.621783Z","iopub.status.idle":"2023-02-05T11:26:11.641600Z","shell.execute_reply.started":"2023-02-05T11:26:11.621749Z","shell.execute_reply":"2023-02-05T11:26:11.640313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(29, 3, figsize = (35,150))\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i < len(del_cols) - 1:\n        sns.kdeplot(x = del_cols[i], hue='target', data = df_del, fill = True, ax = ax, palette =[\"#e63946\",\"#8338ec\"])\n        ax.tick_params()\n        ax.xaxis.get_label()\n        ax.set_ylabel('')\nfig.suptitle('Distribution of Delinquency Variables', fontsize = 35, x = 0.5, y = 1)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:26:20.247301Z","iopub.execute_input":"2023-02-05T11:26:20.247731Z","iopub.status.idle":"2023-02-05T11:26:47.631303Z","shell.execute_reply.started":"2023-02-05T11:26:20.247685Z","shell.execute_reply":"2023-02-05T11:26:47.629036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize =(11,11))\ncorr = df_del.corr()\nmask = np.triu(np.ones_like(corr, dtype = bool))\nsns.heatmap(corr, mask = mask, robust = True, center = 0,square = True, linewidths =.6, cmap = custom_colors)\nplt.title('Correlation of Delinquency Variables')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:05.146596Z","iopub.execute_input":"2023-02-05T11:27:05.147040Z","iopub.status.idle":"2023-02-05T11:27:06.358071Z","shell.execute_reply.started":"2023-02-05T11:27:05.147004Z","shell.execute_reply":"2023-02-05T11:27:06.356731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(8, 3, figsize = (16,18))\nfig.suptitle('Distribution of Spend Variables', fontsize = 15, x = 0.5, y = 1)\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i < len(spd_cols) - 1:\n        sns.kdeplot(x = spd_cols[i], hue ='target', data = df_spd, fill = True, ax = ax, palette =[\"#e63946\",\"#8338ec\"])\n        ax.tick_params()\n        ax.xaxis.get_label()\n        ax.set_ylabel('')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:12.750270Z","iopub.execute_input":"2023-02-05T11:27:12.751061Z","iopub.status.idle":"2023-02-05T11:27:19.438545Z","shell.execute_reply.started":"2023-02-05T11:27:12.751022Z","shell.execute_reply":"2023-02-05T11:27:19.437031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"S_cols = [c for c in df_train.columns if (c.startswith(('S')))]\ndf_S = df_train[S_cols]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:23.757498Z","iopub.execute_input":"2023-02-05T11:27:23.757919Z","iopub.status.idle":"2023-02-05T11:27:23.766451Z","shell.execute_reply.started":"2023-02-05T11:27:23.757885Z","shell.execute_reply":"2023-02-05T11:27:23.765107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (11,11))\ncorr = df_S.corr()\nmask = np.triu(np.ones_like(corr, dtype=bool))\nsns.heatmap(corr, mask = mask, robust = True, center = 0,square = True, linewidths = .6, cmap = custom_colors)\nplt.title('Correlation of Spend Variables')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:27.390254Z","iopub.execute_input":"2023-02-05T11:27:27.390713Z","iopub.status.idle":"2023-02-05T11:27:27.914720Z","shell.execute_reply.started":"2023-02-05T11:27:27.390677Z","shell.execute_reply":"2023-02-05T11:27:27.913384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 3, figsize = (12,4))\nfig.suptitle('Distribution of Payment Variables',fontsize = 15)\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i < len(pay_cols) - 1:\n        sns.kdeplot(x = pay_cols[i], hue ='target', data = df_pay, fill = True, ax = ax, palette =[\"#e63946\",\"#8338ec\"])\n        ax.tick_params()\n        ax.xaxis.get_label()\n        ax.set_ylabel('')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:31.096623Z","iopub.execute_input":"2023-02-05T11:27:31.097253Z","iopub.status.idle":"2023-02-05T11:27:32.057933Z","shell.execute_reply.started":"2023-02-05T11:27:31.097208Z","shell.execute_reply":"2023-02-05T11:27:32.056644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"P_cols = [c for c in df_train.columns if (c.startswith(('P')))]\ndf_P = df_train[P_cols]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:35.788068Z","iopub.execute_input":"2023-02-05T11:27:35.789255Z","iopub.status.idle":"2023-02-05T11:27:35.796329Z","shell.execute_reply.started":"2023-02-05T11:27:35.789202Z","shell.execute_reply":"2023-02-05T11:27:35.795184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (6,6))\ncorr = df_P.corr()\nmask = np.triu(np.ones_like(corr, dtype = bool))\nsns.heatmap(corr, mask = mask, robust = True, center = 0,square = True, linewidths = .6, cmap = custom_colors)\nplt.title('Correlation of Payment Variables')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:37.560973Z","iopub.execute_input":"2023-02-05T11:27:37.561803Z","iopub.status.idle":"2023-02-05T11:27:37.754497Z","shell.execute_reply.started":"2023-02-05T11:27:37.561757Z","shell.execute_reply":"2023-02-05T11:27:37.753049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(10, 4, figsize = (15,24))\nfig.suptitle('Distribution of Balance Variables',fontsize = 15, x = 0.5, y = 1)\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i < len(bal_cols) - 1:\n        sns.kdeplot(x = bal_cols[i], hue ='target', data = df_bal, fill = True, ax = ax, palette =[\"#e63946\",\"#8338ec\"])\n        ax.tick_params()\n        ax.xaxis.get_label()\n        ax.set_ylabel('')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:40.532222Z","iopub.execute_input":"2023-02-05T11:27:40.532616Z","iopub.status.idle":"2023-02-05T11:27:52.279302Z","shell.execute_reply.started":"2023-02-05T11:27:40.532585Z","shell.execute_reply":"2023-02-05T11:27:52.278117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"B_cols = [c for c in df_train.columns if (c.startswith(('B')))]\ndf_B = df_train[B_cols]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:27:57.963021Z","iopub.execute_input":"2023-02-05T11:27:57.963450Z","iopub.status.idle":"2023-02-05T11:27:57.972223Z","shell.execute_reply.started":"2023-02-05T11:27:57.963407Z","shell.execute_reply":"2023-02-05T11:27:57.970779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (11,11))\ncorr = df_B.corr()\nmask = np.triu(np.ones_like(corr, dtype = bool))\nsns.heatmap(corr, mask = mask, robust=True, center = 0,square = True, linewidths =.6, cmap = custom_colors)\nplt.title('Correlation of Balance Variables')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:01.775724Z","iopub.execute_input":"2023-02-05T11:28:01.776177Z","iopub.status.idle":"2023-02-05T11:28:02.630192Z","shell.execute_reply.started":"2023-02-05T11:28:01.776143Z","shell.execute_reply":"2023-02-05T11:28:02.628907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(10, 3, figsize = (18,23))\nfig.suptitle('Distribution of Risk Variables',fontsize=15, x = 0.5, y = 1)\nfor i, ax in enumerate(axes.reshape(-1)):\n    if i < len(ris_cols) - 1:\n        sns.kdeplot(x = ris_cols[i], hue ='target', data = df_ris, fill = True, ax = ax, palette =[\"#e63946\",\"#8338ec\"])\n        ax.tick_params()\n        ax.xaxis.get_label()\n        ax.set_ylabel('')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:06.111744Z","iopub.execute_input":"2023-02-05T11:28:06.112189Z","iopub.status.idle":"2023-02-05T11:28:15.106037Z","shell.execute_reply.started":"2023-02-05T11:28:06.112156Z","shell.execute_reply":"2023-02-05T11:28:15.104758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"R_cols = [c for c in df_train.columns if (c.startswith(('R')))]\ndf_R = df_train[R_cols]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:19.243419Z","iopub.execute_input":"2023-02-05T11:28:19.243839Z","iopub.status.idle":"2023-02-05T11:28:19.250748Z","shell.execute_reply.started":"2023-02-05T11:28:19.243790Z","shell.execute_reply":"2023-02-05T11:28:19.249778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(11,11))\ncorr = df_R.corr()\nmask = np.triu(np.ones_like(corr, dtype=bool))\nsns.heatmap(corr, mask = mask, robust = True, center = 0, square = True, linewidths =.6, cmap = custom_colors)\nplt.title('Correlation of Risk Variables')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:23.102425Z","iopub.execute_input":"2023-02-05T11:28:23.103067Z","iopub.status.idle":"2023-02-05T11:28:23.798448Z","shell.execute_reply.started":"2023-02-05T11:28:23.103032Z","shell.execute_reply":"2023-02-05T11:28:23.797239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"palette = cycle([\"#ffd670\",\"#70d6ff\",\"#ff4d6d\",\"#8338ec\",\"#90cf8e\"])\ntarg = df_train.corrwith(df_train['target'], axis=0)\nval = [str(round(v ,1) *100) + '%' for v in targ.values]\nfig = go.Figure()\nfig.add_trace(go.Bar(y=targ.index, x= targ.values, orientation='h',text = val, marker_color = next(palette)))\nfig.update_layout(title = \"Correlation of variables with Target\",width = 750, height = 3500,\n                  paper_bgcolor='rgb(0,0,0,0)',plot_bgcolor='rgb(0,0,0,0)')","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:29.402148Z","iopub.execute_input":"2023-02-05T11:28:29.402805Z","iopub.status.idle":"2023-02-05T11:28:29.548733Z","shell.execute_reply.started":"2023-02-05T11:28:29.402768Z","shell.execute_reply":"2023-02-05T11:28:29.547374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:35.543791Z","iopub.execute_input":"2023-02-05T11:28:35.544201Z","iopub.status.idle":"2023-02-05T11:28:35.838448Z","shell.execute_reply.started":"2023-02-05T11:28:35.544169Z","shell.execute_reply":"2023-02-05T11:28:35.837627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nlab_enc = LabelEncoder()\nfor cat_feat in cat_cols:\n    df_train[cat_feat] = lab_enc.fit_transform(df_train[cat_feat])","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:28:39.178545Z","iopub.execute_input":"2023-02-05T11:28:39.179189Z","iopub.status.idle":"2023-02-05T11:28:39.408470Z","shell.execute_reply.started":"2023-02-05T11:28:39.179157Z","shell.execute_reply":"2023-02-05T11:28:39.407238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop('target', axis=1)\ny = df_train['target']","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:01.777094Z","iopub.execute_input":"2023-02-05T11:29:01.777637Z","iopub.status.idle":"2023-02-05T11:29:01.791434Z","shell.execute_reply.started":"2023-02-05T11:29:01.777602Z","shell.execute_reply":"2023-02-05T11:29:01.790097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:05.047377Z","iopub.execute_input":"2023-02-05T11:29:05.048225Z","iopub.status.idle":"2023-02-05T11:29:05.067792Z","shell.execute_reply.started":"2023-02-05T11:29:05.048178Z","shell.execute_reply":"2023-02-05T11:29:05.066530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:08.037029Z","iopub.execute_input":"2023-02-05T11:29:08.038445Z","iopub.status.idle":"2023-02-05T11:29:08.045423Z","shell.execute_reply.started":"2023-02-05T11:29:08.038392Z","shell.execute_reply":"2023-02-05T11:29:08.044567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:10.201101Z","iopub.execute_input":"2023-02-05T11:29:10.201510Z","iopub.status.idle":"2023-02-05T11:29:10.208209Z","shell.execute_reply.started":"2023-02-05T11:29:10.201479Z","shell.execute_reply":"2023-02-05T11:29:10.206910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# creating dataset split for prediction\nX_train, X_test , y_train , y_test = train_test_split(X,y,test_size=0.2,random_state=42) # 80-20 split\n\n# Checking split \nprint('X_train:', X_train.shape)\nprint('y_train:', y_train.shape)\nprint('X_test:', X_test.shape)\nprint('y_test:', y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:14.244157Z","iopub.execute_input":"2023-02-05T11:29:14.245187Z","iopub.status.idle":"2023-02-05T11:29:14.338233Z","shell.execute_reply.started":"2023-02-05T11:29:14.245133Z","shell.execute_reply":"2023-02-05T11:29:14.336692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from catboost import CatBoostClassifier\nclf = CatBoostClassifier(iterations = 3000, random_state = 42)\nclf.fit(X_train, y_train, eval_set = [(X_test, y_test)], cat_features=cat_cols,  verbose = 100)\npreds = clf.predict_proba(X_test)[:, 1]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:29:16.964424Z","iopub.execute_input":"2023-02-05T11:29:16.964909Z","iopub.status.idle":"2023-02-05T11:32:21.844560Z","shell.execute_reply.started":"2023-02-05T11:29:16.964871Z","shell.execute_reply":"2023-02-05T11:32:21.843241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = clf.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:09.518616Z","iopub.execute_input":"2023-02-05T11:51:09.519092Z","iopub.status.idle":"2023-02-05T11:51:09.550910Z","shell.execute_reply.started":"2023-02-05T11:51:09.519053Z","shell.execute_reply":"2023-02-05T11:51:09.549680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:16.549802Z","iopub.execute_input":"2023-02-05T11:51:16.550237Z","iopub.status.idle":"2023-02-05T11:51:16.555598Z","shell.execute_reply.started":"2023-02-05T11:51:16.550204Z","shell.execute_reply":"2023-02-05T11:51:16.554270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:17.934118Z","iopub.execute_input":"2023-02-05T11:51:17.934553Z","iopub.status.idle":"2023-02-05T11:51:17.944044Z","shell.execute_reply.started":"2023-02-05T11:51:17.934519Z","shell.execute_reply":"2023-02-05T11:51:17.942608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix  \ncm = confusion_matrix(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:20.469307Z","iopub.execute_input":"2023-02-05T11:51:20.469737Z","iopub.status.idle":"2023-02-05T11:51:20.478287Z","shell.execute_reply.started":"2023-02-05T11:51:20.469705Z","shell.execute_reply":"2023-02-05T11:51:20.477197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm","metadata":{"execution":{"iopub.status.busy":"2023-02-05T09:18:54.347391Z","iopub.execute_input":"2023-02-05T09:18:54.348276Z","iopub.status.idle":"2023-02-05T09:18:54.356273Z","shell.execute_reply.started":"2023-02-05T09:18:54.348225Z","shell.execute_reply":"2023-02-05T09:18:54.355051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[10,7],)\nsns.heatmap(cm, annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:25.432307Z","iopub.execute_input":"2023-02-05T11:51:25.433086Z","iopub.status.idle":"2023-02-05T11:51:25.698309Z","shell.execute_reply.started":"2023-02-05T11:51:25.433046Z","shell.execute_reply":"2023-02-05T11:51:25.697018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\nprint(classification_report(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:29.727535Z","iopub.execute_input":"2023-02-05T11:51:29.727969Z","iopub.status.idle":"2023-02-05T11:51:29.745846Z","shell.execute_reply.started":"2023-02-05T11:51:29.727936Z","shell.execute_reply":"2023-02-05T11:51:29.744598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0.8\ndf_train = df_train.drop(df_train.columns[df_train.isnull().mean() >= threshold], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:33.411897Z","iopub.execute_input":"2023-02-05T11:51:33.413119Z","iopub.status.idle":"2023-02-05T11:51:33.433842Z","shell.execute_reply.started":"2023-02-05T11:51:33.413067Z","shell.execute_reply":"2023-02-05T11:51:33.432564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:37.269034Z","iopub.execute_input":"2023-02-05T11:51:37.269463Z","iopub.status.idle":"2023-02-05T11:51:37.298567Z","shell.execute_reply.started":"2023-02-05T11:51:37.269426Z","shell.execute_reply":"2023-02-05T11:51:37.297185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:39.667344Z","iopub.execute_input":"2023-02-05T11:51:39.668060Z","iopub.status.idle":"2023-02-05T11:51:39.674326Z","shell.execute_reply.started":"2023-02-05T11:51:39.668012Z","shell.execute_reply":"2023-02-05T11:51:39.673406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:42.030478Z","iopub.execute_input":"2023-02-05T11:51:42.030977Z","iopub.status.idle":"2023-02-05T11:51:42.038798Z","shell.execute_reply.started":"2023-02-05T11:51:42.030936Z","shell.execute_reply":"2023-02-05T11:51:42.037812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.duplicated().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:44.478111Z","iopub.execute_input":"2023-02-05T11:51:44.479283Z","iopub.status.idle":"2023-02-05T11:51:44.717326Z","shell.execute_reply.started":"2023-02-05T11:51:44.479235Z","shell.execute_reply":"2023-02-05T11:51:44.716106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:46.880986Z","iopub.execute_input":"2023-02-05T11:51:46.881434Z","iopub.status.idle":"2023-02-05T11:51:46.898028Z","shell.execute_reply.started":"2023-02-05T11:51:46.881399Z","shell.execute_reply":"2023-02-05T11:51:46.896633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.fillna(0)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:50.174571Z","iopub.execute_input":"2023-02-05T11:51:50.175202Z","iopub.status.idle":"2023-02-05T11:51:50.201816Z","shell.execute_reply.started":"2023-02-05T11:51:50.175143Z","shell.execute_reply":"2023-02-05T11:51:50.200283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop('target', axis=1)\ny = df_train['target']","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:53.348874Z","iopub.execute_input":"2023-02-05T11:51:53.349281Z","iopub.status.idle":"2023-02-05T11:51:53.361587Z","shell.execute_reply.started":"2023-02-05T11:51:53.349247Z","shell.execute_reply":"2023-02-05T11:51:53.360185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:55.920358Z","iopub.execute_input":"2023-02-05T11:51:55.920741Z","iopub.status.idle":"2023-02-05T11:51:55.927479Z","shell.execute_reply.started":"2023-02-05T11:51:55.920710Z","shell.execute_reply":"2023-02-05T11:51:55.926550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import ExtraTreesClassifier\nimport matplotlib.pyplot as plt\nmodel = ExtraTreesClassifier()\nmodel.fit(X,y)\nprint(model.feature_importances_)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:51:58.131533Z","iopub.execute_input":"2023-02-05T11:51:58.132012Z","iopub.status.idle":"2023-02-05T11:52:03.027652Z","shell.execute_reply.started":"2023-02-05T11:51:58.131971Z","shell.execute_reply":"2023-02-05T11:52:03.026334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.iloc[:,:-1]","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:23.787905Z","iopub.execute_input":"2023-02-05T11:53:23.788326Z","iopub.status.idle":"2023-02-05T11:53:23.804258Z","shell.execute_reply.started":"2023-02-05T11:53:23.788292Z","shell.execute_reply":"2023-02-05T11:53:23.802746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feat_importances = pd.Series(model.feature_importances_, index=X.columns)\nfeat_importances.nlargest(10).plot(kind='barh')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:26.167556Z","iopub.execute_input":"2023-02-05T11:53:26.168021Z","iopub.status.idle":"2023-02-05T11:53:26.413441Z","shell.execute_reply.started":"2023-02-05T11:53:26.167981Z","shell.execute_reply":"2023-02-05T11:53:26.412258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test , y_train , y_test = train_test_split(X,y,test_size=0.2,random_state=42) # 80-20 split\nprint('X_train:', X_train.shape)\nprint('y_train:', y_train.shape)\nprint('X_test:', X_test.shape)\nprint('y_test:', y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:28.780636Z","iopub.execute_input":"2023-02-05T11:53:28.781052Z","iopub.status.idle":"2023-02-05T11:53:28.813189Z","shell.execute_reply.started":"2023-02-05T11:53:28.781021Z","shell.execute_reply":"2023-02-05T11:53:28.811771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression  \nlog_r= LogisticRegression(random_state=0)  \nlog_r.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:31.047349Z","iopub.execute_input":"2023-02-05T11:53:31.048074Z","iopub.status.idle":"2023-02-05T11:53:31.496346Z","shell.execute_reply.started":"2023-02-05T11:53:31.048038Z","shell.execute_reply":"2023-02-05T11:53:31.494622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_lr= log_r.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:34.106925Z","iopub.execute_input":"2023-02-05T11:53:34.107366Z","iopub.status.idle":"2023-02-05T11:53:34.119353Z","shell.execute_reply.started":"2023-02-05T11:53:34.107332Z","shell.execute_reply":"2023-02-05T11:53:34.117562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred_lr)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:36.766747Z","iopub.execute_input":"2023-02-05T11:53:36.767228Z","iopub.status.idle":"2023-02-05T11:53:36.777652Z","shell.execute_reply.started":"2023-02-05T11:53:36.767182Z","shell.execute_reply":"2023-02-05T11:53:36.776560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred_lr)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:38.770265Z","iopub.execute_input":"2023-02-05T11:53:38.770903Z","iopub.status.idle":"2023-02-05T11:53:38.778149Z","shell.execute_reply.started":"2023-02-05T11:53:38.770859Z","shell.execute_reply":"2023-02-05T11:53:38.777068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:40.677583Z","iopub.execute_input":"2023-02-05T11:53:40.678248Z","iopub.status.idle":"2023-02-05T11:53:40.685149Z","shell.execute_reply.started":"2023-02-05T11:53:40.678198Z","shell.execute_reply":"2023-02-05T11:53:40.684032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[10,7],)\nsns.heatmap(cm, annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:42.710913Z","iopub.execute_input":"2023-02-05T11:53:42.711628Z","iopub.status.idle":"2023-02-05T11:53:42.908992Z","shell.execute_reply.started":"2023-02-05T11:53:42.711579Z","shell.execute_reply":"2023-02-05T11:53:42.907515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred_lr))","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:45.663923Z","iopub.execute_input":"2023-02-05T11:53:45.664586Z","iopub.status.idle":"2023-02-05T11:53:45.680423Z","shell.execute_reply.started":"2023-02-05T11:53:45.664550Z","shell.execute_reply":"2023-02-05T11:53:45.679372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\ndt = DecisionTreeClassifier()\ndt.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:48.232192Z","iopub.execute_input":"2023-02-05T11:53:48.232800Z","iopub.status.idle":"2023-02-05T11:53:53.425857Z","shell.execute_reply.started":"2023-02-05T11:53:48.232766Z","shell.execute_reply":"2023-02-05T11:53:53.424619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_dt= dt.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:53.427579Z","iopub.execute_input":"2023-02-05T11:53:53.427959Z","iopub.status.idle":"2023-02-05T11:53:53.438920Z","shell.execute_reply.started":"2023-02-05T11:53:53.427927Z","shell.execute_reply":"2023-02-05T11:53:53.437970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred_dt)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:53.440259Z","iopub.execute_input":"2023-02-05T11:53:53.440585Z","iopub.status.idle":"2023-02-05T11:53:53.460037Z","shell.execute_reply.started":"2023-02-05T11:53:53.440556Z","shell.execute_reply":"2023-02-05T11:53:53.458703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred_dt)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:56.132925Z","iopub.execute_input":"2023-02-05T11:53:56.133330Z","iopub.status.idle":"2023-02-05T11:53:56.140160Z","shell.execute_reply.started":"2023-02-05T11:53:56.133299Z","shell.execute_reply":"2023-02-05T11:53:56.139147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:53:58.021361Z","iopub.execute_input":"2023-02-05T11:53:58.022273Z","iopub.status.idle":"2023-02-05T11:53:58.028250Z","shell.execute_reply.started":"2023-02-05T11:53:58.022236Z","shell.execute_reply":"2023-02-05T11:53:58.027447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[10,7],)\nsns.heatmap(cm, annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T09:47:32.447116Z","iopub.execute_input":"2023-02-05T09:47:32.447636Z","iopub.status.idle":"2023-02-05T09:47:32.703885Z","shell.execute_reply.started":"2023-02-05T09:47:32.447597Z","shell.execute_reply":"2023-02-05T09:47:32.702454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred_dt))","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:03.080346Z","iopub.execute_input":"2023-02-05T11:54:03.081288Z","iopub.status.idle":"2023-02-05T11:54:03.095972Z","shell.execute_reply.started":"2023-02-05T11:54:03.081246Z","shell.execute_reply":"2023-02-05T11:54:03.094772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier  \nrf_c = RandomForestClassifier(n_estimators= 10, criterion=\"entropy\")  \nrf_c.fit(X_train, y_train) ","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:10.260374Z","iopub.execute_input":"2023-02-05T11:54:10.261006Z","iopub.status.idle":"2023-02-05T11:54:11.811964Z","shell.execute_reply.started":"2023-02-05T11:54:10.260972Z","shell.execute_reply":"2023-02-05T11:54:11.811045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"y_pred_rf_c= rf_c.predict(X_test)  ","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:17.770574Z","iopub.execute_input":"2023-02-05T11:54:17.771247Z","iopub.status.idle":"2023-02-05T11:54:17.792307Z","shell.execute_reply.started":"2023-02-05T11:54:17.771202Z","shell.execute_reply":"2023-02-05T11:54:17.790924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(y_test, y_pred_rf_c)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:20.411111Z","iopub.execute_input":"2023-02-05T11:54:20.411509Z","iopub.status.idle":"2023-02-05T11:54:20.421119Z","shell.execute_reply.started":"2023-02-05T11:54:20.411479Z","shell.execute_reply":"2023-02-05T11:54:20.419912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm= confusion_matrix(y_test, y_pred_rf_c) ","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:22.976677Z","iopub.execute_input":"2023-02-05T11:54:22.977110Z","iopub.status.idle":"2023-02-05T11:54:22.984222Z","shell.execute_reply.started":"2023-02-05T11:54:22.977076Z","shell.execute_reply":"2023-02-05T11:54:22.982907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:25.078018Z","iopub.execute_input":"2023-02-05T11:54:25.078424Z","iopub.status.idle":"2023-02-05T11:54:25.086968Z","shell.execute_reply.started":"2023-02-05T11:54:25.078386Z","shell.execute_reply":"2023-02-05T11:54:25.085681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=[10,7],)\nsns.heatmap(cm, annot = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:27.349877Z","iopub.execute_input":"2023-02-05T11:54:27.350310Z","iopub.status.idle":"2023-02-05T11:54:27.554105Z","shell.execute_reply.started":"2023-02-05T11:54:27.350273Z","shell.execute_reply":"2023-02-05T11:54:27.552852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_test, y_pred_rf_c))","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:30.527121Z","iopub.execute_input":"2023-02-05T11:54:30.528225Z","iopub.status.idle":"2023-02-05T11:54:30.545060Z","shell.execute_reply.started":"2023-02-05T11:54:30.528173Z","shell.execute_reply":"2023-02-05T11:54:30.543548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.model_selection import GridSearchCV","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:33.385976Z","iopub.execute_input":"2023-02-05T11:54:33.388214Z","iopub.status.idle":"2023-02-05T11:54:33.394068Z","shell.execute_reply.started":"2023-02-05T11:54:33.388153Z","shell.execute_reply":"2023-02-05T11:54:33.393178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folds = StratifiedKFold(n_splits = 5, shuffle = True, random_state = 40)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:36.531152Z","iopub.execute_input":"2023-02-05T11:54:36.532022Z","iopub.status.idle":"2023-02-05T11:54:36.536936Z","shell.execute_reply.started":"2023-02-05T11:54:36.531983Z","shell.execute_reply":"2023-02-05T11:54:36.535731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def grid_search(model,folds,params,scoring):\n    grid_search = GridSearchCV(model,\n                                cv=folds, \n                                param_grid=params, \n                                scoring=scoring, \n                                n_jobs=-1, verbose=1)\n    return grid_search","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:59.154594Z","iopub.execute_input":"2023-02-05T11:54:59.155088Z","iopub.status.idle":"2023-02-05T11:54:59.161019Z","shell.execute_reply.started":"2023-02-05T11:54:59.155048Z","shell.execute_reply":"2023-02-05T11:54:59.159778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def print_best_score_params(model):\n    print(\"Best Score: \", model.best_score_)\n    print(\"Best Hyperparameters: \", model.best_params_)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:54:51.660210Z","iopub.execute_input":"2023-02-05T11:54:51.660996Z","iopub.status.idle":"2023-02-05T11:54:51.666556Z","shell.execute_reply.started":"2023-02-05T11:54:51.660957Z","shell.execute_reply":"2023-02-05T11:54:51.665368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_reg = LogisticRegression()\nlog_params = {'C': [0.01, 1, 10], \n          'penalty': ['l1', 'l2'],\n          'solver': ['liblinear','newton-cg','saga']\n         }\ngrid_search_log = grid_search(log_reg, folds, log_params, scoring=None)\ngrid_search_log.fit(X_train, y_train)\nprint_best_score_params(grid_search_log)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T11:55:02.759010Z","iopub.execute_input":"2023-02-05T11:55:02.759926Z","iopub.status.idle":"2023-02-05T11:56:56.290305Z","shell.execute_reply.started":"2023-02-05T11:55:02.759880Z","shell.execute_reply":"2023-02-05T11:56:56.288580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtc = DecisionTreeClassifier(random_state=40)\ndtc_params = {\n    'max_depth': [5,10,20,30],\n    'min_samples_leaf': [5,10,20,30]\n}\ngrid_search_dtc = grid_search(dtc, folds, dtc_params, scoring='roc_auc_ovr')\ngrid_search_dtc.fit(X_train, y_train)\nprint_best_score_params(grid_search_dtc)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T12:04:12.471315Z","iopub.execute_input":"2023-02-05T12:04:12.471805Z","iopub.status.idle":"2023-02-05T12:05:10.051489Z","shell.execute_reply.started":"2023-02-05T12:04:12.471754Z","shell.execute_reply":"2023-02-05T12:05:10.050392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\n\nmodel = Sequential()\nmodel.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-05T09:57:06.140277Z","iopub.execute_input":"2023-02-05T09:57:06.141311Z","iopub.status.idle":"2023-02-05T09:57:12.019695Z","shell.execute_reply.started":"2023-02-05T09:57:06.141257Z","shell.execute_reply":"2023-02-05T09:57:12.018165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs=20, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T09:57:56.368658Z","iopub.execute_input":"2023-02-05T09:57:56.369758Z","iopub.status.idle":"2023-02-05T09:58:16.948576Z","shell.execute_reply.started":"2023-02-05T09:57:56.369715Z","shell.execute_reply":"2023-02-05T09:58:16.947157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_acc = model.evaluate(X_test, y_test)\nprint('Test accuracy:', test_acc)","metadata":{"execution":{"iopub.status.busy":"2023-02-05T09:58:33.593669Z","iopub.execute_input":"2023-02-05T09:58:33.596445Z","iopub.status.idle":"2023-02-05T09:58:34.029642Z","shell.execute_reply.started":"2023-02-05T09:58:33.596395Z","shell.execute_reply":"2023-02-05T09:58:34.028269Z"},"trusted":true},"execution_count":null,"outputs":[]}]}