{"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":"\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">Load Data</p>  ","metadata":{"papermill":{"duration":0.01428,"end_time":"2022-06-28T18:28:34.529850","exception":false,"start_time":"2022-06-28T18:28:34.515570","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\ndf = pd.read_csv(\"../input/titanic/train.csv\")","metadata":{"papermill":{"duration":1.145941,"end_time":"2022-06-28T18:28:35.690099","exception":false,"start_time":"2022-06-28T18:28:34.544158","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.573897Z","iopub.execute_input":"2022-07-27T12:59:37.574767Z","iopub.status.idle":"2022-07-27T12:59:37.586907Z","shell.execute_reply.started":"2022-07-27T12:59:37.574728Z","shell.execute_reply":"2022-07-27T12:59:37.585934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"papermill":{"duration":0.046479,"end_time":"2022-06-28T18:28:35.751761","exception":false,"start_time":"2022-06-28T18:28:35.705282","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.588539Z","iopub.execute_input":"2022-07-27T12:59:37.589179Z","iopub.status.idle":"2022-07-27T12:59:37.605154Z","shell.execute_reply.started":"2022-07-27T12:59:37.589146Z","shell.execute_reply":"2022-07-27T12:59:37.604442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">EDA</p>  ","metadata":{"papermill":{"duration":0.014386,"end_time":"2022-06-28T18:28:35.780933","exception":false,"start_time":"2022-06-28T18:28:35.766547","status":"completed"},"tags":[]}},{"cell_type":"code","source":"df.info()","metadata":{"papermill":{"duration":0.043973,"end_time":"2022-06-28T18:28:35.839746","exception":false,"start_time":"2022-06-28T18:28:35.795773","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.606349Z","iopub.execute_input":"2022-07-27T12:59:37.606827Z","iopub.status.idle":"2022-07-27T12:59:37.626538Z","shell.execute_reply.started":"2022-07-27T12:59:37.606799Z","shell.execute_reply":"2022-07-27T12:59:37.625672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"papermill":{"duration":0.054599,"end_time":"2022-06-28T18:28:35.910401","exception":false,"start_time":"2022-06-28T18:28:35.855802","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.628734Z","iopub.execute_input":"2022-07-27T12:59:37.629029Z","iopub.status.idle":"2022-07-27T12:59:37.666682Z","shell.execute_reply.started":"2022-07-27T12:59:37.629001Z","shell.execute_reply":"2022-07-27T12:59:37.665589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"papermill":{"duration":0.030334,"end_time":"2022-06-28T18:28:35.955889","exception":false,"start_time":"2022-06-28T18:28:35.925555","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.668607Z","iopub.execute_input":"2022-07-27T12:59:37.669411Z","iopub.status.idle":"2022-07-27T12:59:37.681402Z","shell.execute_reply.started":"2022-07-27T12:59:37.669369Z","shell.execute_reply":"2022-07-27T12:59:37.680173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.nunique()","metadata":{"papermill":{"duration":0.035254,"end_time":"2022-06-28T18:28:36.007318","exception":false,"start_time":"2022-06-28T18:28:35.972064","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.682613Z","iopub.execute_input":"2022-07-27T12:59:37.683073Z","iopub.status.idle":"2022-07-27T12:59:37.692313Z","shell.execute_reply.started":"2022-07-27T12:59:37.683043Z","shell.execute_reply":"2022-07-27T12:59:37.691242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Survived\"].value_counts()","metadata":{"papermill":{"duration":0.02955,"end_time":"2022-06-28T18:28:36.053153","exception":false,"start_time":"2022-06-28T18:28:36.023603","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.693717Z","iopub.execute_input":"2022-07-27T12:59:37.694150Z","iopub.status.idle":"2022-07-27T12:59:37.701433Z","shell.execute_reply.started":"2022-07-27T12:59:37.694122Z","shell.execute_reply":"2022-07-27T12:59:37.700550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\ndf[\"Survived\"].value_counts().plot.pie(explode=[0.05,0.05], autopct='%1.1f%%', \n                                          labels=[\"Not Survived\",\"Survived\"],\n                                          shadow=True).set_title(\"Target Distribution\")","metadata":{"papermill":{"duration":0.223447,"end_time":"2022-06-28T18:28:36.292353","exception":false,"start_time":"2022-06-28T18:28:36.068906","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.702596Z","iopub.execute_input":"2022-07-27T12:59:37.702904Z","iopub.status.idle":"2022-07-27T12:59:37.831298Z","shell.execute_reply.started":"2022-07-27T12:59:37.702875Z","shell.execute_reply":"2022-07-27T12:59:37.830162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.duplicated().sum()","metadata":{"papermill":{"duration":0.04223,"end_time":"2022-06-28T18:28:36.364581","exception":false,"start_time":"2022-06-28T18:28:36.322351","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.833137Z","iopub.execute_input":"2022-07-27T12:59:37.833837Z","iopub.status.idle":"2022-07-27T12:59:37.846791Z","shell.execute_reply.started":"2022-07-27T12:59:37.833785Z","shell.execute_reply":"2022-07-27T12:59:37.845833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[[\"Age\"]].boxplot()","metadata":{"papermill":{"duration":0.197165,"end_time":"2022-06-28T18:28:36.578389","exception":false,"start_time":"2022-06-28T18:28:36.381224","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:37.851604Z","iopub.execute_input":"2022-07-27T12:59:37.852489Z","iopub.status.idle":"2022-07-27T12:59:37.996663Z","shell.execute_reply.started":"2022-07-27T12:59:37.852440Z","shell.execute_reply":"2022-07-27T12:59:37.995572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[[\"Fare\"]].boxplot()","metadata":{"papermill":{"duration":0.180796,"end_time":"2022-06-28T18:28:36.776329","exception":false,"start_time":"2022-06-28T18:28:36.595533","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.000260Z","iopub.execute_input":"2022-07-27T12:59:38.001104Z","iopub.status.idle":"2022-07-27T12:59:38.161656Z","shell.execute_reply.started":"2022-07-27T12:59:38.001058Z","shell.execute_reply":"2022-07-27T12:59:38.160355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10,10)) # to resize the heatmap\nsns.heatmap(df.drop(\"Survived\",axis=1).corr(), annot=True, ax=ax)","metadata":{"papermill":{"duration":0.429535,"end_time":"2022-06-28T18:28:37.223487","exception":false,"start_time":"2022-06-28T18:28:36.793952","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.163182Z","iopub.execute_input":"2022-07-27T12:59:38.163694Z","iopub.status.idle":"2022-07-27T12:59:38.556316Z","shell.execute_reply.started":"2022-07-27T12:59:38.163663Z","shell.execute_reply":"2022-07-27T12:59:38.555232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"EDA observations:\n\n1. Dataset contains variables like \"Ticket\" and \"Names\", these can be removed as they appear to be random unique identifies.\n2. \"PassengerId\" is here used only for indexing, so it can be also removed.\n2. Target class \"Survived\" has binary output with slighlt imbalance data.\n3. \"Age\" and \"Cabin\" have missing values. The values in \"Age\" can be filled but in \"Cabin\" as most the values are missing, it will be removed.\n4. With the help of boxplot, we can identify that variables \"Age\" and \"Fare\" contains outliers.\n5. Lastly, no significant correlation is seen between independent variables.","metadata":{"papermill":{"duration":0.017064,"end_time":"2022-06-28T18:28:37.258434","exception":false,"start_time":"2022-06-28T18:28:37.241370","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">Test-Train Split\n   </p>  ","metadata":{"papermill":{"duration":0.017292,"end_time":"2022-06-28T18:28:37.293139","exception":false,"start_time":"2022-06-28T18:28:37.275847","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(df.drop(\"Survived\",axis=1), \n                                                   df[\"Survived\"],\n                                                   test_size=0.3,\n                                                   random_state=100)","metadata":{"papermill":{"duration":0.235007,"end_time":"2022-06-28T18:28:37.545826","exception":false,"start_time":"2022-06-28T18:28:37.310819","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.557803Z","iopub.execute_input":"2022-07-27T12:59:38.558133Z","iopub.status.idle":"2022-07-27T12:59:38.566482Z","shell.execute_reply.started":"2022-07-27T12:59:38.558102Z","shell.execute_reply":"2022-07-27T12:59:38.565757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"papermill":{"duration":0.046683,"end_time":"2022-06-28T18:28:37.610295","exception":false,"start_time":"2022-06-28T18:28:37.563612","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.567474Z","iopub.execute_input":"2022-07-27T12:59:38.568245Z","iopub.status.idle":"2022-07-27T12:59:38.597514Z","shell.execute_reply.started":"2022-07-27T12:59:38.568178Z","shell.execute_reply":"2022-07-27T12:59:38.596293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2022-07-27T12:59:38.599093Z","iopub.execute_input":"2022-07-27T12:59:38.599721Z","iopub.status.idle":"2022-07-27T12:59:38.622861Z","shell.execute_reply.started":"2022-07-27T12:59:38.599678Z","shell.execute_reply":"2022-07-27T12:59:38.621968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">FEATURE ENGINEERING</p>  ","metadata":{}},{"cell_type":"markdown","source":"\n\n1. [Removing variables](#removing)\n2. [Dealing with missing values](#missing)\n3. [One hot encoding variables](#onehot)\n4. [Scaling variables](#scaling)","metadata":{"papermill":{"duration":0.018382,"end_time":"2022-06-28T18:28:37.647635","exception":false,"start_time":"2022-06-28T18:28:37.629253","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id=\"removing\"></a>\n## 1. Removing Variables","metadata":{"papermill":{"duration":0.0183,"end_time":"2022-06-28T18:28:37.684500","exception":false,"start_time":"2022-06-28T18:28:37.666200","status":"completed"},"tags":[]}},{"cell_type":"code","source":"drop_list = [\"PassengerId\", \"Name\", \"Cabin\", \"Ticket\"]\nX_train = X_train.drop(drop_list,axis=1)\nX_test = X_test.drop(drop_list,axis=1)","metadata":{"papermill":{"duration":0.028763,"end_time":"2022-06-28T18:28:37.732285","exception":false,"start_time":"2022-06-28T18:28:37.703522","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.624087Z","iopub.execute_input":"2022-07-27T12:59:38.624908Z","iopub.status.idle":"2022-07-27T12:59:38.635527Z","shell.execute_reply.started":"2022-07-27T12:59:38.624874Z","shell.execute_reply":"2022-07-27T12:59:38.634636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='missing'></a>\n## 2. Dealing with missing values","metadata":{"papermill":{"duration":0.018785,"end_time":"2022-06-28T18:28:37.769991","exception":false,"start_time":"2022-06-28T18:28:37.751206","status":"completed"},"tags":[]}},{"cell_type":"code","source":"X_train[\"Age\"].fillna(X_train[\"Age\"].median(), inplace=True)\nX_train[\"Embarked\"].fillna(X_train[\"Embarked\"].mode(), inplace=True)\n\nX_test[\"Age\"].fillna(X_test[\"Age\"].median(), inplace=True)\nX_test[\"Embarked\"].fillna(X_test[\"Embarked\"].mode(), inplace=True)","metadata":{"papermill":{"duration":0.032436,"end_time":"2022-06-28T18:28:37.821207","exception":false,"start_time":"2022-06-28T18:28:37.788771","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.636927Z","iopub.execute_input":"2022-07-27T12:59:38.637312Z","iopub.status.idle":"2022-07-27T12:59:38.650385Z","shell.execute_reply.started":"2022-07-27T12:59:38.637284Z","shell.execute_reply":"2022-07-27T12:59:38.649441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"onehot\"></a>\n## 3.One hot encoding variables","metadata":{"papermill":{"duration":0.018826,"end_time":"2022-06-28T18:28:37.858520","exception":false,"start_time":"2022-06-28T18:28:37.839694","status":"completed"},"tags":[]}},{"cell_type":"code","source":"X_train = pd.get_dummies(X_train)\nX_test = pd.get_dummies(X_test)","metadata":{"papermill":{"duration":0.038813,"end_time":"2022-06-28T18:28:37.915777","exception":false,"start_time":"2022-06-28T18:28:37.876964","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.651956Z","iopub.execute_input":"2022-07-27T12:59:38.652420Z","iopub.status.idle":"2022-07-27T12:59:38.668020Z","shell.execute_reply.started":"2022-07-27T12:59:38.652390Z","shell.execute_reply":"2022-07-27T12:59:38.666955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"papermill":{"duration":0.037313,"end_time":"2022-06-28T18:28:37.970910","exception":false,"start_time":"2022-06-28T18:28:37.933597","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.673336Z","iopub.execute_input":"2022-07-27T12:59:38.674180Z","iopub.status.idle":"2022-07-27T12:59:38.689164Z","shell.execute_reply.started":"2022-07-27T12:59:38.674125Z","shell.execute_reply":"2022-07-27T12:59:38.688259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"scaling\"></a>\n## 4.Scaling Variables","metadata":{"papermill":{"duration":0.01784,"end_time":"2022-06-28T18:28:38.007067","exception":false,"start_time":"2022-06-28T18:28:37.989227","status":"completed"},"tags":[]}},{"cell_type":"code","source":"columns = X_train.columns","metadata":{"papermill":{"duration":0.028254,"end_time":"2022-06-28T18:28:38.053980","exception":false,"start_time":"2022-06-28T18:28:38.025726","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.690243Z","iopub.execute_input":"2022-07-27T12:59:38.691102Z","iopub.status.idle":"2022-07-27T12:59:38.696508Z","shell.execute_reply.started":"2022-07-27T12:59:38.691057Z","shell.execute_reply":"2022-07-27T12:59:38.695439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler\n\nx_train = X_train.values\nx_test = X_test.values\n\nmin_max_scaler = MinMaxScaler()\n\nx_train_scaled = min_max_scaler.fit_transform(x_train)\nx_test_scaled = min_max_scaler.transform(x_test)\n\nX_train = pd.DataFrame(x_train_scaled)\nX_test = pd.DataFrame(x_test_scaled)\nX_train.head()","metadata":{"papermill":{"duration":0.048943,"end_time":"2022-06-28T18:28:38.121314","exception":false,"start_time":"2022-06-28T18:28:38.072371","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.697627Z","iopub.execute_input":"2022-07-27T12:59:38.697891Z","iopub.status.idle":"2022-07-27T12:59:38.722930Z","shell.execute_reply.started":"2022-07-27T12:59:38.697865Z","shell.execute_reply":"2022-07-27T12:59:38.722248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.columns = columns\nX_test.columns = columns","metadata":{"papermill":{"duration":0.027455,"end_time":"2022-06-28T18:28:38.167392","exception":false,"start_time":"2022-06-28T18:28:38.139937","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.723968Z","iopub.execute_input":"2022-07-27T12:59:38.724631Z","iopub.status.idle":"2022-07-27T12:59:38.728550Z","shell.execute_reply.started":"2022-07-27T12:59:38.724598Z","shell.execute_reply":"2022-07-27T12:59:38.727663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.head()","metadata":{"papermill":{"duration":0.042432,"end_time":"2022-06-28T18:28:38.228826","exception":false,"start_time":"2022-06-28T18:28:38.186394","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.729919Z","iopub.execute_input":"2022-07-27T12:59:38.730423Z","iopub.status.idle":"2022-07-27T12:59:38.755277Z","shell.execute_reply.started":"2022-07-27T12:59:38.730393Z","shell.execute_reply":"2022-07-27T12:59:38.754252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">MODEL TRAINING</p>  ","metadata":{"papermill":{"duration":0.018346,"end_time":"2022-06-28T18:28:38.265881","exception":false,"start_time":"2022-06-28T18:28:38.247535","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB,BernoulliNB,MultinomialNB\nfrom sklearn.metrics import accuracy_score,f1_score\n\ngnb = GaussianNB()\nbnb = BernoulliNB()\nmnb = MultinomialNB()\n\ny_pred_gnb_train = gnb.fit(X_train, y_train).predict(X_train)\nprint(\"Gaussian Naive Bayes Accuracy on training set: {}%\".format(round(accuracy_score(y_train, y_pred_gnb_train)*100,2)))\nprint(\"Gaussian Naive Bayes F1-score on training set: {} \\n\".format(round(f1_score(y_train, y_pred_gnb_train),2)))\n\ny_pred_bnb_train = bnb.fit(X_train, y_train).predict(X_train)\nprint(\"Bernoulli Naive Bayes Accuracy on training set: {}%\".format(round(accuracy_score(y_train, y_pred_bnb_train)*100,2)))\nprint(\"Bernoulli Naive Bayes F1-score on training set: {} \\n\".format(round(f1_score(y_train, y_pred_bnb_train),2)))\n\ny_pred_mnb_train = mnb.fit(X_train, y_train).predict(X_train)\nprint(\"Multinomial Naive Bayes Accuracy on training set: {}%\".format(round(accuracy_score(y_train, y_pred_mnb_train)*100,2)))\nprint(\"Multinomial Naive Bayes F1-score on training set: {}\".format(round(f1_score(y_train, y_pred_mnb_train),2)))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T12:59:38.756681Z","iopub.execute_input":"2022-07-27T12:59:38.757797Z","iopub.status.idle":"2022-07-27T12:59:38.789139Z","shell.execute_reply.started":"2022-07-27T12:59:38.757756Z","shell.execute_reply":"2022-07-27T12:59:38.788302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All Naive Bayes gives almost same result on training set. Let's check the results on the test dataset.","metadata":{}},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">METRICS</p>  ","metadata":{"papermill":{"duration":0.020195,"end_time":"2022-06-28T18:29:25.527195","exception":false,"start_time":"2022-06-28T18:29:25.507000","status":"completed"},"tags":[]}},{"cell_type":"code","source":"y_pred_gnb_test = gnb.fit(X_test, y_test).predict(X_test)\ny_pred_bnb_test = bnb.fit(X_test, y_test).predict(X_test)\ny_pred_mnb_test = mnb.fit(X_test, y_test).predict(X_test)","metadata":{"papermill":{"duration":0.035135,"end_time":"2022-06-28T18:29:25.622298","exception":false,"start_time":"2022-06-28T18:29:25.587163","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.790429Z","iopub.execute_input":"2022-07-27T12:59:38.790964Z","iopub.status.idle":"2022-07-27T12:59:38.807224Z","shell.execute_reply.started":"2022-07-27T12:59:38.790928Z","shell.execute_reply":"2022-07-27T12:59:38.806160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Accuracy","metadata":{"papermill":{"duration":0.019959,"end_time":"2022-06-28T18:29:25.567313","exception":false,"start_time":"2022-06-28T18:29:25.547354","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(f\"Accuracy Score(%) on test set for:\\n\")\nprint(\"Gaussian Naive Bayes: {} %\".format(round(accuracy_score(y_test, y_pred_gnb_test)*100,2)))\nprint(\"Bernoulli Naive Bayes: {} %\".format(round(accuracy_score(y_test, y_pred_bnb_test)*100,2)))\nprint(\"Multinomial Naive Bayes: {} % \".format(round(accuracy_score(y_test, y_pred_mnb_test)*100,2)))","metadata":{"execution":{"iopub.status.busy":"2022-07-27T12:59:38.808990Z","iopub.execute_input":"2022-07-27T12:59:38.809732Z","iopub.status.idle":"2022-07-27T12:59:38.818049Z","shell.execute_reply.started":"2022-07-27T12:59:38.809687Z","shell.execute_reply":"2022-07-27T12:59:38.817250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Gaussian Naive Bayes give the highest accuracy.","metadata":{"papermill":{"duration":0.019703,"end_time":"2022-06-28T18:29:25.662215","exception":false,"start_time":"2022-06-28T18:29:25.642512","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## F1 score","metadata":{"papermill":{"duration":0.02061,"end_time":"2022-06-28T18:29:25.704116","exception":false,"start_time":"2022-06-28T18:29:25.683506","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(f\"f1-score on test set for: \\n\")\nprint(\"Gaussian Naive Bayes: {}\".format(round(f1_score(y_test, y_pred_gnb_test),2)))\nprint(\"Bernoulli Naive Bayes: {}\".format(round(f1_score(y_test, y_pred_bnb_test),2)))\nprint(\"Multinomial Naive Bayes: {}\".format(round(f1_score(y_test, y_pred_mnb_test),2)))","metadata":{"papermill":{"duration":0.038683,"end_time":"2022-06-28T18:29:25.763351","exception":false,"start_time":"2022-06-28T18:29:25.724668","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.819396Z","iopub.execute_input":"2022-07-27T12:59:38.819971Z","iopub.status.idle":"2022-07-27T12:59:38.833731Z","shell.execute_reply.started":"2022-07-27T12:59:38.819938Z","shell.execute_reply":"2022-07-27T12:59:38.832851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Gaussian Naive Bayes f1-score is the highest.","metadata":{"papermill":{"duration":0.020157,"end_time":"2022-06-28T18:29:25.805313","exception":false,"start_time":"2022-06-28T18:29:25.785156","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style= \"background-color:#154c79; font-family:fantasy; color:#FFF9ED; font-size:200%; text-align:center; border-radius:10px;\">CONCLUSION</p>  ","metadata":{"papermill":{"duration":0.019565,"end_time":"2022-06-28T18:29:25.844616","exception":false,"start_time":"2022-06-28T18:29:25.825051","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"Model Gaussian Naive Bayes GNB gave highest accuracy (78.73%), plus accuracy on test and train dataset is almost similar so there is no overfitting and F1 score(0.73). So it is the most accurate model.","metadata":{"papermill":{"duration":0.019132,"end_time":"2022-06-28T18:29:25.883772","exception":false,"start_time":"2022-06-28T18:29:25.864640","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id=\"predict\"></a>\n# Predictions test.csv for submitions\n\nUsing (GNB) model to predict the test.csv and submit the prediction.","metadata":{"papermill":{"duration":0.019293,"end_time":"2022-06-28T18:29:25.922423","exception":false,"start_time":"2022-06-28T18:29:25.903130","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test = pd.read_csv('../input/titanic/test.csv')\ntest_data = test.copy()","metadata":{"papermill":{"duration":0.042177,"end_time":"2022-06-28T18:29:25.985393","exception":false,"start_time":"2022-06-28T18:29:25.943216","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.834890Z","iopub.execute_input":"2022-07-27T12:59:38.835460Z","iopub.status.idle":"2022-07-27T12:59:38.845750Z","shell.execute_reply.started":"2022-07-27T12:59:38.835425Z","shell.execute_reply":"2022-07-27T12:59:38.844928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"papermill":{"duration":0.044549,"end_time":"2022-06-28T18:29:26.050198","exception":false,"start_time":"2022-06-28T18:29:26.005649","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.847393Z","iopub.execute_input":"2022-07-27T12:59:38.848061Z","iopub.status.idle":"2022-07-27T12:59:38.865899Z","shell.execute_reply.started":"2022-07-27T12:59:38.848019Z","shell.execute_reply":"2022-07-27T12:59:38.864912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"papermill":{"duration":0.039917,"end_time":"2022-06-28T18:29:26.111039","exception":false,"start_time":"2022-06-28T18:29:26.071122","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.867101Z","iopub.execute_input":"2022-07-27T12:59:38.867721Z","iopub.status.idle":"2022-07-27T12:59:38.881154Z","shell.execute_reply.started":"2022-07-27T12:59:38.867686Z","shell.execute_reply":"2022-07-27T12:59:38.880259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Here it can be seen that with \"Age\", \"Fare\" also has missing value. So, we have to take care of that.\n\nIn this problem, we have access to test.csv but in other problem, we might not have access to test dataset so we have to consider that and build model according to that.","metadata":{"papermill":{"duration":0.022626,"end_time":"2022-06-28T18:29:26.156373","exception":false,"start_time":"2022-06-28T18:29:26.133747","status":"completed"},"tags":[]}},{"cell_type":"code","source":"test = test.drop(drop_list, axis=1)\ntest[\"Age\"].fillna(test[\"Age\"].median(), inplace=True)\ntest[\"Embarked\"].fillna(test[\"Embarked\"].mode(), inplace=True)\ntest[\"Fare\"].fillna(test[\"Fare\"].median(), inplace=True)\ntest = pd.get_dummies(test)\n\ntest_values = test.values\ntest_scaled = min_max_scaler.fit_transform(test_values)\ntest = pd.DataFrame(test_scaled)\ntest.columns = columns\n\ntest.head()","metadata":{"papermill":{"duration":0.064657,"end_time":"2022-06-28T18:29:26.243171","exception":false,"start_time":"2022-06-28T18:29:26.178514","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.882783Z","iopub.execute_input":"2022-07-27T12:59:38.883537Z","iopub.status.idle":"2022-07-27T12:59:38.916669Z","shell.execute_reply.started":"2022-07-27T12:59:38.883481Z","shell.execute_reply":"2022-07-27T12:59:38.915551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_gnb_final = gnb.predict(test)\ny_pred_gnb_final","metadata":{"papermill":{"duration":0.040862,"end_time":"2022-06-28T18:29:26.304895","exception":false,"start_time":"2022-06-28T18:29:26.264033","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.918110Z","iopub.execute_input":"2022-07-27T12:59:38.918623Z","iopub.status.idle":"2022-07-27T12:59:38.928513Z","shell.execute_reply.started":"2022-07-27T12:59:38.918588Z","shell.execute_reply":"2022-07-27T12:59:38.927675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y_pred_gnb_final})\nprint(output)\noutput.to_csv('./submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"papermill":{"duration":0.036776,"end_time":"2022-06-28T18:29:26.362948","exception":false,"start_time":"2022-06-28T18:29:26.326172","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-07-27T12:59:38.929625Z","iopub.execute_input":"2022-07-27T12:59:38.930087Z","iopub.status.idle":"2022-07-27T12:59:38.945442Z","shell.execute_reply.started":"2022-07-27T12:59:38.930057Z","shell.execute_reply":"2022-07-27T12:59:38.944007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Please let me know if any mistakes are there and if you want me to add something.\n\nHappy Learning 😃","metadata":{"papermill":{"duration":0.020051,"end_time":"2022-06-28T18:29:26.403302","exception":false,"start_time":"2022-06-28T18:29:26.383251","status":"completed"},"tags":[]}}]}