{"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-07-12T16:25:45.429356Z","iopub.execute_input":"2023-07-12T16:25:45.429717Z","iopub.status.idle":"2023-07-12T16:25:45.439920Z","shell.execute_reply.started":"2023-07-12T16:25:45.429688Z","shell.execute_reply":"2023-07-12T16:25:45.438750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Project Name : Bank Customer Churn Modelling\n\n**About the data set** :\n  The data set contains of pariculars of 1000 customers as rows  with 14 different features . the main aim of the data set is to predict whether the customer has left the bank or not..\n  \n  This is clasissfication Probem\n  \n  **Models Applied:**\n  \n* Logistic Regression\n* SVM\n* KNN\n* Random Forest\n* Gradient Boosting\n  \n  \n  ","metadata":{}},{"cell_type":"code","source":"#Importing Additional libraries\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.441274Z","iopub.execute_input":"2023-07-12T16:25:45.441654Z","iopub.status.idle":"2023-07-12T16:25:45.455930Z","shell.execute_reply.started":"2023-07-12T16:25:45.441628Z","shell.execute_reply":"2023-07-12T16:25:45.454745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading the Data set","metadata":{}},{"cell_type":"code","source":"data= pd.read_csv('/kaggle/input/churn-modelling/Churn_Modelling.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.457245Z","iopub.execute_input":"2023-07-12T16:25:45.457545Z","iopub.status.idle":"2023-07-12T16:25:45.485926Z","shell.execute_reply.started":"2023-07-12T16:25:45.457521Z","shell.execute_reply":"2023-07-12T16:25:45.484759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n## 1. Display Top 5 Rows of The Dataset","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.487044Z","iopub.execute_input":"2023-07-12T16:25:45.487338Z","iopub.status.idle":"2023-07-12T16:25:45.502497Z","shell.execute_reply.started":"2023-07-12T16:25:45.487308Z","shell.execute_reply":"2023-07-12T16:25:45.501745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting the shape of the data","metadata":{}},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.503906Z","iopub.execute_input":"2023-07-12T16:25:45.504184Z","iopub.status.idle":"2023-07-12T16:25:45.518650Z","shell.execute_reply.started":"2023-07-12T16:25:45.504161Z","shell.execute_reply":"2023-07-12T16:25:45.517365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Getting information about the data ","metadata":{}},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.520108Z","iopub.execute_input":"2023-07-12T16:25:45.520423Z","iopub.status.idle":"2023-07-12T16:25:45.542953Z","shell.execute_reply.started":"2023-07-12T16:25:45.520398Z","shell.execute_reply":"2023-07-12T16:25:45.542198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**There is no null values in the Dataset**\n\n## Getting the statistical informtaion about the data","metadata":{}},{"cell_type":"code","source":"data.describe()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.544021Z","iopub.execute_input":"2023-07-12T16:25:45.545024Z","iopub.status.idle":"2023-07-12T16:25:45.582833Z","shell.execute_reply.started":"2023-07-12T16:25:45.544991Z","shell.execute_reply":"2023-07-12T16:25:45.581857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data preprocessing","metadata":{}},{"cell_type":"code","source":"data.corr()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:45.584070Z","iopub.execute_input":"2023-07-12T16:25:45.584407Z","iopub.status.idle":"2023-07-12T16:25:45.603891Z","shell.execute_reply.started":"2023-07-12T16:25:45.584379Z","shell.execute_reply":"2023-07-12T16:25:45.602892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting the graph of Correlation\nplt.figure(figsize=(10,5))\nsns.heatmap(data.corr(),cmap='coolwarm',annot=True,fmt='0.1g',linewidth=1.5,linecolor='white',cbar=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:28:49.650519Z","iopub.execute_input":"2023-07-12T16:28:49.650938Z","iopub.status.idle":"2023-07-12T16:28:50.286347Z","shell.execute_reply.started":"2023-07-12T16:28:49.650905Z","shell.execute_reply":"2023-07-12T16:28:50.285019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dropping the Irrelevant columns ( Row number, Customer Id, Surname )\ndata.drop(columns=['RowNumber','CustomerId','Surname'],axis=1,inplace=True)\ndata.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.204176Z","iopub.execute_input":"2023-07-12T16:25:46.204664Z","iopub.status.idle":"2023-07-12T16:25:46.220739Z","shell.execute_reply.started":"2023-07-12T16:25:46.204627Z","shell.execute_reply":"2023-07-12T16:25:46.219444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Encoding categorical Data","metadata":{}},{"cell_type":"code","source":"# Encoding the categorical value by get dummies method\ndata=pd.get_dummies(data,drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.222329Z","iopub.execute_input":"2023-07-12T16:25:46.222646Z","iopub.status.idle":"2023-07-12T16:25:46.240619Z","shell.execute_reply.started":"2023-07-12T16:25:46.222620Z","shell.execute_reply":"2023-07-12T16:25:46.239091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # verifying the first few rows.\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.242104Z","iopub.execute_input":"2023-07-12T16:25:46.243534Z","iopub.status.idle":"2023-07-12T16:25:46.261098Z","shell.execute_reply.started":"2023-07-12T16:25:46.243485Z","shell.execute_reply":"2023-07-12T16:25:46.259700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# verifying the shape of the data\ndata.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.262603Z","iopub.execute_input":"2023-07-12T16:25:46.263109Z","iopub.status.idle":"2023-07-12T16:25:46.275770Z","shell.execute_reply.started":"2023-07-12T16:25:46.263078Z","shell.execute_reply":"2023-07-12T16:25:46.274504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Verifying  the  poprtion the data, whether balanced or imbalanced by checking the target variable ","metadata":{}},{"cell_type":"code","source":"#  Segregating the independent and the dependent variable from the data set \nX= data.drop('Exited',axis=1)\ny=data['Exited']","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.277066Z","iopub.execute_input":"2023-07-12T16:25:46.277722Z","iopub.status.idle":"2023-07-12T16:25:46.289198Z","shell.execute_reply.started":"2023-07-12T16:25:46.277687Z","shell.execute_reply":"2023-07-12T16:25:46.288279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verifying the first few rows X  \nX.head(3)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.291046Z","iopub.execute_input":"2023-07-12T16:25:46.291380Z","iopub.status.idle":"2023-07-12T16:25:46.311602Z","shell.execute_reply.started":"2023-07-12T16:25:46.291355Z","shell.execute_reply":"2023-07-12T16:25:46.310429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## verifying the target varaiabe  output  for Knowing the proportion of data .\n","metadata":{}},{"cell_type":"code","source":"# Cheking the value count of the data.\ny.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.313361Z","iopub.execute_input":"2023-07-12T16:25:46.313649Z","iopub.status.idle":"2023-07-12T16:25:46.322918Z","shell.execute_reply.started":"2023-07-12T16:25:46.313626Z","shell.execute_reply":"2023-07-12T16:25:46.321793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The target data seems to be imbalanced. hence there is every chance of bias prediction towards the majority count. hence we balance by smote method","metadata":{}},{"cell_type":"markdown","source":"## Handling imbalance data with SMOTE","metadata":{}},{"cell_type":"code","source":"# Employing the oversample method and importing Smote to Adjust te imbalance\nfrom imblearn.over_sampling  import SMOTE","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.324827Z","iopub.execute_input":"2023-07-12T16:25:46.325202Z","iopub.status.idle":"2023-07-12T16:25:46.332651Z","shell.execute_reply.started":"2023-07-12T16:25:46.325173Z","shell.execute_reply":"2023-07-12T16:25:46.331986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting the data \nX_res,y_res=SMOTE().fit_resample(X,y)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.333609Z","iopub.execute_input":"2023-07-12T16:25:46.334930Z","iopub.status.idle":"2023-07-12T16:25:46.369669Z","shell.execute_reply.started":"2023-07-12T16:25:46.334881Z","shell.execute_reply":"2023-07-12T16:25:46.368962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the Data  after Smote \ny_res.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.370618Z","iopub.execute_input":"2023-07-12T16:25:46.371152Z","iopub.status.idle":"2023-07-12T16:25:46.376750Z","shell.execute_reply.started":"2023-07-12T16:25:46.371129Z","shell.execute_reply":"2023-07-12T16:25:46.375892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now the data has been balanced  we can go for train test Split\n\n\n# Train Test Split\n ","metadata":{}},{"cell_type":"code","source":"# importing the necessary library\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.377928Z","iopub.execute_input":"2023-07-12T16:25:46.378243Z","iopub.status.idle":"2023-07-12T16:25:46.390475Z","shell.execute_reply.started":"2023-07-12T16:25:46.378223Z","shell.execute_reply":"2023-07-12T16:25:46.389189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Splitting the training and test Split\nX_train,X_test,y_train,y_test= train_test_split(X_res,y_res,test_size=0.20,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.391400Z","iopub.execute_input":"2023-07-12T16:25:46.392126Z","iopub.status.idle":"2023-07-12T16:25:46.408333Z","shell.execute_reply.started":"2023-07-12T16:25:46.392103Z","shell.execute_reply":"2023-07-12T16:25:46.407414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature scaling","metadata":{}},{"cell_type":"code","source":"# importing the necessary libraries\nfrom sklearn.preprocessing import StandardScaler\n","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.409427Z","iopub.execute_input":"2023-07-12T16:25:46.409691Z","iopub.status.idle":"2023-07-12T16:25:46.419857Z","shell.execute_reply.started":"2023-07-12T16:25:46.409668Z","shell.execute_reply":"2023-07-12T16:25:46.419139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.420882Z","iopub.execute_input":"2023-07-12T16:25:46.421299Z","iopub.status.idle":"2023-07-12T16:25:46.443057Z","shell.execute_reply.started":"2023-07-12T16:25:46.421259Z","shell.execute_reply":"2023-07-12T16:25:46.441883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There still exist some scaling difference between our datas,which we can bring down by using feature scaling.","metadata":{}},{"cell_type":"code","source":"# Creating an instance for StandardScaler\nsc=StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.444478Z","iopub.execute_input":"2023-07-12T16:25:46.445054Z","iopub.status.idle":"2023-07-12T16:25:46.455937Z","shell.execute_reply.started":"2023-07-12T16:25:46.445024Z","shell.execute_reply":"2023-07-12T16:25:46.455084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fitting the data to X train and X test\nX_train=sc.fit_transform(X_train)\nX_test = sc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.466235Z","iopub.execute_input":"2023-07-12T16:25:46.467012Z","iopub.status.idle":"2023-07-12T16:25:46.476605Z","shell.execute_reply.started":"2023-07-12T16:25:46.466986Z","shell.execute_reply":"2023-07-12T16:25:46.475816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verifying the few rows of X_train\nX_train","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.477687Z","iopub.execute_input":"2023-07-12T16:25:46.478086Z","iopub.status.idle":"2023-07-12T16:25:46.487254Z","shell.execute_reply.started":"2023-07-12T16:25:46.478064Z","shell.execute_reply":"2023-07-12T16:25:46.486266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Fitting\n","metadata":{}},{"cell_type":"code","source":"# importing the necessary libraries\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn import svm\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.metrics import accuracy_score, precision_score,recall_score, f1_score","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.488256Z","iopub.execute_input":"2023-07-12T16:25:46.489842Z","iopub.status.idle":"2023-07-12T16:25:46.499922Z","shell.execute_reply.started":"2023-07-12T16:25:46.489802Z","shell.execute_reply":"2023-07-12T16:25:46.499142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model-1: Logistic Regression**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\nlog=LogisticRegression()\nlog.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.500896Z","iopub.execute_input":"2023-07-12T16:25:46.501686Z","iopub.status.idle":"2023-07-12T16:25:46.538149Z","shell.execute_reply.started":"2023-07-12T16:25:46.501642Z","shell.execute_reply":"2023-07-12T16:25:46.537048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred1=log.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.539471Z","iopub.execute_input":"2023-07-12T16:25:46.539774Z","iopub.status.idle":"2023-07-12T16:25:46.544999Z","shell.execute_reply.started":"2023-07-12T16:25:46.539747Z","shell.execute_reply":"2023-07-12T16:25:46.544023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the accuracy scores\naccuracy_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.546514Z","iopub.execute_input":"2023-07-12T16:25:46.547072Z","iopub.status.idle":"2023-07-12T16:25:46.561109Z","shell.execute_reply.started":"2023-07-12T16:25:46.547042Z","shell.execute_reply":"2023-07-12T16:25:46.560308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  precision scores\nprecision_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.562331Z","iopub.execute_input":"2023-07-12T16:25:46.563532Z","iopub.status.idle":"2023-07-12T16:25:46.575358Z","shell.execute_reply.started":"2023-07-12T16:25:46.563501Z","shell.execute_reply":"2023-07-12T16:25:46.574635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.576546Z","iopub.execute_input":"2023-07-12T16:25:46.577332Z","iopub.status.idle":"2023-07-12T16:25:46.593620Z","shell.execute_reply.started":"2023-07-12T16:25:46.577274Z","shell.execute_reply":"2023-07-12T16:25:46.592823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred1)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.594940Z","iopub.execute_input":"2023-07-12T16:25:46.595513Z","iopub.status.idle":"2023-07-12T16:25:46.621601Z","shell.execute_reply.started":"2023-07-12T16:25:46.595485Z","shell.execute_reply":"2023-07-12T16:25:46.620804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model-2: SVM**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\nsvm=svm.SVC()\nsvm.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:46.622988Z","iopub.execute_input":"2023-07-12T16:25:46.623582Z","iopub.status.idle":"2023-07-12T16:25:49.569923Z","shell.execute_reply.started":"2023-07-12T16:25:46.623553Z","shell.execute_reply":"2023-07-12T16:25:49.568612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred2=svm.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:49.571256Z","iopub.execute_input":"2023-07-12T16:25:49.571607Z","iopub.status.idle":"2023-07-12T16:25:50.308850Z","shell.execute_reply.started":"2023-07-12T16:25:49.571578Z","shell.execute_reply":"2023-07-12T16:25:50.307484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  precision scores\naccuracy_score(y_test,y_pred2)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.310341Z","iopub.execute_input":"2023-07-12T16:25:50.310729Z","iopub.status.idle":"2023-07-12T16:25:50.320546Z","shell.execute_reply.started":"2023-07-12T16:25:50.310696Z","shell.execute_reply":"2023-07-12T16:25:50.319159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  precision scores\nprecision_score(y_test,y_pred2)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.322272Z","iopub.execute_input":"2023-07-12T16:25:50.322678Z","iopub.status.idle":"2023-07-12T16:25:50.337925Z","shell.execute_reply.started":"2023-07-12T16:25:50.322646Z","shell.execute_reply":"2023-07-12T16:25:50.336842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred2)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.339383Z","iopub.execute_input":"2023-07-12T16:25:50.339730Z","iopub.status.idle":"2023-07-12T16:25:50.350262Z","shell.execute_reply.started":"2023-07-12T16:25:50.339698Z","shell.execute_reply":"2023-07-12T16:25:50.349355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred2)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.351560Z","iopub.execute_input":"2023-07-12T16:25:50.351929Z","iopub.status.idle":"2023-07-12T16:25:50.366301Z","shell.execute_reply.started":"2023-07-12T16:25:50.351871Z","shell.execute_reply":"2023-07-12T16:25:50.365103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model-3: KNeighborsClassifier**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\nknn=KNeighborsClassifier()\nknn.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.367820Z","iopub.execute_input":"2023-07-12T16:25:50.368104Z","iopub.status.idle":"2023-07-12T16:25:50.401233Z","shell.execute_reply.started":"2023-07-12T16:25:50.368077Z","shell.execute_reply":"2023-07-12T16:25:50.400075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred3=knn.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.402454Z","iopub.execute_input":"2023-07-12T16:25:50.402877Z","iopub.status.idle":"2023-07-12T16:25:50.659007Z","shell.execute_reply.started":"2023-07-12T16:25:50.402854Z","shell.execute_reply":"2023-07-12T16:25:50.658004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the accuracy\naccuracy_score(y_test,y_pred3)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.660372Z","iopub.execute_input":"2023-07-12T16:25:50.660728Z","iopub.status.idle":"2023-07-12T16:25:50.671261Z","shell.execute_reply.started":"2023-07-12T16:25:50.660697Z","shell.execute_reply":"2023-07-12T16:25:50.670322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  precision scores\nprecision_score(y_test,y_pred3)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.672393Z","iopub.execute_input":"2023-07-12T16:25:50.672678Z","iopub.status.idle":"2023-07-12T16:25:50.689102Z","shell.execute_reply.started":"2023-07-12T16:25:50.672652Z","shell.execute_reply":"2023-07-12T16:25:50.687692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred3)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.690250Z","iopub.execute_input":"2023-07-12T16:25:50.690653Z","iopub.status.idle":"2023-07-12T16:25:50.704839Z","shell.execute_reply.started":"2023-07-12T16:25:50.690621Z","shell.execute_reply":"2023-07-12T16:25:50.703634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred3)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.706072Z","iopub.execute_input":"2023-07-12T16:25:50.706550Z","iopub.status.idle":"2023-07-12T16:25:50.719386Z","shell.execute_reply.started":"2023-07-12T16:25:50.706519Z","shell.execute_reply":"2023-07-12T16:25:50.717785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing the necessary libraries\n\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier\nfrom sklearn.metrics import accuracy_score,precision_score,recall_score,f1_score","metadata":{}},{"cell_type":"markdown","source":"**Model-4: Decision Tree Classifier**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\ndtc=DecisionTreeClassifier()\ndtc.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.723347Z","iopub.execute_input":"2023-07-12T16:25:50.723790Z","iopub.status.idle":"2023-07-12T16:25:50.805773Z","shell.execute_reply.started":"2023-07-12T16:25:50.723748Z","shell.execute_reply":"2023-07-12T16:25:50.804075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred4=dtc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.807549Z","iopub.execute_input":"2023-07-12T16:25:50.807868Z","iopub.status.idle":"2023-07-12T16:25:50.813593Z","shell.execute_reply.started":"2023-07-12T16:25:50.807840Z","shell.execute_reply":"2023-07-12T16:25:50.812184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the accuracy scores\naccuracy_score(y_test,y_pred4)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.815384Z","iopub.execute_input":"2023-07-12T16:25:50.815767Z","iopub.status.idle":"2023-07-12T16:25:50.882141Z","shell.execute_reply.started":"2023-07-12T16:25:50.815733Z","shell.execute_reply":"2023-07-12T16:25:50.881119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the precision scores\nprecision_score(y_test,y_pred4)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.883449Z","iopub.execute_input":"2023-07-12T16:25:50.883929Z","iopub.status.idle":"2023-07-12T16:25:50.899268Z","shell.execute_reply.started":"2023-07-12T16:25:50.883904Z","shell.execute_reply":"2023-07-12T16:25:50.898406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred4)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.900342Z","iopub.execute_input":"2023-07-12T16:25:50.900665Z","iopub.status.idle":"2023-07-12T16:25:50.915767Z","shell.execute_reply.started":"2023-07-12T16:25:50.900637Z","shell.execute_reply":"2023-07-12T16:25:50.914939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred4)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.916901Z","iopub.execute_input":"2023-07-12T16:25:50.918033Z","iopub.status.idle":"2023-07-12T16:25:50.930798Z","shell.execute_reply.started":"2023-07-12T16:25:50.917952Z","shell.execute_reply":"2023-07-12T16:25:50.929468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model-5: Random Forest Classifier**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\nrfc=RandomForestClassifier()\nrfc.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:50.932225Z","iopub.execute_input":"2023-07-12T16:25:50.933524Z","iopub.status.idle":"2023-07-12T16:25:52.687551Z","shell.execute_reply.started":"2023-07-12T16:25:50.933473Z","shell.execute_reply":"2023-07-12T16:25:52.686655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred5=rfc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.688571Z","iopub.execute_input":"2023-07-12T16:25:52.688811Z","iopub.status.idle":"2023-07-12T16:25:52.760967Z","shell.execute_reply.started":"2023-07-12T16:25:52.688789Z","shell.execute_reply":"2023-07-12T16:25:52.759700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the accuracy scores\naccuracy_score(y_test,y_pred5)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.762366Z","iopub.execute_input":"2023-07-12T16:25:52.763166Z","iopub.status.idle":"2023-07-12T16:25:52.770575Z","shell.execute_reply.started":"2023-07-12T16:25:52.763137Z","shell.execute_reply":"2023-07-12T16:25:52.769057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the precision scores\nprecision_score(y_test,y_pred5)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.772624Z","iopub.execute_input":"2023-07-12T16:25:52.773027Z","iopub.status.idle":"2023-07-12T16:25:52.789745Z","shell.execute_reply.started":"2023-07-12T16:25:52.772997Z","shell.execute_reply":"2023-07-12T16:25:52.788787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred5)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.791004Z","iopub.execute_input":"2023-07-12T16:25:52.791354Z","iopub.status.idle":"2023-07-12T16:25:52.804339Z","shell.execute_reply.started":"2023-07-12T16:25:52.791312Z","shell.execute_reply":"2023-07-12T16:25:52.802928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred5)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.805919Z","iopub.execute_input":"2023-07-12T16:25:52.806231Z","iopub.status.idle":"2023-07-12T16:25:52.821293Z","shell.execute_reply.started":"2023-07-12T16:25:52.806202Z","shell.execute_reply":"2023-07-12T16:25:52.819868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model-6: Gradient Boosting Classifier**","metadata":{}},{"cell_type":"code","source":"# Creating an instance and fitting the data\ngbc=GradientBoostingClassifier()\ngbc.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:52.822333Z","iopub.execute_input":"2023-07-12T16:25:52.822595Z","iopub.status.idle":"2023-07-12T16:25:54.639362Z","shell.execute_reply.started":"2023-07-12T16:25:52.822572Z","shell.execute_reply":"2023-07-12T16:25:54.638112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction of X_test\ny_pred6=gbc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.640710Z","iopub.execute_input":"2023-07-12T16:25:54.641051Z","iopub.status.idle":"2023-07-12T16:25:54.652693Z","shell.execute_reply.started":"2023-07-12T16:25:54.641024Z","shell.execute_reply":"2023-07-12T16:25:54.652014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the accuracy Score\naccuracy_score(y_test,y_pred6)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.653845Z","iopub.execute_input":"2023-07-12T16:25:54.654975Z","iopub.status.idle":"2023-07-12T16:25:54.662245Z","shell.execute_reply.started":"2023-07-12T16:25:54.654942Z","shell.execute_reply":"2023-07-12T16:25:54.661216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the precision Score\nprecision_score(y_test,y_pred6)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.663199Z","iopub.execute_input":"2023-07-12T16:25:54.663846Z","iopub.status.idle":"2023-07-12T16:25:54.678363Z","shell.execute_reply.started":"2023-07-12T16:25:54.663821Z","shell.execute_reply":"2023-07-12T16:25:54.677433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  recall scores\nrecall_score(y_test,y_pred6)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.679348Z","iopub.execute_input":"2023-07-12T16:25:54.679599Z","iopub.status.idle":"2023-07-12T16:25:54.691693Z","shell.execute_reply.started":"2023-07-12T16:25:54.679578Z","shell.execute_reply":"2023-07-12T16:25:54.690702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting the  f1_score scores\nf1_score(y_test,y_pred6)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.693239Z","iopub.execute_input":"2023-07-12T16:25:54.694394Z","iopub.status.idle":"2023-07-12T16:25:54.707425Z","shell.execute_reply.started":"2023-07-12T16:25:54.694343Z","shell.execute_reply":"2023-07-12T16:25:54.706427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Summary of the scores**","metadata":{}},{"cell_type":"code","source":"# compiling the accuracy scores into a dataframe.\nfinal_data1= pd.DataFrame({'Models': ['LOG','SVM','KNN','DTC','RFC','GBC'],\n                         'ACC': [accuracy_score(y_test,y_pred1)*100,\n                                accuracy_score(y_test,y_pred2)*100,\n                                accuracy_score(y_test,y_pred3)*100,\n                                accuracy_score(y_test,y_pred4)*100,\n                                accuracy_score(y_test,y_pred5)*100,\n                                accuracy_score(y_test,y_pred6)*100] })               \n                         \n                        \nfinal_data1","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.710939Z","iopub.execute_input":"2023-07-12T16:25:54.711235Z","iopub.status.idle":"2023-07-12T16:25:54.727097Z","shell.execute_reply.started":"2023-07-12T16:25:54.711211Z","shell.execute_reply":"2023-07-12T16:25:54.725889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Plotting the same into a graph**","metadata":{}},{"cell_type":"code","source":"# plotting the graph of he above Accuracy metrics\nplt.figure(figsize=(5,3))\nsns.barplot(x='Models',y='ACC',data=final_data1,palette='Accent')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.728475Z","iopub.execute_input":"2023-07-12T16:25:54.728783Z","iopub.status.idle":"2023-07-12T16:25:54.867579Z","shell.execute_reply.started":"2023-07-12T16:25:54.728745Z","shell.execute_reply":"2023-07-12T16:25:54.866681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compiling the accuracy scores into a dataframe.\nfinal_data2= pd.DataFrame({'Models': ['LOG','SVM','KNN','DTC','RFC','GBC'],\n                         'Precision': [precision_score(y_test,y_pred1)*100,\n                                precision_score(y_test,y_pred2)*100,\n                                precision_score(y_test,y_pred3)*100,\n                                precision_score(y_test,y_pred4)*100,\n                                precision_score(y_test,y_pred5)*100,\n                                precision_score(y_test,y_pred6)*100] })               \n                         \n                        \nfinal_data2","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.868715Z","iopub.execute_input":"2023-07-12T16:25:54.869903Z","iopub.status.idle":"2023-07-12T16:25:54.898169Z","shell.execute_reply.started":"2023-07-12T16:25:54.869871Z","shell.execute_reply":"2023-07-12T16:25:54.897132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting the graph of he above precision metrics\nplt.figure(figsize=(5,3))\nsns.barplot(x='Models',y='Precision',data=final_data2,palette='Set2')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:54.899807Z","iopub.execute_input":"2023-07-12T16:25:54.900170Z","iopub.status.idle":"2023-07-12T16:25:55.044064Z","shell.execute_reply.started":"2023-07-12T16:25:54.900136Z","shell.execute_reply":"2023-07-12T16:25:55.042704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compiling the accuracy scores into a dataframe.\nfinal_data3= pd.DataFrame({'Models': ['LOG','SVM','KNN','DTC','RFC','GBC'],\n                         'Recall': [recall_score(y_test,y_pred1)*100,\n                                recall_score(y_test,y_pred2)*100,\n                                recall_score(y_test,y_pred3)*100,\n                                recall_score(y_test,y_pred4)*100,\n                                recall_score(y_test,y_pred5)*100,\n                                recall_score(y_test,y_pred6)*100] })         \n                         \n                        \nfinal_data3","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.045965Z","iopub.execute_input":"2023-07-12T16:25:55.046600Z","iopub.status.idle":"2023-07-12T16:25:55.072890Z","shell.execute_reply.started":"2023-07-12T16:25:55.046569Z","shell.execute_reply":"2023-07-12T16:25:55.072020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting the graph of the above recall metrics\nplt.figure(figsize=(5,3))\nsns.barplot(x='Models',y='Recall',data=final_data3,palette='Set3')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.074220Z","iopub.execute_input":"2023-07-12T16:25:55.074548Z","iopub.status.idle":"2023-07-12T16:25:55.204660Z","shell.execute_reply.started":"2023-07-12T16:25:55.074519Z","shell.execute_reply":"2023-07-12T16:25:55.203831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compiling the accuracy scores into a dataframe.\nfinal_data4= pd.DataFrame({'Models': ['LOG','SVM','KNN','DTC','RFC','GBC'],\n                         'f1_score': [f1_score(y_test,y_pred1)*100,\n                                f1_score(y_test,y_pred2)*100,\n                                f1_score(y_test,y_pred3)*100,\n                                f1_score(y_test,y_pred4)*100,\n                                f1_score(y_test,y_pred5)*100,\n                                f1_score(y_test,y_pred6)*100] })       \n                         \n                        \nfinal_data4","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.205967Z","iopub.execute_input":"2023-07-12T16:25:55.206471Z","iopub.status.idle":"2023-07-12T16:25:55.231322Z","shell.execute_reply.started":"2023-07-12T16:25:55.206443Z","shell.execute_reply":"2023-07-12T16:25:55.230473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plotting the graph of the above recall metrics\nplt.figure(figsize=(5,3))\nsns.barplot(x='Models',y='f1_score',data=final_data4,palette='Dark2')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.232651Z","iopub.execute_input":"2023-07-12T16:25:55.232955Z","iopub.status.idle":"2023-07-12T16:25:55.360316Z","shell.execute_reply.started":"2023-07-12T16:25:55.232927Z","shell.execute_reply":"2023-07-12T16:25:55.359331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Remarks: Since in all the metrics Random forest classifier gives us the highest Accuracy score, and hence we should take Random forest classifier as our Model","metadata":{}},{"cell_type":"markdown","source":"# Save the model","metadata":{}},{"cell_type":"code","source":"# We need to do Feature scale by using standard scaler  on the full data O independent variable of X_res\nX_res=sc.fit_transform(X_res)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.361737Z","iopub.execute_input":"2023-07-12T16:25:55.362038Z","iopub.status.idle":"2023-07-12T16:25:55.371931Z","shell.execute_reply.started":"2023-07-12T16:25:55.362011Z","shell.execute_reply":"2023-07-12T16:25:55.371221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting our best model  on the entire data\nrfc=RandomForestClassifier()\nrfc.fit(X_res,y_res)","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:55.373106Z","iopub.execute_input":"2023-07-12T16:25:55.373601Z","iopub.status.idle":"2023-07-12T16:25:57.606477Z","shell.execute_reply.started":"2023-07-12T16:25:55.373573Z","shell.execute_reply":"2023-07-12T16:25:57.605460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#  For Saving our Model using Joblib library, we need to import the same \nimport joblib","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:57.607622Z","iopub.execute_input":"2023-07-12T16:25:57.607947Z","iopub.status.idle":"2023-07-12T16:25:57.611805Z","shell.execute_reply.started":"2023-07-12T16:25:57.607917Z","shell.execute_reply":"2023-07-12T16:25:57.611220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving our Model\njoblib.dump(rfc,'Churn_Pred_Model')","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:57.612609Z","iopub.execute_input":"2023-07-12T16:25:57.613051Z","iopub.status.idle":"2023-07-12T16:25:57.712618Z","shell.execute_reply.started":"2023-07-12T16:25:57.613030Z","shell.execute_reply":"2023-07-12T16:25:57.711498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assigning the model to a variable for future prediction\nmodel= joblib.load('Churn_Pred_Model')","metadata":{"execution":{"iopub.status.busy":"2023-07-12T16:25:57.713513Z","iopub.execute_input":"2023-07-12T16:25:57.713774Z","iopub.status.idle":"2023-07-12T16:25:57.785577Z","shell.execute_reply.started":"2023-07-12T16:25:57.713751Z","shell.execute_reply":"2023-07-12T16:25:57.784328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Thank you ....","metadata":{}}]}