{"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":"markdown","source":"# Importing the Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport seaborn as sns\nimport os\nimport sklearn","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:19.418410Z","iopub.execute_input":"2022-08-11T12:49:19.419380Z","iopub.status.idle":"2022-08-11T12:49:20.719630Z","shell.execute_reply.started":"2022-08-11T12:49:19.419261Z","shell.execute_reply":"2022-08-11T12:49:20.718385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing and Preparing the Training and Test Datasets","metadata":{}},{"cell_type":"code","source":"X_train = pd.read_csv(r'../input/titanic/train.csv')\nX_test = pd.read_csv(r'../input/titanic/test.csv')\nGender_Data = pd.read_csv(r'../input/titanic/gender_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.722800Z","iopub.execute_input":"2022-08-11T12:49:20.723863Z","iopub.status.idle":"2022-08-11T12:49:20.756694Z","shell.execute_reply.started":"2022-08-11T12:49:20.723818Z","shell.execute_reply":"2022-08-11T12:49:20.755628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test['Survived'] = Gender_Data['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.758359Z","iopub.execute_input":"2022-08-11T12:49:20.759504Z","iopub.status.idle":"2022-08-11T12:49:20.771589Z","shell.execute_reply.started":"2022-08-11T12:49:20.759456Z","shell.execute_reply":"2022-08-11T12:49:20.770472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Understanding the Data","metadata":{}},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.773916Z","iopub.execute_input":"2022-08-11T12:49:20.774310Z","iopub.status.idle":"2022-08-11T12:49:20.788802Z","shell.execute_reply.started":"2022-08-11T12:49:20.774276Z","shell.execute_reply":"2022-08-11T12:49:20.787484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.790111Z","iopub.execute_input":"2022-08-11T12:49:20.790701Z","iopub.status.idle":"2022-08-11T12:49:20.804465Z","shell.execute_reply.started":"2022-08-11T12:49:20.790667Z","shell.execute_reply":"2022-08-11T12:49:20.803160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Gender_Data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.806026Z","iopub.execute_input":"2022-08-11T12:49:20.806430Z","iopub.status.idle":"2022-08-11T12:49:20.819578Z","shell.execute_reply.started":"2022-08-11T12:49:20.806394Z","shell.execute_reply":"2022-08-11T12:49:20.818643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.821020Z","iopub.execute_input":"2022-08-11T12:49:20.821683Z","iopub.status.idle":"2022-08-11T12:49:20.850260Z","shell.execute_reply.started":"2022-08-11T12:49:20.821645Z","shell.execute_reply":"2022-08-11T12:49:20.849340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.851642Z","iopub.execute_input":"2022-08-11T12:49:20.852353Z","iopub.status.idle":"2022-08-11T12:49:20.873969Z","shell.execute_reply.started":"2022-08-11T12:49:20.852318Z","shell.execute_reply":"2022-08-11T12:49:20.872746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Gender_Data.info()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.875690Z","iopub.execute_input":"2022-08-11T12:49:20.876417Z","iopub.status.idle":"2022-08-11T12:49:20.896683Z","shell.execute_reply.started":"2022-08-11T12:49:20.876378Z","shell.execute_reply":"2022-08-11T12:49:20.895484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.900565Z","iopub.execute_input":"2022-08-11T12:49:20.901225Z","iopub.status.idle":"2022-08-11T12:49:20.914845Z","shell.execute_reply.started":"2022-08-11T12:49:20.901151Z","shell.execute_reply":"2022-08-11T12:49:20.913668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.916531Z","iopub.execute_input":"2022-08-11T12:49:20.918022Z","iopub.status.idle":"2022-08-11T12:49:20.935695Z","shell.execute_reply.started":"2022-08-11T12:49:20.917970Z","shell.execute_reply":"2022-08-11T12:49:20.934341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Gender_Data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.937770Z","iopub.execute_input":"2022-08-11T12:49:20.938795Z","iopub.status.idle":"2022-08-11T12:49:20.950517Z","shell.execute_reply.started":"2022-08-11T12:49:20.938734Z","shell.execute_reply":"2022-08-11T12:49:20.949570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dealing with the Null Values","metadata":{}},{"cell_type":"code","source":"X_train['Age'] = X_train['Age'].fillna(X_train['Age'].median())\nX_test['Age'] = X_test['Age'].fillna(X_test['Age'].median())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.951701Z","iopub.execute_input":"2022-08-11T12:49:20.952389Z","iopub.status.idle":"2022-08-11T12:49:20.967523Z","shell.execute_reply.started":"2022-08-11T12:49:20.952354Z","shell.execute_reply":"2022-08-11T12:49:20.966464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.drop(['Cabin'], axis=1, inplace=True)\nX_test.drop(['Cabin'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.969098Z","iopub.execute_input":"2022-08-11T12:49:20.969477Z","iopub.status.idle":"2022-08-11T12:49:20.983378Z","shell.execute_reply.started":"2022-08-11T12:49:20.969444Z","shell.execute_reply":"2022-08-11T12:49:20.981842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test['Fare'] = X_test['Fare'].fillna(X_test['Fare'].mean())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.985277Z","iopub.execute_input":"2022-08-11T12:49:20.986017Z","iopub.status.idle":"2022-08-11T12:49:20.996418Z","shell.execute_reply.started":"2022-08-11T12:49:20.985967Z","shell.execute_reply":"2022-08-11T12:49:20.994957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['Embarked'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:20.998554Z","iopub.execute_input":"2022-08-11T12:49:20.999587Z","iopub.status.idle":"2022-08-11T12:49:21.013832Z","shell.execute_reply.started":"2022-08-11T12:49:20.999535Z","shell.execute_reply":"2022-08-11T12:49:21.012425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['Embarked'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.015615Z","iopub.execute_input":"2022-08-11T12:49:21.016745Z","iopub.status.idle":"2022-08-11T12:49:21.036136Z","shell.execute_reply.started":"2022-08-11T12:49:21.016704Z","shell.execute_reply":"2022-08-11T12:49:21.034941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['Embarked'] = X_train['Embarked'].fillna('S')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.038751Z","iopub.execute_input":"2022-08-11T12:49:21.039386Z","iopub.status.idle":"2022-08-11T12:49:21.055935Z","shell.execute_reply.started":"2022-08-11T12:49:21.039339Z","shell.execute_reply":"2022-08-11T12:49:21.054407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.057992Z","iopub.execute_input":"2022-08-11T12:49:21.058799Z","iopub.status.idle":"2022-08-11T12:49:21.075744Z","shell.execute_reply.started":"2022-08-11T12:49:21.058750Z","shell.execute_reply":"2022-08-11T12:49:21.074466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.077405Z","iopub.execute_input":"2022-08-11T12:49:21.078086Z","iopub.status.idle":"2022-08-11T12:49:21.091441Z","shell.execute_reply.started":"2022-08-11T12:49:21.078049Z","shell.execute_reply":"2022-08-11T12:49:21.090102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Removing the Unnecessary Columns","metadata":{}},{"cell_type":"code","source":"X_train.drop(['PassengerId', 'Name', 'Ticket'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.093172Z","iopub.execute_input":"2022-08-11T12:49:21.094125Z","iopub.status.idle":"2022-08-11T12:49:21.105643Z","shell.execute_reply.started":"2022-08-11T12:49:21.094079Z","shell.execute_reply":"2022-08-11T12:49:21.104241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.drop(['PassengerId', 'Name', 'Ticket'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.107474Z","iopub.execute_input":"2022-08-11T12:49:21.107913Z","iopub.status.idle":"2022-08-11T12:49:21.120088Z","shell.execute_reply.started":"2022-08-11T12:49:21.107870Z","shell.execute_reply":"2022-08-11T12:49:21.119223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.121649Z","iopub.execute_input":"2022-08-11T12:49:21.122716Z","iopub.status.idle":"2022-08-11T12:49:21.137175Z","shell.execute_reply.started":"2022-08-11T12:49:21.122669Z","shell.execute_reply":"2022-08-11T12:49:21.135987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.columns","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.139914Z","iopub.execute_input":"2022-08-11T12:49:21.140688Z","iopub.status.idle":"2022-08-11T12:49:21.153243Z","shell.execute_reply.started":"2022-08-11T12:49:21.140639Z","shell.execute_reply":"2022-08-11T12:49:21.152053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis","metadata":{}},{"cell_type":"code","source":"for i in X_train.columns:\n    print(X_train[i].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.154575Z","iopub.execute_input":"2022-08-11T12:49:21.155556Z","iopub.status.idle":"2022-08-11T12:49:21.182093Z","shell.execute_reply.started":"2022-08-11T12:49:21.155522Z","shell.execute_reply":"2022-08-11T12:49:21.180838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in X_test.columns:\n    print(X_test[i].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.183783Z","iopub.execute_input":"2022-08-11T12:49:21.184513Z","iopub.status.idle":"2022-08-11T12:49:21.202677Z","shell.execute_reply.started":"2022-08-11T12:49:21.184467Z","shell.execute_reply":"2022-08-11T12:49:21.201622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.206213Z","iopub.execute_input":"2022-08-11T12:49:21.207264Z","iopub.status.idle":"2022-08-11T12:49:21.244466Z","shell.execute_reply.started":"2022-08-11T12:49:21.207213Z","shell.execute_reply":"2022-08-11T12:49:21.243206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.252053Z","iopub.execute_input":"2022-08-11T12:49:21.252732Z","iopub.status.idle":"2022-08-11T12:49:21.284439Z","shell.execute_reply.started":"2022-08-11T12:49:21.252684Z","shell.execute_reply":"2022-08-11T12:49:21.283123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Separating the Dependent Variables from X_train and X_test","metadata":{}},{"cell_type":"code","source":"y_train = X_train['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.286207Z","iopub.execute_input":"2022-08-11T12:49:21.286627Z","iopub.status.idle":"2022-08-11T12:49:21.292117Z","shell.execute_reply.started":"2022-08-11T12:49:21.286585Z","shell.execute_reply":"2022-08-11T12:49:21.290905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.drop(['Survived'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.293522Z","iopub.execute_input":"2022-08-11T12:49:21.294380Z","iopub.status.idle":"2022-08-11T12:49:21.306741Z","shell.execute_reply.started":"2022-08-11T12:49:21.294332Z","shell.execute_reply":"2022-08-11T12:49:21.305610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = X_test['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.308319Z","iopub.execute_input":"2022-08-11T12:49:21.308927Z","iopub.status.idle":"2022-08-11T12:49:21.320703Z","shell.execute_reply.started":"2022-08-11T12:49:21.308885Z","shell.execute_reply":"2022-08-11T12:49:21.319624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.drop(['Survived'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.322170Z","iopub.execute_input":"2022-08-11T12:49:21.322879Z","iopub.status.idle":"2022-08-11T12:49:21.334451Z","shell.execute_reply.started":"2022-08-11T12:49:21.322836Z","shell.execute_reply":"2022-08-11T12:49:21.333493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding Categorical Variables","metadata":{}},{"cell_type":"code","source":"X_train = pd.get_dummies(X_train, drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.335723Z","iopub.execute_input":"2022-08-11T12:49:21.336253Z","iopub.status.idle":"2022-08-11T12:49:21.353533Z","shell.execute_reply.started":"2022-08-11T12:49:21.336211Z","shell.execute_reply":"2022-08-11T12:49:21.352333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.354838Z","iopub.execute_input":"2022-08-11T12:49:21.355615Z","iopub.status.idle":"2022-08-11T12:49:21.366958Z","shell.execute_reply.started":"2022-08-11T12:49:21.355579Z","shell.execute_reply":"2022-08-11T12:49:21.365781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = pd.get_dummies(X_test, drop_first=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.368438Z","iopub.execute_input":"2022-08-11T12:49:21.368982Z","iopub.status.idle":"2022-08-11T12:49:21.387272Z","shell.execute_reply.started":"2022-08-11T12:49:21.368951Z","shell.execute_reply":"2022-08-11T12:49:21.385955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.389151Z","iopub.execute_input":"2022-08-11T12:49:21.389549Z","iopub.status.idle":"2022-08-11T12:49:21.403450Z","shell.execute_reply.started":"2022-08-11T12:49:21.389514Z","shell.execute_reply":"2022-08-11T12:49:21.402226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Scaling","metadata":{}},{"cell_type":"code","source":"#final_X_train = X_train.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.405255Z","iopub.execute_input":"2022-08-11T12:49:21.406098Z","iopub.status.idle":"2022-08-11T12:49:21.416464Z","shell.execute_reply.started":"2022-08-11T12:49:21.406050Z","shell.execute_reply":"2022-08-11T12:49:21.415269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#final_X_test = X_test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.417981Z","iopub.execute_input":"2022-08-11T12:49:21.418395Z","iopub.status.idle":"2022-08-11T12:49:21.430600Z","shell.execute_reply.started":"2022-08-11T12:49:21.418353Z","shell.execute_reply":"2022-08-11T12:49:21.429462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nmysc = StandardScaler()\nX_train = mysc.fit_transform(X_train)\nX_test = mysc.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.431904Z","iopub.execute_input":"2022-08-11T12:49:21.432883Z","iopub.status.idle":"2022-08-11T12:49:21.490584Z","shell.execute_reply.started":"2022-08-11T12:49:21.432791Z","shell.execute_reply":"2022-08-11T12:49:21.489178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from sklearn.preprocessing import StandardScaler\n#mysc = StandardScaler()\n#X_res[:,5:] = mysc.fit_transform(X_res[:,5:])\n#X_test[:,5:] = mysc.transform(X_test[:,5:])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.492713Z","iopub.execute_input":"2022-08-11T12:49:21.493546Z","iopub.status.idle":"2022-08-11T12:49:21.498686Z","shell.execute_reply.started":"2022-08-11T12:49:21.493498Z","shell.execute_reply":"2022-08-11T12:49:21.497356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing the Machine Learning Algorithms","metadata":{}},{"cell_type":"code","source":"from xgboost import XGBClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.svm import SVC\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:21.500891Z","iopub.execute_input":"2022-08-11T12:49:21.501803Z","iopub.status.idle":"2022-08-11T12:49:22.029112Z","shell.execute_reply.started":"2022-08-11T12:49:21.501755Z","shell.execute_reply":"2022-08-11T12:49:22.028059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training XGBoost Algo on Training set","metadata":{}},{"cell_type":"code","source":"#classifier = XGBClassifier()\n#classifier.fit(X_train, y_train)\n\n# [[211  55]\n# [ 33 119]]\n#              precision    recall  f1-score   support\n#\n#           0       0.86      0.79      0.83       266\n#           1       0.68      0.78      0.73       152\n#\n#    accuracy                           0.79       418\n#   macro avg       0.77      0.79      0.78       418\n# weighted avg       0.80      0.79      0.79       418\n\n# 0.7894736842105263\n\n# Accuracy: 82.38 %\n# Standard Deviation: 3.67 %\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.030781Z","iopub.execute_input":"2022-08-11T12:49:22.031534Z","iopub.status.idle":"2022-08-11T12:49:22.036589Z","shell.execute_reply.started":"2022-08-11T12:49:22.031488Z","shell.execute_reply":"2022-08-11T12:49:22.035386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Logistic Regression Algo on Training set","metadata":{}},{"cell_type":"code","source":"#classifier = LogisticRegression(random_state = 0)\n#classifier.fit(X_train, y_train)\n\n#  [[251  15]\n#   [  9 143]]\n#               precision    recall  f1-score   support\n\n#            0       0.97      0.94      0.95       266\n#            1       0.91      0.94      0.92       152\n\n#     accuracy                           0.94       418\n#    macro avg       0.94      0.94      0.94       418\n#  weighted avg       0.94      0.94      0.94       418\n\n# 0.9425837320574163\n\n#  Accuracy: 79.24 %\n#  Standard Deviation: 2.15 %\n    \n#  Best Accuracy: 79.35 %\n#  Best Parameters: {'penalty': 'none'}\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.038311Z","iopub.execute_input":"2022-08-11T12:49:22.038995Z","iopub.status.idle":"2022-08-11T12:49:22.050105Z","shell.execute_reply.started":"2022-08-11T12:49:22.038953Z","shell.execute_reply":"2022-08-11T12:49:22.049170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training KNN Algo on Training set","metadata":{}},{"cell_type":"code","source":"#classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)\n#classifier.fit(X_train, y_train)\n\n# [[213  53]\n#  [ 25 127]]\n#               precision    recall  f1-score   support\n\n#            0       0.89      0.80      0.85       266\n#            1       0.71      0.84      0.77       152\n\n#     accuracy                           0.81       418\n#    macro avg       0.80      0.82      0.81       418\n# weighted avg       0.83      0.81      0.82       418\n\n# 0.8133971291866029\n\n# Accuracy: 80.92 %\n# Standard Deviation: 3.47 %\n    \n\n# Best Accuracy: 81.94 %\n# Best Parameters: {'algorithm': 'auto', 'metric': 'minkowski', 'n_neighbors': 15, 'p': 1, 'weights': 'uniform'}","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.051728Z","iopub.execute_input":"2022-08-11T12:49:22.052438Z","iopub.status.idle":"2022-08-11T12:49:22.062625Z","shell.execute_reply.started":"2022-08-11T12:49:22.052393Z","shell.execute_reply":"2022-08-11T12:49:22.061430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Decision Trees Algo on Training set¶","metadata":{}},{"cell_type":"code","source":"#classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)\n#classifier.fit(X_train, y_train)\n\n# [[206  60]\n#  [ 38 114]]\n#               precision    recall  f1-score   support\n\n#            0       0.84      0.77      0.81       266\n#            1       0.66      0.75      0.70       152\n\n#     accuracy                           0.77       418\n#    macro avg       0.75      0.76      0.75       418\n# weighted avg       0.78      0.77      0.77       418\n\n# 0.7655502392344498\n\n# Accuracy: 77.56 %\n# Standard Deviation: 4.70 %\n    \n# Best Accuracy: 78.79 %\n# Best Parameters: {'criterion': 'gini'}","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.063922Z","iopub.execute_input":"2022-08-11T12:49:22.064472Z","iopub.status.idle":"2022-08-11T12:49:22.080645Z","shell.execute_reply.started":"2022-08-11T12:49:22.064439Z","shell.execute_reply":"2022-08-11T12:49:22.079278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Naive Bayes Algo on Training set¶","metadata":{}},{"cell_type":"code","source":"#classifier = GaussianNB()\n#classifier.fit(X_train, y_train)\n\n# [[238  28]\n#  [  6 146]]\n#               precision    recall  f1-score   support\n\n#            0       0.98      0.89      0.93       266\n#            1       0.84      0.96      0.90       152\n\n#     accuracy                           0.92       418\n#    macro avg       0.91      0.93      0.91       418\n# weighted avg       0.93      0.92      0.92       418\n\n# 0.9186602870813397\n\n# Accuracy: 78.12 %\n# Standard Deviation: 1.81 %","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.082000Z","iopub.execute_input":"2022-08-11T12:49:22.082579Z","iopub.status.idle":"2022-08-11T12:49:22.092546Z","shell.execute_reply.started":"2022-08-11T12:49:22.082546Z","shell.execute_reply":"2022-08-11T12:49:22.091536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training SVM Algo on Training set","metadata":{}},{"cell_type":"code","source":"classifier = SVC(kernel = 'rbf', random_state = 0)\nclassifier.fit(X_train, y_train)\n\n# [[256  10]\n#  [ 33 119]]\n#               precision    recall  f1-score   support\n\n#            0       0.89      0.96      0.92       266\n#            1       0.92      0.78      0.85       152\n\n#     accuracy                           0.90       418\n#    macro avg       0.90      0.87      0.88       418\n# weighted avg       0.90      0.90      0.90       418\n\n# 0.8971291866028708\n\n# Accuracy: 82.49 %\n# Standard Deviation: 3.89 %\n    \n# Best Accuracy: 83.17 %\n# Best Parameters: {'gamma': 0.2, 'kernel': 'rbf'}","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.094170Z","iopub.execute_input":"2022-08-11T12:49:22.095346Z","iopub.status.idle":"2022-08-11T12:49:22.143676Z","shell.execute_reply.started":"2022-08-11T12:49:22.095300Z","shell.execute_reply":"2022-08-11T12:49:22.142404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Random Forest Algo on Training set¶","metadata":{}},{"cell_type":"code","source":"#classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)\n#classifier.fit(X_train, y_train)\n\n# [[233  33]\n#  [ 39 113]]\n#               precision    recall  f1-score   support\n\n#            0       0.86      0.88      0.87       266\n#            1       0.77      0.74      0.76       152\n\n#     accuracy                           0.83       418\n#    macro avg       0.82      0.81      0.81       418\n# weighted avg       0.83      0.83      0.83       418\n\n# 0.8277511961722488\n\n# Accuracy: 80.48 %\n# Standard Deviation: 3.97 %\n    \n# Best Accuracy: 80.93 %\n# Best Parameters: {'criterion': 'entropy', 'n_estimators': 90}","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.145068Z","iopub.execute_input":"2022-08-11T12:49:22.146066Z","iopub.status.idle":"2022-08-11T12:49:22.151020Z","shell.execute_reply.started":"2022-08-11T12:49:22.146021Z","shell.execute_reply":"2022-08-11T12:49:22.150139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix - Accuracy Score","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score, classification_report, plot_confusion_matrix, precision_score, recall_score\ny_pred = classifier.predict(X_test)\ncm = confusion_matrix(y_test, y_pred)\ncr = classification_report(y_test, y_pred)\nprint(cm)\nprint(cr)\naccuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.152585Z","iopub.execute_input":"2022-08-11T12:49:22.153302Z","iopub.status.idle":"2022-08-11T12:49:22.189522Z","shell.execute_reply.started":"2022-08-11T12:49:22.153258Z","shell.execute_reply":"2022-08-11T12:49:22.188506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(*y_pred, sep = '\\n')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.191009Z","iopub.execute_input":"2022-08-11T12:49:22.191657Z","iopub.status.idle":"2022-08-11T12:49:22.201474Z","shell.execute_reply.started":"2022-08-11T12:49:22.191614Z","shell.execute_reply":"2022-08-11T12:49:22.200422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# K-Fold Cross Validation","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_score\naccuracies = cross_val_score(estimator = classifier, X = X_train, y = y_train, cv = 10)\nprint(\"Accuracy: {:.2f} %\".format(accuracies.mean()*100))\nprint(\"Standard Deviation: {:.2f} %\".format(accuracies.std()*100))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.203109Z","iopub.execute_input":"2022-08-11T12:49:22.203926Z","iopub.status.idle":"2022-08-11T12:49:22.490643Z","shell.execute_reply.started":"2022-08-11T12:49:22.203880Z","shell.execute_reply":"2022-08-11T12:49:22.489451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Grid Search","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nparameters = [{'kernel': ['rbf', 'linear','sigmoid','callable'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]\ngrid_search = GridSearchCV(estimator = classifier,\n                           param_grid = parameters,\n                           scoring = 'accuracy',\n                           cv = 10,\n                           n_jobs = -1)\ngrid_search.fit(X_train, y_train)\nbest_accuracy = grid_search.best_score_\nbest_parameters = grid_search.best_params_\nprint(\"Best Accuracy: {:.2f} %\".format(best_accuracy*100))\nprint(\"Best Parameters:\", best_parameters)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T12:49:22.492152Z","iopub.execute_input":"2022-08-11T12:49:22.493152Z","iopub.status.idle":"2022-08-11T12:49:28.131624Z","shell.execute_reply.started":"2022-08-11T12:49:22.493106Z","shell.execute_reply":"2022-08-11T12:49:28.128678Z"},"trusted":true},"execution_count":null,"outputs":[]}]}