{"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":"<div style='font-size:200%;'>\n    <a id='nan'></a>\n    <h1 style='color: darkslateblue; font-weight: bold; font-family: Cascadia code;'>\n        <center> Multiple Model analysis with EDA on Spaceship Titanic Dataset </center>\n    </h1>\n</div>\n\n<center> <img src=\"https://64.media.tumblr.com/debec6caf9c8a009a73207228ffde853/5866f6929b208337-e6/s540x810/aacff5ed83186c696f4c3c6796eccdf2f8f68bd7.gifv\"> </center>","metadata":{}},{"cell_type":"markdown","source":"- - -\n\n<div style='font-size:200%;'>\n    <a id='nan'></a>\n    <h1 style='color: chartreuse; font-weight: bold; font-family: Cascadia code;'> Contents </h1>\n</div>\n\n- [Importing necessary libraries](#import)\n- [Importing the data](#data)\n- [Exploratory Data Analysis](#eda)\n    - [NaN values heat-map](#heatmap)\n    - [Typecasting and dropping columns](#typecast)\n    - [Correlation between different features and our target variable](#corr)\n    - [Distribution of Individuals based on HomePlanet](#planet)\n- [Distribution of transported individuals](#trans)\n- [Age distribution of the passengers](#age)\n- [Auto-Visualization](#auto)\n    - [Training Data](#auto-train)\n    - [Testing Data](#auto-test)\n- [Data Pre-processing](#preprocess)\n    - [Imputing missing data](#impute)\n    - [Typecasting and dropping columns](#typecast)\n    - [One-Hot Encoding](#ohe)\n    - [Splitting data into x (Values) and y (labels)](#split)\n- [Classifying](#classify)\n    - [Building and fitting the models](#build)\n    - [Performance Analysis of the different models](#anal)\n- [Submission](#submit)\n\n- - -","metadata":{}},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='import'></a>\n    <h1 style='color: maroon; font-weight: bold; font-family: Cascadia code;'>\n        <center> Importing necessary libraries 📚 </center>\n    </h1>\n    <img src=\"https://miro.medium.com/max/1400/1*RIrPOCyMFwFC-XULbja3rw.png\">\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport sklearn\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:24:20.303975Z","iopub.execute_input":"2022-08-06T17:24:20.305306Z","iopub.status.idle":"2022-08-06T17:24:22.256235Z","shell.execute_reply.started":"2022-08-06T17:24:20.305247Z","shell.execute_reply":"2022-08-06T17:24:22.255251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='clean'></a>\n    <h1 style='color: orangered; font-weight: bold; font-family: Cascadia code;'>\n        <center> Importing the dataset ⬇️ </center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:30:40.952028Z","iopub.execute_input":"2022-07-18T20:30:40.952409Z","iopub.status.idle":"2022-07-18T20:30:40.959892Z","shell.execute_reply.started":"2022-07-18T20:30:40.952380Z","shell.execute_reply":"2022-07-18T20:30:40.958889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataTrain = pd.read_csv('/kaggle/input/spaceship-titanic/train.csv')\ndataTest = pd.read_csv('/kaggle/input/spaceship-titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:30:43.801094Z","iopub.execute_input":"2022-07-18T20:30:43.801549Z","iopub.status.idle":"2022-07-18T20:30:43.901081Z","shell.execute_reply.started":"2022-07-18T20:30:43.801519Z","shell.execute_reply":"2022-07-18T20:30:43.899931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='eda'></a>\n    <h1 style='color: lightgreen; font-weight: bold; font-family: Cascadia code;'>\n        <center> Exploratory Data Analysis 📊 </center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='heatmap'><b>Null values heat-map<b></a></h1>","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, sharex=True, figsize=(20,10))\nsns.heatmap(ax=axes[0], yticklabels=False, data=dataTrain.isnull(), cbar=False, cmap=\"viridis\")\nsns.heatmap(ax=axes[1], yticklabels=False, data=dataTest.isnull(), cbar=False, cmap=\"tab20c\")\naxes[0].set_title('Heatmap of missing values in training data')\naxes[1].set_title('Heatmap of missing values in testing data')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:30:49.684414Z","iopub.execute_input":"2022-07-18T20:30:49.684842Z","iopub.status.idle":"2022-07-18T20:30:50.361890Z","shell.execute_reply.started":"2022-07-18T20:30:49.684798Z","shell.execute_reply":"2022-07-18T20:30:50.360828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Unique HomePlanet:', dataTrain.HomePlanet.unique(), '\\nUnique Destination:', dataTrain.Destination.unique())","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:30:59.415687Z","iopub.execute_input":"2022-07-18T20:30:59.416106Z","iopub.status.idle":"2022-07-18T20:30:59.432860Z","shell.execute_reply.started":"2022-07-18T20:30:59.416070Z","shell.execute_reply":"2022-07-18T20:30:59.430580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='corr'><b>Correlation between different features and our target variable<b></a></h1>","metadata":{}},{"cell_type":"markdown","source":"## **Correlation coefficient**\n\nThe correlation coefficient is a statistical measure of the strength of the relationship between the relative movements of two variables. The values range between -1.0 and 1.0. A calculated number greater than 1.0 or less than -1.0 means that there was an error in the correlation measurement. A correlation of -1.0 shows a perfect negative correlation, while a correlation of 1.0 shows a perfect positive correlation. A correlation of 0.0 shows no linear relationship between the movement of the two variables.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\ndata = dataTrain.corr()[\"Transported\"].sort_values(ascending=False)\nindices = data.index\nlabels = []\ncorr = []\nfor i in range(1, len(indices)):\n    labels.append(indices[i])\n    corr.append(data[i])\nsns.barplot(x=corr, y=labels, palette='magma')\nplt.title('Correlation coefficient between different features and Transported')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-18T20:31:13.613708Z","iopub.execute_input":"2022-07-18T20:31:13.614079Z","iopub.status.idle":"2022-07-18T20:31:13.822842Z","shell.execute_reply.started":"2022-07-18T20:31:13.614049Z","shell.execute_reply":"2022-07-18T20:31:13.821827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(18, 9))\nsns.heatmap(dataTrain.corr(), cmap=\"YlGnBu\", annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:38:47.976439Z","iopub.execute_input":"2022-07-18T20:38:47.976873Z","iopub.status.idle":"2022-07-18T20:38:48.484347Z","shell.execute_reply.started":"2022-07-18T20:38:47.976842Z","shell.execute_reply":"2022-07-18T20:38:48.483334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='planet'><b>Distribution of Individuals based on HomePlanet<b></a></h1>","metadata":{}},{"cell_type":"code","source":"tPlanet = pd.crosstab(dataTrain['Transported'], dataTrain['HomePlanet'])\ntDest = pd.crosstab(dataTrain['Transported'], dataTrain['Destination'])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:31:18.679992Z","iopub.execute_input":"2022-07-18T20:31:18.680990Z","iopub.status.idle":"2022-07-18T20:31:18.722024Z","shell.execute_reply.started":"2022-07-18T20:31:18.680946Z","shell.execute_reply":"2022-07-18T20:31:18.720947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\ncolors = sns.color_palette('pastel')\nplt.pie([item/len(dataTrain.HomePlanet) for item in dataTrain.HomePlanet.value_counts()], labels=['Earth', 'Europa', 'Mars'], colors=colors, autopct='%.0f%%')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-18T20:31:23.961122Z","iopub.execute_input":"2022-07-18T20:31:23.961455Z","iopub.status.idle":"2022-07-18T20:31:24.069998Z","shell.execute_reply.started":"2022-07-18T20:31:23.961431Z","shell.execute_reply":"2022-07-18T20:31:24.069242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='trans'><b>Distribution of transported individuals<b></a></h1>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15))\nplt.subplot(2,2,1)\nsns.countplot(x = dataTrain.HomePlanet, hue = dataTrain.Transported, palette=\"viridis\")\nplt.title('Transported individuals - Home Planets', fontsize=15)\nplt.xlabel('HomePlanet', fontsize=15)\nplt.ylabel('Number of Individuals', fontsize=15)\n\nplt.subplot(2,2,2)\nsns.countplot(x = dataTrain.HomePlanet, hue = dataTrain.CryoSleep, palette=\"viridis\")\nplt.title('Transported individuals - Cryosleep', fontsize=14)\nplt.xlabel('HomePlanet', fontsize=15)\nplt.ylabel('Number of passengers', fontsize=15)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-07-18T20:31:30.721282Z","iopub.execute_input":"2022-07-18T20:31:30.721730Z","iopub.status.idle":"2022-07-18T20:31:31.064578Z","shell.execute_reply.started":"2022-07-18T20:31:30.721698Z","shell.execute_reply":"2022-07-18T20:31:31.062874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='age'><b>Age distribution of the passengers<b></a></h1>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,8))\nsns.histplot(dataTrain.Age, color=sns.color_palette('magma')[2])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:31:35.438986Z","iopub.execute_input":"2022-07-18T20:31:35.439338Z","iopub.status.idle":"2022-07-18T20:31:35.716371Z","shell.execute_reply.started":"2022-07-18T20:31:35.439312Z","shell.execute_reply":"2022-07-18T20:31:35.714631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainAge = dataTrain.copy()\ntestAge = dataTest.copy()\ntrainAge[\"type\"] = \"Train\"\ntestAge[\"type\"] = \"Test\"\nageDf = pd.concat([trainAge, testAge])\nfig = px.histogram(data_frame = ageDf, \n                   x=\"Age\",\n                   color= \"type\",\n                   color_discrete_sequence =  ['#FFA500','#87CEEB'],\n                   marginal=\"box\",\n                   nbins= 100,\n                   template=\"plotly_white\"\n                )\nfig.update_layout(title = \"Distribution of Age\" , title_x = 0.5)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:04.782810Z","iopub.execute_input":"2022-07-18T20:35:04.783118Z","iopub.status.idle":"2022-07-18T20:35:04.877317Z","shell.execute_reply.started":"2022-07-18T20:35:04.783094Z","shell.execute_reply":"2022-07-18T20:35:04.876694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='auto'></a>\n    <h1 style='color: green; font-weight: bold; font-family: Cascadia code;'>\n        <center> Auto-Visualization 🤖</center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='auto-train'><b>Training Data<b></a></h1>","metadata":{}},{"cell_type":"code","source":"!pip install autoviz","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:22:45.091274Z","iopub.execute_input":"2022-08-06T17:22:45.091672Z","iopub.status.idle":"2022-08-06T17:23:06.071599Z","shell.execute_reply.started":"2022-08-06T17:22:45.091568Z","shell.execute_reply":"2022-08-06T17:23:06.070256Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 5))\nfrom autoviz.AutoViz_Class import AutoViz_Class\nAV = AutoViz_Class()\ndf_av = AV.AutoViz('/kaggle/input/spaceship-titanic/train.csv')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:25:07.688199Z","iopub.execute_input":"2022-08-06T17:25:07.689366Z","iopub.status.idle":"2022-08-06T17:25:24.487416Z","shell.execute_reply.started":"2022-08-06T17:25:07.689307Z","shell.execute_reply":"2022-08-06T17:25:24.485865Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='auto-test'><b>Testing Data<b></a></h1>","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (10, 5))\nfrom autoviz.AutoViz_Class import AutoViz_Class\nAV = AutoViz_Class()\ndf_av = AV.AutoViz('/kaggle/input/spaceship-titanic/test.csv')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T17:26:19.708147Z","iopub.execute_input":"2022-08-06T17:26:19.708577Z","iopub.status.idle":"2022-08-06T17:26:34.447070Z","shell.execute_reply.started":"2022-08-06T17:26:19.708544Z","shell.execute_reply":"2022-08-06T17:26:34.445958Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='preprocess'></a>\n    <h1 style='color: purple; font-weight: bold; font-family: Cascadia code;'>\n        <center> Data Pre-processing ⌛ </center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='impute'><b>Imputing missing data<b></a></h1>","metadata":{}},{"cell_type":"markdown","source":"## **Data imputation**\n\nImputation is a technique used for replacing the missing data with some substitute value to retain most of the data/information of the dataset. These techniques are used because removing the data from the dataset every time is not feasible and can lead to a reduction in the size of the dataset to a large extend, which not only raises concerns for biasing the dataset but also leads to incorrect analysis.","metadata":{}},{"cell_type":"code","source":"idCol = dataTest.PassengerId.to_numpy()\ndataTrain.set_index('PassengerId', inplace=True)\ndataTest.set_index('PassengerId', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:22.493717Z","iopub.execute_input":"2022-07-18T20:35:22.495131Z","iopub.status.idle":"2022-07-18T20:35:22.503101Z","shell.execute_reply.started":"2022-07-18T20:35:22.495085Z","shell.execute_reply":"2022-07-18T20:35:22.501426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.impute import SimpleImputer\nimputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')\ndataTrain = pd.DataFrame(imputer.fit_transform(dataTrain), columns=dataTrain.columns, index=dataTrain.index)\ndataTest = pd.DataFrame(imputer.fit_transform(dataTest), columns=dataTest.columns, index=dataTest.index)\ndataTrain = dataTrain.reset_index(drop=True)\ndataTest = dataTest.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:28.988468Z","iopub.execute_input":"2022-07-18T20:35:28.988912Z","iopub.status.idle":"2022-07-18T20:35:29.314639Z","shell.execute_reply.started":"2022-07-18T20:35:28.988885Z","shell.execute_reply":"2022-07-18T20:35:29.313443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='typecast'><b>Typecasting and dropping columns<b></a></h1>","metadata":{}},{"cell_type":"code","source":"dataTrain.Transported = dataTrain.Transported.astype('int')\ndataTrain.VIP = dataTrain.VIP.astype('int')\ndataTrain.CryoSleep = dataTrain.CryoSleep.astype('int')\ndataTrain.drop(columns=['Cabin', 'Name'], inplace=True)\ndataTest.drop(columns=['Cabin', 'Name'], inplace=True)\ndataTrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:32.710858Z","iopub.execute_input":"2022-07-18T20:35:32.711252Z","iopub.status.idle":"2022-07-18T20:35:32.743913Z","shell.execute_reply.started":"2022-07-18T20:35:32.711221Z","shell.execute_reply":"2022-07-18T20:35:32.743016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='ohe'><b>One-Hot Encoding 🔥<b></a></h1>","metadata":{}},{"cell_type":"markdown","source":"## **One-Hot Encoding**\n\nOne hot encoding is one method of converting data to prepare it for an algorithm and get a better prediction. With one-hot, we convert each categorical value into a new categorical column and assign a binary value of 1 or 0 to those columns. Each integer value is represented as a binary vector. All the values are zero, and the index is marked with a 1.","metadata":{}},{"cell_type":"code","source":"dataTrain = pd.get_dummies(dataTrain, columns=['HomePlanet', 'CryoSleep', 'Destination'])\ndataTest = pd.get_dummies(dataTest, columns=['HomePlanet', 'CryoSleep', 'Destination'])\ndataTrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:36.684671Z","iopub.execute_input":"2022-07-18T20:35:36.684985Z","iopub.status.idle":"2022-07-18T20:35:36.710537Z","shell.execute_reply.started":"2022-07-18T20:35:36.684963Z","shell.execute_reply":"2022-07-18T20:35:36.709597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='split'><b>Splitting data into x (Values) and y (labels) 🪓<b></a</h1>","metadata":{}},{"cell_type":"code","source":"yTrain = dataTrain.pop('Transported').to_numpy()\nxTrain = dataTrain.to_numpy()\nxTest = dataTest.to_numpy()\nxTrain.shape, yTrain.shape, xTest.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T20:35:40.266374Z","iopub.execute_input":"2022-07-18T20:35:40.266698Z","iopub.status.idle":"2022-07-18T20:35:40.281679Z","shell.execute_reply.started":"2022-07-18T20:35:40.266673Z","shell.execute_reply":"2022-07-18T20:35:40.280079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='classify'></a>\n    <h1 style='color: orange; font-weight: bold; font-family: Cascadia code;'>\n        <center> Classifying 🔃 </center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='build'><b>Building and fitting the models 🏗️<b></a></h1>","metadata":{}},{"cell_type":"markdown","source":"## **The K-nearest neighbours**\n\nThe K-nearest neighbours (KNN) classifier uses proximity to make classifications or predictions about independent data points. This technique may be used for both classification and regression scenarios and the output will vary. In classification instances, a decision is made based on majority vote, i.e., the class assigned to the new data point is taken to be the one that is most frequently seen in the vicinity of the point. KNN is also known as a lazy learner technique since a model is not learned. Instead, the raw data is stored and used everytime a prediction must be made.","metadata":{}},{"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier\n\nknnClassifier = KNeighborsClassifier(3)\nknnClassifier.fit(xTrain, yTrain)\nknnClassifier.score(xTrain, yTrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:20.751951Z","iopub.execute_input":"2022-07-14T16:38:20.752563Z","iopub.status.idle":"2022-07-14T16:38:21.592484Z","shell.execute_reply.started":"2022-07-14T16:38:20.752525Z","shell.execute_reply":"2022-07-14T16:38:21.590996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Support Vector Classifier**\n\nSupport Vector Machine or SVM is one of the most popular Supervised Learning algorithms, which is used for Classification as well as Regression problems. However, primarily, it is used for Classification problems in Machine Learning.\n\nThe goal of the SVM algorithm is to create the best line or decision boundary that can segregate n-dimensional space into classes so that we can easily put the new data point in the correct category in the future. This best decision boundary is called a hyperplane. SVM chooses the extreme points/vectors that help in creating the hyperplane.","metadata":{}},{"cell_type":"code","source":"from sklearn.svm import SVC\n\nsvClassifier = SVC()\nsvClassifier.fit(xTrain, yTrain)\nsvClassifier.score(xTrain, yTrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:21.594736Z","iopub.execute_input":"2022-07-14T16:38:21.596036Z","iopub.status.idle":"2022-07-14T16:38:27.595972Z","shell.execute_reply.started":"2022-07-14T16:38:21.595982Z","shell.execute_reply":"2022-07-14T16:38:27.594676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Random forest classifier**\n\nThe random forest classifier is an improvement over decision tree classifiers. Based on ensemble learning, a random forest classifier contains a number of decision trees on various subsets of the given dataset and takes the average to improve the predictive accuracy of that dataset. In general, a greater number of trees in the forest leads to higher accuracy and prevents the problem of overfitting.","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\nrfClassifier = RandomForestClassifier()\nrfClassifier.fit(xTrain, yTrain)\nrfClassifier.score(xTrain, yTrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:27.597699Z","iopub.execute_input":"2022-07-14T16:38:27.598444Z","iopub.status.idle":"2022-07-14T16:38:28.860311Z","shell.execute_reply.started":"2022-07-14T16:38:27.598386Z","shell.execute_reply":"2022-07-14T16:38:28.858756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Naive Bayes classifier**\n\nNaive Bayes makes the assumption that the features are independent. This means that we are still assuming class-specific covariance matrices (as in QDA), but the covariance matrices are diagonal matrices. This is due to the assumption that the features are independent.\n\nSo, given a training dataset of N input variables x with corresponding target variables t, (Gaussian) Naive Bayes assumes that the class-conditional densities are normally distributed.","metadata":{}},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\n\nnbClassifier = GaussianNB()\nnbClassifier.fit(xTrain, yTrain)\nnbClassifier.score(xTrain, yTrain)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:28.861821Z","iopub.execute_input":"2022-07-14T16:38:28.862183Z","iopub.status.idle":"2022-07-14T16:38:28.904986Z","shell.execute_reply.started":"2022-07-14T16:38:28.862151Z","shell.execute_reply":"2022-07-14T16:38:28.903855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1 align=\"center\" ><a id='anal'><b>Performance Analysis of the different models 📈<b></a></h1>","metadata":{}},{"cell_type":"code","source":"dataPerf = pd.DataFrame(data={'Model': ['SVM', 'RandomForest', 'Naive-Bayes','KNN'], 'Score': [svClassifier.score(xTrain, yTrain), rfClassifier.score(xTrain, yTrain), nbClassifier.score(xTrain, yTrain), knnClassifier.score(xTrain, yTrain)]})\n\nplt.figure(figsize=(12, 8))\nsns.barplot(x=\"Model\", y=\"Score\", data=dataPerf, palette=\"magma\")\nplt.title('Performance analysis of different classifiers')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:28.906782Z","iopub.execute_input":"2022-07-14T16:38:28.908132Z","iopub.status.idle":"2022-07-14T16:38:32.676711Z","shell.execute_reply.started":"2022-07-14T16:38:28.908057Z","shell.execute_reply":"2022-07-14T16:38:32.675506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"----\n#### Based on the above findings, we can conclude that the RandomForestClassifier is best suited for this classification.\n#### Hence we will use this model for our final submission.\n----","metadata":{}},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='submit'></a>\n    <h1 style='color: lightskyblue; font-weight: bold; font-family: Cascadia code;'>\n        <center> Submission ✅ </center>\n    </h1>\n</div>","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame(columns=[\"PassengerId\",\"Transported\"])\nsubmission[\"PassengerId\"] = idCol\nsubmission.set_index('PassengerId')\nsubmission[\"Transported\"] = rfClassifier.predict(xTest).astype(bool)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:32.678328Z","iopub.execute_input":"2022-07-14T16:38:32.678951Z","iopub.status.idle":"2022-07-14T16:38:32.815795Z","shell.execute_reply.started":"2022-07-14T16:38:32.678887Z","shell.execute_reply":"2022-07-14T16:38:32.814561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)\nprint('Submission succesful!')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T16:38:32.818125Z","iopub.execute_input":"2022-07-14T16:38:32.818674Z","iopub.status.idle":"2022-07-14T16:38:32.837409Z","shell.execute_reply.started":"2022-07-14T16:38:32.818611Z","shell.execute_reply":"2022-07-14T16:38:32.836157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style='font-size:200%;'>\n    <a id='import'></a>\n    <h3 style='color: orange; font-weight: bold; font-family: Cascadia code;'>\n        <center> Alright, that's it folks, Thank you for the visit! </center>\n    </h3>\n    <h1 style='color: skyblue; font-weight: bold; font-family: Cascadia code;'>\n        <center> Happy Kaggling! </center>\n    </h1>\n    <center><img style='height: 40%; width: 40%' src=\"https://miro.medium.com/max/3150/2*tQb2DNhHAMPj6u3peTXOFQ.png\"> </center>\n</div>","metadata":{}}]}