{"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 numpy as np \nimport pandas as pd \nimport seaborn as sb\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nplt.style.use('seaborn-darkgrid')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-12-10T04:21:14.421723Z","iopub.execute_input":"2022-12-10T04:21:14.422087Z","iopub.status.idle":"2022-12-10T04:21:14.429365Z","shell.execute_reply.started":"2022-12-10T04:21:14.422059Z","shell.execute_reply":"2022-12-10T04:21:14.427790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/bank-customer-churn-dataset/Bank Customer Churn Prediction.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.440934Z","iopub.execute_input":"2022-12-10T04:21:14.441419Z","iopub.status.idle":"2022-12-10T04:21:14.465438Z","shell.execute_reply.started":"2022-12-10T04:21:14.441339Z","shell.execute_reply":"2022-12-10T04:21:14.464493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.469394Z","iopub.execute_input":"2022-12-10T04:21:14.470078Z","iopub.status.idle":"2022-12-10T04:21:14.487735Z","shell.execute_reply.started":"2022-12-10T04:21:14.470038Z","shell.execute_reply":"2022-12-10T04:21:14.486148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.489745Z","iopub.execute_input":"2022-12-10T04:21:14.490283Z","iopub.status.idle":"2022-12-10T04:21:14.508544Z","shell.execute_reply.started":"2022-12-10T04:21:14.490248Z","shell.execute_reply":"2022-12-10T04:21:14.507019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(columns = ['customer_id'])","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.511875Z","iopub.execute_input":"2022-12-10T04:21:14.512379Z","iopub.status.idle":"2022-12-10T04:21:14.519815Z","shell.execute_reply.started":"2022-12-10T04:21:14.512272Z","shell.execute_reply":"2022-12-10T04:21:14.518277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (25,8))\n# px.imshow(df.corr() , text_auto = True , aspect=\"auto\")\nsb.heatmap(df[[col for col in df.columns if col not in ['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'country' , 'gender' ]]].corr() , annot = True)\nplt.xlabel('Fig:1 Correlation matrix for dataset', weight = 'bold' , fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.521884Z","iopub.execute_input":"2022-12-10T04:21:14.522274Z","iopub.status.idle":"2022-12-10T04:21:14.812335Z","shell.execute_reply.started":"2022-12-10T04:21:14.522242Z","shell.execute_reply":"2022-12-10T04:21:14.811394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[[col for col in df.columns if col not in ['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'churn' , 'country' , 'gender' ]]].corr()\nprint('Fig:2 ')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.813547Z","iopub.execute_input":"2022-12-10T04:21:14.814857Z","iopub.status.idle":"2022-12-10T04:21:14.823372Z","shell.execute_reply.started":"2022-12-10T04:21:14.814821Z","shell.execute_reply":"2022-12-10T04:21:14.821842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_cols  = df.select_dtypes(['int' , 'float'])\nnum_cols.describe()","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.825068Z","iopub.execute_input":"2022-12-10T04:21:14.825528Z","iopub.status.idle":"2022-12-10T04:21:14.871949Z","shell.execute_reply.started":"2022-12-10T04:21:14.825494Z","shell.execute_reply":"2022-12-10T04:21:14.870105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# round(std_num_df[['credit_score'\t,'age',\t'balance'\t,\t'credit_card','estimated_salary']].describe() , 2)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.875171Z","iopub.execute_input":"2022-12-10T04:21:14.875497Z","iopub.status.idle":"2022-12-10T04:21:14.883016Z","shell.execute_reply.started":"2022-12-10T04:21:14.875466Z","shell.execute_reply":"2022-12-10T04:21:14.881016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(3,4,figsize = (25,25))\nax = ax.ravel()\nfor i,col in enumerate(df.select_dtypes(['int' , 'float' , 'bool']).columns.to_list()):\n    sb.kdeplot(x = df[col] , ax=ax[i] , shade ='fill') \n#     plt.title(col , weight = 'bold')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:14.884674Z","iopub.execute_input":"2022-12-10T04:21:14.885065Z","iopub.status.idle":"2022-12-10T04:21:16.753122Z","shell.execute_reply.started":"2022-12-10T04:21:14.885028Z","shell.execute_reply":"2022-12-10T04:21:16.751705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import MinMaxScaler\ns_scaler = StandardScaler()\nmm_scaler = MinMaxScaler()\nstd_num_df = pd.DataFrame(s_scaler.fit_transform(num_cols) , columns = num_cols.columns)\nmm_num_df = pd.DataFrame(mm_scaler.fit_transform(num_cols) , columns = num_cols.columns)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:16.754825Z","iopub.execute_input":"2022-12-10T04:21:16.755401Z","iopub.status.idle":"2022-12-10T04:21:16.773993Z","shell.execute_reply.started":"2022-12-10T04:21:16.755361Z","shell.execute_reply":"2022-12-10T04:21:16.771753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(3,4,figsize = (25,25))\nax = ax.ravel()\nfor i,col in enumerate(mm_num_df.columns.to_list()):\n    sb.kdeplot(x = mm_num_df[col] , ax=ax[i] , shade ='fill') ","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:16.775815Z","iopub.execute_input":"2022-12-10T04:21:16.776991Z","iopub.status.idle":"2022-12-10T04:21:18.849921Z","shell.execute_reply.started":"2022-12-10T04:21:16.776931Z","shell.execute_reply":"2022-12-10T04:21:18.848559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,ax = plt.subplots(3,4,figsize = (25,25))\nax = ax.ravel()\nfor i,col in enumerate(std_num_df.columns.to_list()):\n    sb.kdeplot(x = std_num_df[col] , ax=ax[i] , shade ='fill') ","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:18.851382Z","iopub.execute_input":"2022-12-10T04:21:18.851735Z","iopub.status.idle":"2022-12-10T04:21:20.678052Z","shell.execute_reply.started":"2022-12-10T04:21:18.851704Z","shell.execute_reply":"2022-12-10T04:21:20.677399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df[['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'churn' , 'country' , 'gender' ]]\n[col for col in df.columns if col not in ['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'churn' , 'country' , 'gender' ]]","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:20.679155Z","iopub.execute_input":"2022-12-10T04:21:20.680682Z","iopub.status.idle":"2022-12-10T04:21:20.690540Z","shell.execute_reply.started":"2022-12-10T04:21:20.680625Z","shell.execute_reply":"2022-12-10T04:21:20.688933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,3,figsize = (20,8))\nax = ax.ravel()\nfor i,col in enumerate(['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'churn' ]):\n    sb.countplot(x = col , data = df , ax = ax[i]) \n    plt.title(col , weight = 'bold')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:20.692418Z","iopub.execute_input":"2022-12-10T04:21:20.693419Z","iopub.status.idle":"2022-12-10T04:21:21.308663Z","shell.execute_reply.started":"2022-12-10T04:21:20.693375Z","shell.execute_reply":"2022-12-10T04:21:21.307365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,7))\nfor i,col in enumerate(df.select_dtypes(['object']).columns.to_list()):\n    plt.subplot(1,2,i+1)\n    df[col].value_counts().plot(kind = 'bar' , title = col)\n    plt.title(col , weight = 'bold' , fontsize = 20)\n    plt.ylabel('count')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:21.309949Z","iopub.execute_input":"2022-12-10T04:21:21.310333Z","iopub.status.idle":"2022-12-10T04:21:21.542952Z","shell.execute_reply.started":"2022-12-10T04:21:21.310298Z","shell.execute_reply":"2022-12-10T04:21:21.542299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" df.groupby(['active_member' , 'country' , 'gender'])['credit_card'].count().unstack().astype('int') \\\n.style.background_gradient(cmap = 'YlGnBu')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:21.546490Z","iopub.execute_input":"2022-12-10T04:21:21.546899Z","iopub.status.idle":"2022-12-10T04:21:21.565864Z","shell.execute_reply.started":"2022-12-10T04:21:21.546874Z","shell.execute_reply":"2022-12-10T04:21:21.564296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (25,10))\nsb.boxplot(y='credit_score'  , x = 'tenure' , hue = 'gender', data = df)\nplt.title('Credit score for genders with different tenures' , weight = 'bold' , fontsize = 20)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:21.567304Z","iopub.execute_input":"2022-12-10T04:21:21.567593Z","iopub.status.idle":"2022-12-10T04:21:22.343648Z","shell.execute_reply.started":"2022-12-10T04:21:21.567569Z","shell.execute_reply":"2022-12-10T04:21:22.342489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,7))\nplt.suptitle('\\n With no balance \\n' , fontsize = 20 , weight = 'bold')\nplt.subplot(1,3,1)\nsb.kdeplot('balance' , hue = 'country', data = df)\nplt.subplot(1,3,2)\nsb.countplot(x ='active_member' , hue = 'country' , data = df.query('balance == 0'));\nplt.subplot(1,3,3)\nsb.countplot( x = 'country' , hue = 'gender' , data = df.query('balance == 0'));","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:22.345173Z","iopub.execute_input":"2022-12-10T04:21:22.346219Z","iopub.status.idle":"2022-12-10T04:21:22.863769Z","shell.execute_reply.started":"2022-12-10T04:21:22.346179Z","shell.execute_reply":"2022-12-10T04:21:22.862498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights :**\n* Germany has no customers with 0 balance \n* France has maximum customers with 0 balance","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20,7))\nplt.suptitle('\\n With balance \\n' , fontsize = 20 , weight = 'bold')\nplt.subplot(1,3,1)\nsb.countplot(x ='active_member' , hue = 'country' , data = df.query('balance > 0'));\nplt.subplot(1,3,2)\nsb.countplot( x = 'country' , hue = 'gender' , data = df.query('balance > 0'));\nplt.subplot(1,3,3)\nsb.kdeplot( hue = 'products_number' ,  x = 'balance' , shade = 'fill' , data = df.query('balance > 0'));","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:22.865356Z","iopub.execute_input":"2022-12-10T04:21:22.865669Z","iopub.status.idle":"2022-12-10T04:21:23.400130Z","shell.execute_reply.started":"2022-12-10T04:21:22.865640Z","shell.execute_reply":"2022-12-10T04:21:23.399282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,7))\nplt.suptitle('\\n Products used \\n' , fontsize = 20 , weight = 'bold')\nplt.subplot(1,3,1)\nsb.boxplot(x = 'products_number' , y = 'age' , hue = 'gender', data = df)\nplt.subplot(1,3,2)\nsb.countplot(x ='products_number' , hue = 'country' , data = df);\nplt.subplot(1,3,3)\nsb.countplot( x = 'products_number' , hue = 'gender' , data = df);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:23.401430Z","iopub.execute_input":"2022-12-10T04:21:23.401787Z","iopub.status.idle":"2022-12-10T04:21:24.289602Z","shell.execute_reply.started":"2022-12-10T04:21:23.401752Z","shell.execute_reply":"2022-12-10T04:21:24.288133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights :**\n* Product number 1 has maximum users \n* Product number 3 & 4 has majority users with median age more than 40 yrs while Product number 1 and 2 has majority users with median age less than 40 yrs which explains the less usage of product number 3 & 4.\n* Females are leading users for Product number 3 & 4.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20,7))\nplt.suptitle('Products used by customers with and without balance' , weight = 'bold' , fontsize = 20)\nplt.subplot(121)\nsb.countplot(x = 'products_number' , hue = 'country' , data = df.query('balance == 0'))\nplt.title('Without balance' , weight = 'bold')\n\nplt.subplot(122)\nsb.countplot(x = 'products_number' , hue = 'country', data = df.query('balance > 0'))\nplt.title('With balance' , weight = 'bold')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:24.291003Z","iopub.execute_input":"2022-12-10T04:21:24.291327Z","iopub.status.idle":"2022-12-10T04:21:24.683850Z","shell.execute_reply.started":"2022-12-10T04:21:24.291300Z","shell.execute_reply":"2022-12-10T04:21:24.682970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insight :**  \n* Customers in  France & Spain with no balance extensively use product number 2\n* Majority customers with balance use product number 1 compared to other products.\n* While France has the maximum users in total for both the categories but Germany leads in the useage of the products other than product number 1 among the customers that have balance.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20,6))\nsb.boxplot(x = 'churn' , y = 'age' , hue = 'gender' , data = df)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:24.685425Z","iopub.execute_input":"2022-12-10T04:21:24.685727Z","iopub.status.idle":"2022-12-10T04:21:24.938848Z","shell.execute_reply.started":"2022-12-10T04:21:24.685700Z","shell.execute_reply":"2022-12-10T04:21:24.937492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Insights :**\n* From the correlation table we can see that age & churn has a correlation. Here we can see that customers with age greater that or equal to 45   forms the majority of those leaving the bank.","metadata":{}},{"cell_type":"markdown","source":"## Data Preprocessing","metadata":{}},{"cell_type":"code","source":"# converting the categorical columns\ndf = pd.get_dummies(data = df , columns = ['country' , 'gender'])\ndf","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:24.940748Z","iopub.execute_input":"2022-12-10T04:21:24.941577Z","iopub.status.idle":"2022-12-10T04:21:24.970010Z","shell.execute_reply.started":"2022-12-10T04:21:24.941521Z","shell.execute_reply":"2022-12-10T04:21:24.968693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = df[['credit_score', 'age', 'tenure', 'balance', 'products_number','credit_card', 'active_member', 'estimated_salary', 'country_France', 'country_Germany', 'country_Spain', 'gender_Female','gender_Male']]\ny = df[['churn']]","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:24.971608Z","iopub.execute_input":"2022-12-10T04:21:24.971911Z","iopub.status.idle":"2022-12-10T04:21:24.984050Z","shell.execute_reply.started":"2022-12-10T04:21:24.971883Z","shell.execute_reply":"2022-12-10T04:21:24.982835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As there is imbalance in the classes so to overcome this issue using oversampling through SMOTE to generate synthetic samples\nfrom imblearn.over_sampling import SMOTE\nsmote = SMOTE(sampling_strategy = 'minority')\nx_new , y_new = smote.fit_resample(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:24.985670Z","iopub.execute_input":"2022-12-10T04:21:24.986003Z","iopub.status.idle":"2022-12-10T04:21:25.017679Z","shell.execute_reply.started":"2022-12-10T04:21:24.985975Z","shell.execute_reply":"2022-12-10T04:21:25.016673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Building","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\ns_scaler =  StandardScaler()\nx_std_new = s_scaler.fit_transform(x_new)\nx_new = pd.DataFrame(x_std_new , columns = x_new.columns , index = x_new.index.to_list())","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.028265Z","iopub.execute_input":"2022-12-10T04:21:25.028595Z","iopub.status.idle":"2022-12-10T04:21:25.049536Z","shell.execute_reply.started":"2022-12-10T04:21:25.028561Z","shell.execute_reply":"2022-12-10T04:21:25.047851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_new","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.050906Z","iopub.execute_input":"2022-12-10T04:21:25.052136Z","iopub.status.idle":"2022-12-10T04:21:25.073906Z","shell.execute_reply.started":"2022-12-10T04:21:25.052067Z","shell.execute_reply":"2022-12-10T04:21:25.072601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_new[[col for col in df.columns if col not in ['tenure' , 'credit_card' , 'active_member' , 'products_number' , 'churn' , 'gender' , \\\n                                             'country_France',\t'country_Germany'\t, 'country_Spain',\t'gender_Female',\t'gender_Male' ]]]","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.075144Z","iopub.execute_input":"2022-12-10T04:21:25.075485Z","iopub.status.idle":"2022-12-10T04:21:25.093574Z","shell.execute_reply.started":"2022-12-10T04:21:25.075458Z","shell.execute_reply":"2022-12-10T04:21:25.092381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_new.sample(5)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.095426Z","iopub.execute_input":"2022-12-10T04:21:25.095952Z","iopub.status.idle":"2022-12-10T04:21:25.117180Z","shell.execute_reply.started":"2022-12-10T04:21:25.095912Z","shell.execute_reply":"2022-12-10T04:21:25.115723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nxtrain,xtest,ytrain,ytest = train_test_split(x_new,y_new,test_size = 0.2 , stratify = y_new)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.119826Z","iopub.execute_input":"2022-12-10T04:21:25.120607Z","iopub.status.idle":"2022-12-10T04:21:25.188175Z","shell.execute_reply.started":"2022-12-10T04:21:25.120563Z","shell.execute_reply":"2022-12-10T04:21:25.186825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,6))\nplt.suptitle('Fig:2 Proportion of classes in training and test set is same' , weight = 'bold')\nplt.subplot(121)\nytrain.value_counts().plot(kind = 'bar')\nplt.title('Training set' , weight = 'bold')\n\n\nplt.subplot(122)\nytest.value_counts().plot(kind = 'bar')\nplt.title('Testing set' , weight = 'bold')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.189620Z","iopub.execute_input":"2022-12-10T04:21:25.189957Z","iopub.status.idle":"2022-12-10T04:21:25.473248Z","shell.execute_reply.started":"2022-12-10T04:21:25.189926Z","shell.execute_reply":"2022-12-10T04:21:25.472155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nmodel1 = LogisticRegression(solver = 'liblinear' , max_iter = 500)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.474855Z","iopub.execute_input":"2022-12-10T04:21:25.475324Z","iopub.status.idle":"2022-12-10T04:21:25.481366Z","shell.execute_reply.started":"2022-12-10T04:21:25.475285Z","shell.execute_reply":"2022-12-10T04:21:25.480254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report , confusion_matrix\nmodel1.fit(xtrain , ytrain.values.ravel())\nprint(classification_report(ytest , model1.predict(xtest)))\nprint('    Fig:3 Classification report for Logistic Regression model')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.482996Z","iopub.execute_input":"2022-12-10T04:21:25.483585Z","iopub.status.idle":"2022-12-10T04:21:25.567742Z","shell.execute_reply.started":"2022-12-10T04:21:25.483556Z","shell.execute_reply":"2022-12-10T04:21:25.566835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(ytest , model1.predict(xtest)))\nplt.figure(figsize = (25,7))\nplt.subplot(121)\nsb.heatmap(confusion_matrix(ytest , model1.predict(xtest) , normalize = 'all') , cmap = 'Greens' , annot = True)\nplt.title('Fig:4 Confusion matrix for Logistic Regression' , weight = 'bold')\nplt.ylabel('Actual condition' , weight = 'bold')\nplt.xlabel('Predicted condition' , weight = 'bold')\n\nplt.subplot(122)\nweights = pd.DataFrame({'columns':xtrain.columns, 'weights': model1.coef_[0]}).sort_values('weights' , ascending = False)\nsb.barplot(y = 'columns' , x = 'weights' , data =  weights)\nplt.title('Fig:5 Important columns for Logistic Regression' , weight = 'bold')\n# plt.xticks(rotation = 90);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:25.569144Z","iopub.execute_input":"2022-12-10T04:21:25.569510Z","iopub.status.idle":"2022-12-10T04:21:26.027357Z","shell.execute_reply.started":"2022-12-10T04:21:25.569474Z","shell.execute_reply":"2022-12-10T04:21:26.026149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_curve\nm1fpr , m1tpr , a = roc_curve(ytest , model1.predict(xtest))\n# plt.plot(fpr , tpr)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:26.029079Z","iopub.execute_input":"2022-12-10T04:21:26.029472Z","iopub.status.idle":"2022-12-10T04:21:26.037794Z","shell.execute_reply.started":"2022-12-10T04:21:26.029437Z","shell.execute_reply":"2022-12-10T04:21:26.036401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.tree import DecisionTreeClassifier\nmodel2 = DecisionTreeClassifier(criterion='entropy' , max_depth=3 , random_state = 10)\nmodel2.fit(xtrain,ytrain)\nprint(classification_report(ytest , model2.predict(xtest)))\nprint('      Fig:6 Classification report for Decision Tree model')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:26.041226Z","iopub.execute_input":"2022-12-10T04:21:26.042788Z","iopub.status.idle":"2022-12-10T04:21:26.094444Z","shell.execute_reply.started":"2022-12-10T04:21:26.042742Z","shell.execute_reply":"2022-12-10T04:21:26.093627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import tree\nplt.figure(figsize = (25 , 8))\ntree.plot_tree(model2);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:26.097853Z","iopub.execute_input":"2022-12-10T04:21:26.100250Z","iopub.status.idle":"2022-12-10T04:21:26.899780Z","shell.execute_reply.started":"2022-12-10T04:21:26.100210Z","shell.execute_reply":"2022-12-10T04:21:26.897936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(ytest , model2.predict(xtest)))\nplt.figure(figsize = (25,7))\nplt.subplot(121)\nsb.heatmap(confusion_matrix(ytest , model2.predict(xtest)  ,  normalize = 'all') , cmap = 'Greens' , annot = True)\nplt.title('Fig:7 Confusion matrix for Decision Tree' , weight = 'bold')\nplt.ylabel('Actual condition' , weight = 'bold')\nplt.xlabel('Predicted condition' , weight = 'bold')\n\nplt.subplot(122)\nft_imp = pd.DataFrame({ 'columns' : xtrain.columns , 'feat_impt' : model2.feature_importances_}).sort_values('feat_impt' , ascending = False)\nsb.barplot(x = 'columns' , y = 'feat_impt' , data =  ft_imp)\nplt.title('Fig:8 Important columns for Decision Tree model' , weight = 'bold')\nplt.xticks(rotation = 90);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:26.901743Z","iopub.execute_input":"2022-12-10T04:21:26.902167Z","iopub.status.idle":"2022-12-10T04:21:27.356274Z","shell.execute_reply.started":"2022-12-10T04:21:26.902134Z","shell.execute_reply":"2022-12-10T04:21:27.355114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m2fpr , m2tpr , a = roc_curve(ytest , model2.predict(xtest))\n# plt.plot(fpr , tpr)","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:27.361938Z","iopub.execute_input":"2022-12-10T04:21:27.362340Z","iopub.status.idle":"2022-12-10T04:21:27.372517Z","shell.execute_reply.started":"2022-12-10T04:21:27.362308Z","shell.execute_reply":"2022-12-10T04:21:27.370225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nmodel3 = RandomForestClassifier()\nmodel3.fit(xtrain,ytrain.values.ravel())\nprint(classification_report(ytest , model3.predict(xtest)))\nprint('      Fig:9 Classification report for Random Forest model')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:27.374069Z","iopub.execute_input":"2022-12-10T04:21:27.374609Z","iopub.status.idle":"2022-12-10T04:21:28.908941Z","shell.execute_reply.started":"2022-12-10T04:21:27.374571Z","shell.execute_reply":"2022-12-10T04:21:28.907689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(ytest , model3.predict(xtest)))\nplt.figure(figsize = (25,7))\nplt.subplot(121)\nsb.heatmap(confusion_matrix(ytest , model3.predict(xtest) , normalize = 'all') , cmap = 'Greens' , annot = True)\nplt.title('Fig:10 Confusion matrix for Random Forest' , weight = 'bold')\nplt.ylabel('Actual condition' , weight = 'bold')\nplt.xlabel('Predicted condition' , weight = 'bold')\n\nplt.subplot(122)\nft_imp = pd.DataFrame({ 'columns' : xtrain.columns , 'feat_impt' : model3.feature_importances_}).sort_values('feat_impt' , ascending = False)\nsb.barplot(x = 'columns' , y = 'feat_impt' , data =  ft_imp)\nplt.title('Fig:11 Important columns for Random Forest model' , weight = 'bold')\nplt.xticks(rotation = 90);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:28.910943Z","iopub.execute_input":"2022-12-10T04:21:28.911371Z","iopub.status.idle":"2022-12-10T04:21:29.493268Z","shell.execute_reply.started":"2022-12-10T04:21:28.911336Z","shell.execute_reply":"2022-12-10T04:21:29.491588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost\nxgb = xgboost.XGBClassifier()\nxgb.fit(xtrain,ytrain.values.ravel())\nprint(classification_report(ytest , xgb.predict(xtest)))\nprint('        Fig:12 Classification report for Xgboost model')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:29.494879Z","iopub.execute_input":"2022-12-10T04:21:29.495292Z","iopub.status.idle":"2022-12-10T04:21:30.374919Z","shell.execute_reply.started":"2022-12-10T04:21:29.495256Z","shell.execute_reply":"2022-12-10T04:21:30.374278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(ytest , xgb.predict(xtest)))\nplt.figure(figsize = (25,7))\nplt.subplot(121)\nsb.heatmap(confusion_matrix(ytest , xgb.predict(xtest) , normalize = 'all') , cmap = 'Greens' , annot = True)\nplt.title('Fig:13 Confusion matrix for Xgboost model' , weight = 'bold')\nplt.ylabel('Actual condition' , weight = 'bold')\nplt.xlabel('Predicted condition' , weight = 'bold')\n\nplt.subplot(122)\nft_imp = pd.DataFrame({ 'columns' : xtrain.columns , 'feat_impt' : xgb.feature_importances_}).sort_values('feat_impt' , ascending = False)\nsb.barplot(x = 'columns' , y = 'feat_impt' , data =  ft_imp)\nplt.title('Fig:14 Important columns for Xgboost model' , weight = 'bold')\nplt.xticks(rotation = 90);","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:30.375881Z","iopub.execute_input":"2022-12-10T04:21:30.376831Z","iopub.status.idle":"2022-12-10T04:21:30.835955Z","shell.execute_reply.started":"2022-12-10T04:21:30.376803Z","shell.execute_reply":"2022-12-10T04:21:30.834637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m1fpr , m1tpr , a = roc_curve(ytest , model1.predict(xtest))\nm2fpr , m2tpr , a = roc_curve(ytest , model2.predict(xtest))\nm3fpr , m3tpr , a = roc_curve(ytest , model3.predict(xtest))\nm4fpr , m4tpr , a = roc_curve(ytest , xgb.predict(xtest))","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:30.837334Z","iopub.execute_input":"2022-12-10T04:21:30.837649Z","iopub.status.idle":"2022-12-10T04:21:31.012640Z","shell.execute_reply.started":"2022-12-10T04:21:30.837609Z","shell.execute_reply":"2022-12-10T04:21:31.011880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\nplt.figure(figsize  =(20,8))\nplt.plot(np.linspace(0,1) , np.linspace(0,1) , linestyle = '--' , label = 'Random model')\nplt.plot(m1fpr , m1tpr , linestyle = '--' , marker = 'o'  ,label = 'Logistic Regression with AUC = {}'.format(round( roc_auc_score(ytest , model1.predict(xtest)) , 2 )) )\nplt.plot(m2fpr , m2tpr ,  linestyle = '--' , marker = 'o' , label = 'Decision Tree with AUC = {}'.format(round( roc_auc_score(ytest , model2.predict(xtest)) , 2 ))   )\nplt.plot(m3fpr , m3tpr ,  linestyle = '--' , marker = 'o' , label = 'RandomForest Classifier with AUC = {}'.format(round( roc_auc_score(ytest , model3.predict(xtest)) , 2 ))  )\nplt.plot(m4fpr , m4tpr ,  linestyle = '--' , marker = 'o' , label = 'XGBoost Classifier with AUC = {}'.format(round( roc_auc_score(ytest , xgb.predict(xtest)) , 2 ))  )\nplt.legend()\nplt.title('Fig:16 Comparing various classifier models' , weight = 'bold' , fontsize = 20)\nplt.xlabel('False Positive Rate' , weight = 'bold')\nplt.ylabel('True Positive Rate' , weight = 'bold')","metadata":{"execution":{"iopub.status.busy":"2022-12-10T04:21:31.016718Z","iopub.execute_input":"2022-12-10T04:21:31.018522Z","iopub.status.idle":"2022-12-10T04:21:31.422529Z","shell.execute_reply.started":"2022-12-10T04:21:31.018489Z","shell.execute_reply":"2022-12-10T04:21:31.421071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}