{"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":"2022-07-19T08:55:54.458064Z","iopub.execute_input":"2022-07-19T08:55:54.458484Z","iopub.status.idle":"2022-07-19T08:55:54.467906Z","shell.execute_reply.started":"2022-07-19T08:55:54.458443Z","shell.execute_reply":"2022-07-19T08:55:54.467186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Workflow of the Notebook\n\n1. Importing Libraries\n2. Exploring the data\n3. Cleaning the Data \n4. Creating feature set \n    >Encoding Categorical Variables \n5. Splitting the data \n    >Normalizing the columns  \n6. Creating the Model \n    >Predicting and Evaluating\n7. Final Submission \n\n","metadata":{}},{"cell_type":"markdown","source":"## 1.Importing Libraries","metadata":{"execution":{"iopub.status.busy":"2022-06-21T09:51:51.169629Z","iopub.execute_input":"2022-06-21T09:51:51.170533Z","iopub.status.idle":"2022-06-21T09:51:51.174904Z","shell.execute_reply.started":"2022-06-21T09:51:51.170491Z","shell.execute_reply":"2022-06-21T09:51:51.173775Z"}}},{"cell_type":"code","source":"# import libraries\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\n\nfrom sklearn.svm import SVC, LinearSVC\nfrom sklearn.model_selection import train_test_split, StratifiedKFold\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.linear_model import Perceptron\nfrom sklearn.linear_model import SGDClassifier\n\nfrom sklearn.ensemble import RandomForestClassifier , GradientBoostingClassifier , AdaBoostClassifier,ExtraTreesClassifier\n\nfrom sklearn.preprocessing import Normalizer , scale\nfrom sklearn.impute import SimpleImputer\nfrom sklearn.feature_selection import RFECV","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:54.661658Z","iopub.execute_input":"2022-07-19T08:55:54.661950Z","iopub.status.idle":"2022-07-19T08:55:54.669111Z","shell.execute_reply.started":"2022-07-19T08:55:54.661918Z","shell.execute_reply":"2022-07-19T08:55:54.668524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/titanic/test.csv')\ndf_train = pd.read_csv('/kaggle/input/titanic/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:54.852001Z","iopub.execute_input":"2022-07-19T08:55:54.852293Z","iopub.status.idle":"2022-07-19T08:55:54.867971Z","shell.execute_reply.started":"2022-07-19T08:55:54.852264Z","shell.execute_reply":"2022-07-19T08:55:54.867012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:55.041728Z","iopub.execute_input":"2022-07-19T08:55:55.042011Z","iopub.status.idle":"2022-07-19T08:55:55.057474Z","shell.execute_reply.started":"2022-07-19T08:55:55.041981Z","shell.execute_reply":"2022-07-19T08:55:55.056632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:55.248777Z","iopub.execute_input":"2022-07-19T08:55:55.249433Z","iopub.status.idle":"2022-07-19T08:55:55.266213Z","shell.execute_reply.started":"2022-07-19T08:55:55.249385Z","shell.execute_reply":"2022-07-19T08:55:55.265162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# merging the datasets for preprocessing the data \n\nfull_data = pd.concat([df_train , df_test],ignore_index = True )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:55.463190Z","iopub.execute_input":"2022-07-19T08:55:55.463510Z","iopub.status.idle":"2022-07-19T08:55:55.473478Z","shell.execute_reply.started":"2022-07-19T08:55:55.463476Z","shell.execute_reply":"2022-07-19T08:55:55.472610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:55.708963Z","iopub.execute_input":"2022-07-19T08:55:55.709734Z","iopub.status.idle":"2022-07-19T08:55:55.723195Z","shell.execute_reply.started":"2022-07-19T08:55:55.709686Z","shell.execute_reply":"2022-07-19T08:55:55.722278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:55.903928Z","iopub.execute_input":"2022-07-19T08:55:55.904235Z","iopub.status.idle":"2022-07-19T08:55:55.938075Z","shell.execute_reply.started":"2022-07-19T08:55:55.904184Z","shell.execute_reply":"2022-07-19T08:55:55.937126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:56.105355Z","iopub.execute_input":"2022-07-19T08:55:56.106185Z","iopub.status.idle":"2022-07-19T08:55:56.112007Z","shell.execute_reply.started":"2022-07-19T08:55:56.106138Z","shell.execute_reply":"2022-07-19T08:55:56.111108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:56.322288Z","iopub.execute_input":"2022-07-19T08:55:56.322604Z","iopub.status.idle":"2022-07-19T08:55:56.336008Z","shell.execute_reply.started":"2022-07-19T08:55:56.322566Z","shell.execute_reply":"2022-07-19T08:55:56.335108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  2.Exploring the Data","metadata":{"execution":{"iopub.status.busy":"2022-06-21T09:51:53.121029Z","iopub.execute_input":"2022-06-21T09:51:53.121699Z","iopub.status.idle":"2022-06-21T09:51:53.124824Z","shell.execute_reply.started":"2022-06-21T09:51:53.121665Z","shell.execute_reply":"2022-06-21T09:51:53.124047Z"}}},{"cell_type":"code","source":"full_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:56.508576Z","iopub.execute_input":"2022-07-19T08:55:56.508871Z","iopub.status.idle":"2022-07-19T08:55:56.518677Z","shell.execute_reply.started":"2022-07-19T08:55:56.508842Z","shell.execute_reply":"2022-07-19T08:55:56.517859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There are 5 columsn with null values and 3 of them ['Survived','Age','Cabin','Fare' ,'Embarked'] have considerably high amount of null values .","metadata":{"execution":{"iopub.status.busy":"2022-06-20T11:06:46.180915Z","iopub.execute_input":"2022-06-20T11:06:46.181554Z","iopub.status.idle":"2022-06-20T11:06:46.186663Z","shell.execute_reply.started":"2022-06-20T11:06:46.181505Z","shell.execute_reply":"2022-06-20T11:06:46.185784Z"}}},{"cell_type":"code","source":"# Handling the missing data Column-Wise\n\nfull_data['Survived'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:56.697035Z","iopub.execute_input":"2022-07-19T08:55:56.697589Z","iopub.status.idle":"2022-07-19T08:55:56.704052Z","shell.execute_reply.started":"2022-07-19T08:55:56.697530Z","shell.execute_reply":"2022-07-19T08:55:56.703525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Age'].value_counts(bins = 10)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:56.880370Z","iopub.execute_input":"2022-07-19T08:55:56.881003Z","iopub.status.idle":"2022-07-19T08:55:56.896613Z","shell.execute_reply.started":"2022-07-19T08:55:56.880954Z","shell.execute_reply":"2022-07-19T08:55:56.895940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:57.074557Z","iopub.execute_input":"2022-07-19T08:55:57.075300Z","iopub.status.idle":"2022-07-19T08:55:57.088571Z","shell.execute_reply.started":"2022-07-19T08:55:57.075256Z","shell.execute_reply":"2022-07-19T08:55:57.087954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Mean age is :\",full_data['Age'].mean())\nprint(\"The median of Age is :\",full_data['Age'].median())\nprint(\"The mode of the data is :\",full_data['Age'].mode())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:57.291698Z","iopub.execute_input":"2022-07-19T08:55:57.292168Z","iopub.status.idle":"2022-07-19T08:55:57.302351Z","shell.execute_reply.started":"2022-07-19T08:55:57.292131Z","shell.execute_reply":"2022-07-19T08:55:57.301336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Fare'].value_counts(bins = 10 )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:57.529521Z","iopub.execute_input":"2022-07-19T08:55:57.530356Z","iopub.status.idle":"2022-07-19T08:55:57.545403Z","shell.execute_reply.started":"2022-07-19T08:55:57.530309Z","shell.execute_reply":"2022-07-19T08:55:57.544612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:57.738809Z","iopub.execute_input":"2022-07-19T08:55:57.739482Z","iopub.status.idle":"2022-07-19T08:55:57.772362Z","shell.execute_reply.started":"2022-07-19T08:55:57.739436Z","shell.execute_reply":"2022-07-19T08:55:57.771439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(full_data['Fare'].mean())\nprint(full_data['Fare'].median())\nprint(full_data['Fare'].mode())\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:57.931627Z","iopub.execute_input":"2022-07-19T08:55:57.931916Z","iopub.status.idle":"2022-07-19T08:55:57.940230Z","shell.execute_reply.started":"2022-07-19T08:55:57.931884Z","shell.execute_reply":"2022-07-19T08:55:57.939242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Cabin'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:58.113048Z","iopub.execute_input":"2022-07-19T08:55:58.113502Z","iopub.status.idle":"2022-07-19T08:55:58.123380Z","shell.execute_reply.started":"2022-07-19T08:55:58.113461Z","shell.execute_reply":"2022-07-19T08:55:58.122463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,8))\nsns.boxplot(y=full_data['Fare'])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:58.337368Z","iopub.execute_input":"2022-07-19T08:55:58.337663Z","iopub.status.idle":"2022-07-19T08:55:58.525010Z","shell.execute_reply.started":"2022-07-19T08:55:58.337631Z","shell.execute_reply":"2022-07-19T08:55:58.524413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,8))\nsns.boxplot(y=full_data['Age'])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:58.546473Z","iopub.execute_input":"2022-07-19T08:55:58.547348Z","iopub.status.idle":"2022-07-19T08:55:58.758431Z","shell.execute_reply.started":"2022-07-19T08:55:58.547299Z","shell.execute_reply":"2022-07-19T08:55:58.755680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Embarked'].value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:58.760259Z","iopub.execute_input":"2022-07-19T08:55:58.760616Z","iopub.status.idle":"2022-07-19T08:55:58.769628Z","shell.execute_reply.started":"2022-07-19T08:55:58.760571Z","shell.execute_reply":"2022-07-19T08:55:58.768795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The embarked column has the highest values of S . Filling the null values with the same . The missing rows for Fare is very low and hence filling it with the mean value. The Cabin Column is not of much use and hence removing it.","metadata":{}},{"cell_type":"markdown","source":"## 3. Cleaning the data ","metadata":{"execution":{"iopub.status.busy":"2022-06-21T09:55:45.608648Z","iopub.execute_input":"2022-06-21T09:55:45.608979Z","iopub.status.idle":"2022-06-21T09:55:45.612771Z","shell.execute_reply.started":"2022-06-21T09:55:45.608944Z","shell.execute_reply":"2022-06-21T09:55:45.611973Z"}}},{"cell_type":"code","source":"full_data.drop('Cabin',axis = 1 , inplace = True )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:58.915746Z","iopub.execute_input":"2022-07-19T08:55:58.916051Z","iopub.status.idle":"2022-07-19T08:55:58.921409Z","shell.execute_reply.started":"2022-07-19T08:55:58.916020Z","shell.execute_reply":"2022-07-19T08:55:58.920833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Embarked'].fillna('S',inplace= True)   ","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:59.106379Z","iopub.execute_input":"2022-07-19T08:55:59.106691Z","iopub.status.idle":"2022-07-19T08:55:59.112072Z","shell.execute_reply.started":"2022-07-19T08:55:59.106651Z","shell.execute_reply":"2022-07-19T08:55:59.111159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Fare'].fillna(full_data['Fare'].mean(),inplace = True )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:59.314239Z","iopub.execute_input":"2022-07-19T08:55:59.314698Z","iopub.status.idle":"2022-07-19T08:55:59.319465Z","shell.execute_reply.started":"2022-07-19T08:55:59.314666Z","shell.execute_reply":"2022-07-19T08:55:59.318638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_df = full_data[['Survived', 'Age']].groupby(by='Age').size().reset_index().rename(columns = {0:'Entries'})\ngrouped_df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:59.572695Z","iopub.execute_input":"2022-07-19T08:55:59.573090Z","iopub.status.idle":"2022-07-19T08:55:59.590095Z","shell.execute_reply.started":"2022-07-19T08:55:59.573061Z","shell.execute_reply":"2022-07-19T08:55:59.589451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(full_data['Age'], bins = 10)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:59.751121Z","iopub.execute_input":"2022-07-19T08:55:59.751524Z","iopub.status.idle":"2022-07-19T08:55:59.948294Z","shell.execute_reply.started":"2022-07-19T08:55:59.751493Z","shell.execute_reply":"2022-07-19T08:55:59.947164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grid = sns.FacetGrid(df_train, row='Embarked', size=2.2, aspect=1.6)\ngrid.map(sns.pointplot, 'Pclass', 'Survived', 'Sex', palette='deep')\ngrid.add_legend()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:55:59.949754Z","iopub.execute_input":"2022-07-19T08:55:59.950008Z","iopub.status.idle":"2022-07-19T08:56:01.329537Z","shell.execute_reply.started":"2022-07-19T08:55:59.949977Z","shell.execute_reply":"2022-07-19T08:56:01.328653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot( data = df_train , x = 'Age' ,bins = 6)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.331244Z","iopub.execute_input":"2022-07-19T08:56:01.331591Z","iopub.status.idle":"2022-07-19T08:56:01.568322Z","shell.execute_reply.started":"2022-07-19T08:56:01.331528Z","shell.execute_reply":"2022-07-19T08:56:01.567473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.569516Z","iopub.execute_input":"2022-07-19T08:56:01.569789Z","iopub.status.idle":"2022-07-19T08:56:01.575638Z","shell.execute_reply.started":"2022-07-19T08:56:01.569759Z","shell.execute_reply":"2022-07-19T08:56:01.574841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.577733Z","iopub.execute_input":"2022-07-19T08:56:01.577955Z","iopub.status.idle":"2022-07-19T08:56:01.595387Z","shell.execute_reply.started":"2022-07-19T08:56:01.577928Z","shell.execute_reply":"2022-07-19T08:56:01.594669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.median()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.596508Z","iopub.execute_input":"2022-07-19T08:56:01.597668Z","iopub.status.idle":"2022-07-19T08:56:01.613234Z","shell.execute_reply.started":"2022-07-19T08:56:01.597617Z","shell.execute_reply":"2022-07-19T08:56:01.612602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.614376Z","iopub.execute_input":"2022-07-19T08:56:01.615424Z","iopub.status.idle":"2022-07-19T08:56:01.620158Z","shell.execute_reply.started":"2022-07-19T08:56:01.615388Z","shell.execute_reply":"2022-07-19T08:56:01.619608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.621238Z","iopub.execute_input":"2022-07-19T08:56:01.621612Z","iopub.status.idle":"2022-07-19T08:56:01.634433Z","shell.execute_reply.started":"2022-07-19T08:56:01.621527Z","shell.execute_reply":"2022-07-19T08:56:01.633594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(data = full_data, x= 'Age')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:01.637904Z","iopub.execute_input":"2022-07-19T08:56:01.638560Z","iopub.status.idle":"2022-07-19T08:56:02.021795Z","shell.execute_reply.started":"2022-07-19T08:56:01.638442Z","shell.execute_reply":"2022-07-19T08:56:02.020612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The mean , median , mode of the Age are close and hence imputing the value of missing columns as the average value. From the histogram the distribution of the age range in the datset is also visible, with most of the people falling in the age group of (14 - 40 )","metadata":{"execution":{"iopub.status.busy":"2022-06-21T10:05:57.04851Z","iopub.execute_input":"2022-06-21T10:05:57.048811Z","iopub.status.idle":"2022-06-21T10:05:57.053738Z","shell.execute_reply.started":"2022-06-21T10:05:57.048777Z","shell.execute_reply":"2022-06-21T10:05:57.052804Z"}}},{"cell_type":"code","source":"### Age column has empty values\n# Imputing the missing values with the mean value of age since the distribution of the mean , median and mode is close\nfull_data['Age'].fillna(full_data['Age'].mean(), inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.023189Z","iopub.execute_input":"2022-07-19T08:56:02.023443Z","iopub.status.idle":"2022-07-19T08:56:02.029228Z","shell.execute_reply.started":"2022-07-19T08:56:02.023412Z","shell.execute_reply":"2022-07-19T08:56:02.027997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.mean()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.031746Z","iopub.execute_input":"2022-07-19T08:56:02.032316Z","iopub.status.idle":"2022-07-19T08:56:02.057512Z","shell.execute_reply.started":"2022-07-19T08:56:02.032273Z","shell.execute_reply":"2022-07-19T08:56:02.056657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.058758Z","iopub.execute_input":"2022-07-19T08:56:02.059816Z","iopub.status.idle":"2022-07-19T08:56:02.079576Z","shell.execute_reply.started":"2022-07-19T08:56:02.059771Z","shell.execute_reply":"2022-07-19T08:56:02.078626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Creating feature set","metadata":{}},{"cell_type":"code","source":"# Converting categorical features \n\nfull_data.Sex.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.080972Z","iopub.execute_input":"2022-07-19T08:56:02.081254Z","iopub.status.idle":"2022-07-19T08:56:02.096800Z","shell.execute_reply.started":"2022-07-19T08:56:02.081221Z","shell.execute_reply":"2022-07-19T08:56:02.095861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data['Sex'] = full_data.Sex.map({'male' : 0, 'female' : 1})","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.130238Z","iopub.execute_input":"2022-07-19T08:56:02.130516Z","iopub.status.idle":"2022-07-19T08:56:02.137899Z","shell.execute_reply.started":"2022-07-19T08:56:02.130485Z","shell.execute_reply":"2022-07-19T08:56:02.136969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.Sex.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.344858Z","iopub.execute_input":"2022-07-19T08:56:02.345119Z","iopub.status.idle":"2022-07-19T08:56:02.352712Z","shell.execute_reply.started":"2022-07-19T08:56:02.345092Z","shell.execute_reply":"2022-07-19T08:56:02.351846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sibsp # of siblings / spouses aboard the Titanic\n\nfull_data.SibSp.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.530406Z","iopub.execute_input":"2022-07-19T08:56:02.531061Z","iopub.status.idle":"2022-07-19T08:56:02.540325Z","shell.execute_reply.started":"2022-07-19T08:56:02.531013Z","shell.execute_reply":"2022-07-19T08:56:02.539502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# of parents / children aboard the Titanic\nfull_data.Parch.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.711515Z","iopub.execute_input":"2022-07-19T08:56:02.711843Z","iopub.status.idle":"2022-07-19T08:56:02.719600Z","shell.execute_reply.started":"2022-07-19T08:56:02.711810Z","shell.execute_reply":"2022-07-19T08:56:02.718745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:02.897140Z","iopub.execute_input":"2022-07-19T08:56:02.897412Z","iopub.status.idle":"2022-07-19T08:56:02.903964Z","shell.execute_reply.started":"2022-07-19T08:56:02.897384Z","shell.execute_reply":"2022-07-19T08:56:02.903143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.Pclass.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.091938Z","iopub.execute_input":"2022-07-19T08:56:03.092225Z","iopub.status.idle":"2022-07-19T08:56:03.100179Z","shell.execute_reply.started":"2022-07-19T08:56:03.092196Z","shell.execute_reply":"2022-07-19T08:56:03.099373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data.Embarked.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.300488Z","iopub.execute_input":"2022-07-19T08:56:03.300779Z","iopub.status.idle":"2022-07-19T08:56:03.309408Z","shell.execute_reply.started":"2022-07-19T08:56:03.300751Z","shell.execute_reply":"2022-07-19T08:56:03.308647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.get_dummies(full_data ,  columns = ['Embarked'] ,drop_first = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.502125Z","iopub.execute_input":"2022-07-19T08:56:03.502724Z","iopub.status.idle":"2022-07-19T08:56:03.511271Z","shell.execute_reply.started":"2022-07-19T08:56:03.502691Z","shell.execute_reply":"2022-07-19T08:56:03.510633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.Survived.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.652567Z","iopub.execute_input":"2022-07-19T08:56:03.653065Z","iopub.status.idle":"2022-07-19T08:56:03.660155Z","shell.execute_reply.started":"2022-07-19T08:56:03.653030Z","shell.execute_reply":"2022-07-19T08:56:03.659254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('Name', axis = 1 , inplace = True )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.747142Z","iopub.execute_input":"2022-07-19T08:56:03.747433Z","iopub.status.idle":"2022-07-19T08:56:03.754531Z","shell.execute_reply.started":"2022-07-19T08:56:03.747401Z","shell.execute_reply":"2022-07-19T08:56:03.753954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop('Ticket', axis = 1 , inplace = True )","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.829397Z","iopub.execute_input":"2022-07-19T08:56:03.829762Z","iopub.status.idle":"2022-07-19T08:56:03.834832Z","shell.execute_reply.started":"2022-07-19T08:56:03.829726Z","shell.execute_reply":"2022-07-19T08:56:03.834240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:03.970626Z","iopub.execute_input":"2022-07-19T08:56:03.971068Z","iopub.status.idle":"2022-07-19T08:56:03.987759Z","shell.execute_reply.started":"2022-07-19T08:56:03.971038Z","shell.execute_reply":"2022-07-19T08:56:03.987068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,10))\nsns.heatmap(df.corr(),cmap = 'YlGnBu',annot = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.105142Z","iopub.execute_input":"2022-07-19T08:56:04.106180Z","iopub.status.idle":"2022-07-19T08:56:04.935563Z","shell.execute_reply.started":"2022-07-19T08:56:04.106138Z","shell.execute_reply":"2022-07-19T08:56:04.934672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"No concrete corelation between any column.Considerable correlation between sex and survival status of a passenger. There is a considerable positive correlation between Survived column and the 'Fare' and 'Class' columns.Another positive correlation between Parch and SibSp ... Given that ceratin passenegers were travelling with families , there could have been high likelihood between the columns. Since that is not the case we will not be dropping any columns further. ","metadata":{"execution":{"iopub.status.busy":"2022-06-22T07:07:13.135384Z","iopub.execute_input":"2022-06-22T07:07:13.135682Z","iopub.status.idle":"2022-06-22T07:07:13.140305Z","shell.execute_reply.started":"2022-06-22T07:07:13.135647Z","shell.execute_reply":"2022-06-22T07:07:13.139448Z"}}},{"cell_type":"code","source":"df.Survived.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.937098Z","iopub.execute_input":"2022-07-19T08:56:04.937338Z","iopub.status.idle":"2022-07-19T08:56:04.945142Z","shell.execute_reply.started":"2022-07-19T08:56:04.937308Z","shell.execute_reply":"2022-07-19T08:56:04.944594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 5.Splitting the data","metadata":{}},{"cell_type":"code","source":"df_train = df[df['Survived'] >= 0]\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.946139Z","iopub.execute_input":"2022-07-19T08:56:04.946569Z","iopub.status.idle":"2022-07-19T08:56:04.959130Z","shell.execute_reply.started":"2022-07-19T08:56:04.946500Z","shell.execute_reply":"2022-07-19T08:56:04.958460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = df[~(df['Survived']>= 0)]","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.961702Z","iopub.execute_input":"2022-07-19T08:56:04.962113Z","iopub.status.idle":"2022-07-19T08:56:04.975174Z","shell.execute_reply.started":"2022-07-19T08:56:04.962078Z","shell.execute_reply":"2022-07-19T08:56:04.974512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df_train.drop(['Survived','PassengerId'],axis = 1 )\ny_train = df_train['Survived']\nX_test  = df_test.drop(['Survived','PassengerId'],axis = 1 )\ny_test  = df_test['Survived']","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.976473Z","iopub.execute_input":"2022-07-19T08:56:04.977214Z","iopub.status.idle":"2022-07-19T08:56:04.991216Z","shell.execute_reply.started":"2022-07-19T08:56:04.977168Z","shell.execute_reply":"2022-07-19T08:56:04.990220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scaling the dataset \nfrom sklearn.preprocessing import MinMaxScaler\n\n\n\nscaler = MinMaxScaler()\nX_train = scaler.fit_transform(X_train)\n\n# we must apply the scaling to the test set as well that we are computing for the training set\nX_test = scaler.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:04.992416Z","iopub.execute_input":"2022-07-19T08:56:04.992829Z","iopub.status.idle":"2022-07-19T08:56:05.011731Z","shell.execute_reply.started":"2022-07-19T08:56:04.992778Z","shell.execute_reply":"2022-07-19T08:56:05.010765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Logistic Regression","metadata":{}},{"cell_type":"code","source":"#Fitting the data in the Logistic Regression Model \n\nlog_reg = LogisticRegression()\nlog_reg.fit(X_train, y_train)\nY_pred1 = log_reg.predict(X_test)\nacc_log = round(log_reg.score(X_train, y_train) * 100, 2)\nacc_log","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.012855Z","iopub.execute_input":"2022-07-19T08:56:05.013106Z","iopub.status.idle":"2022-07-19T08:56:05.039940Z","shell.execute_reply.started":"2022-07-19T08:56:05.013076Z","shell.execute_reply":"2022-07-19T08:56:05.039056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coef = pd.DataFrame(df_train.columns.delete(0))\ncoef.columns = ['Feature']\ncoef['Corr'] = pd.Series(log_reg.coef_[0])\ncoef.sort_values(by = 'Corr', ascending = False)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.040944Z","iopub.execute_input":"2022-07-19T08:56:05.041425Z","iopub.status.idle":"2022-07-19T08:56:05.057313Z","shell.execute_reply.started":"2022-07-19T08:56:05.041389Z","shell.execute_reply":"2022-07-19T08:56:05.056372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Support Vectors","metadata":{}},{"cell_type":"code","source":"### Support Vectors \n\nsvc = SVC()\nsvc.fit(X_train , y_train)\nY_pred2 = svc.predict(X_test)\nacc = round(svc.score(X_train, y_train)*100,2)\nacc\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.058486Z","iopub.execute_input":"2022-07-19T08:56:05.058778Z","iopub.status.idle":"2022-07-19T08:56:05.117980Z","shell.execute_reply.started":"2022-07-19T08:56:05.058747Z","shell.execute_reply":"2022-07-19T08:56:05.116938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### KNN","metadata":{}},{"cell_type":"code","source":"knn = KNeighborsClassifier(n_neighbors = 3)\nknn.fit(X_train, y_train)\nY_pred3 = knn.predict(X_test)\nacc_knn = round(knn.score(X_train, y_train) * 100, 2)\nacc_knn","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.120320Z","iopub.execute_input":"2022-07-19T08:56:05.120606Z","iopub.status.idle":"2022-07-19T08:56:05.186315Z","shell.execute_reply.started":"2022-07-19T08:56:05.120564Z","shell.execute_reply":"2022-07-19T08:56:05.185511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###  Gaussian Naive Bayes","metadata":{}},{"cell_type":"code","source":"\ngaussian = GaussianNB()\ngaussian.fit(X_train, y_train)\nY_pred4 = gaussian.predict(X_test)\nacc_gaussian = round(gaussian.score(X_train, y_train) * 100, 2)\nacc_gaussian","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.187469Z","iopub.execute_input":"2022-07-19T08:56:05.188048Z","iopub.status.idle":"2022-07-19T08:56:05.198084Z","shell.execute_reply.started":"2022-07-19T08:56:05.188011Z","shell.execute_reply":"2022-07-19T08:56:05.197107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Perceptron","metadata":{}},{"cell_type":"code","source":"\n\nperceptron = Perceptron()\nperceptron.fit(X_train, y_train)\nY_pred5 = perceptron.predict(X_test)\nacc_perceptron = round(perceptron.score(X_train, y_train) * 100, 2)\nacc_perceptron\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.199625Z","iopub.execute_input":"2022-07-19T08:56:05.200197Z","iopub.status.idle":"2022-07-19T08:56:05.211770Z","shell.execute_reply.started":"2022-07-19T08:56:05.200151Z","shell.execute_reply":"2022-07-19T08:56:05.211012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Linear SVC","metadata":{}},{"cell_type":"code","source":"\n\nlinear_svc = LinearSVC()\nlinear_svc.fit(X_train, y_train)\nY_pred6 = linear_svc.predict(X_test)\nacc_linear_svc = round(linear_svc.score(X_train, y_train) * 100, 2)\nacc_linear_svc","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.212787Z","iopub.execute_input":"2022-07-19T08:56:05.213392Z","iopub.status.idle":"2022-07-19T08:56:05.229527Z","shell.execute_reply.started":"2022-07-19T08:56:05.213348Z","shell.execute_reply":"2022-07-19T08:56:05.228680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Stochastic Gradient Descent ","metadata":{"execution":{"iopub.status.busy":"2022-07-03T16:52:11.898253Z","iopub.execute_input":"2022-07-03T16:52:11.89857Z","iopub.status.idle":"2022-07-03T16:52:11.901911Z","shell.execute_reply.started":"2022-07-03T16:52:11.898535Z","shell.execute_reply":"2022-07-03T16:52:11.901269Z"}}},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.230728Z","iopub.execute_input":"2022-07-19T08:56:05.231125Z","iopub.status.idle":"2022-07-19T08:56:05.235064Z","shell.execute_reply.started":"2022-07-19T08:56:05.231094Z","shell.execute_reply":"2022-07-19T08:56:05.234307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGDClassifier()\nsgd.fit(X_train, y_train)\nY_pred7 = sgd.predict(X_test)\nacc_sgd = round(sgd.score(X_train, y_train) * 100, 2)\nacc_sgd","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.236872Z","iopub.execute_input":"2022-07-19T08:56:05.237575Z","iopub.status.idle":"2022-07-19T08:56:05.255861Z","shell.execute_reply.started":"2022-07-19T08:56:05.237465Z","shell.execute_reply":"2022-07-19T08:56:05.254956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Decision Trees","metadata":{}},{"cell_type":"code","source":"# Decision Tree\n\ndecision_tree = DecisionTreeClassifier()\ndecision_tree.fit(X_train, y_train)\nY_pred8 = decision_tree.predict(X_test)\nacc_decision_tree = round(decision_tree.score(X_train, y_train) * 100, 2)\nacc_decision_tree","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.331047Z","iopub.execute_input":"2022-07-19T08:56:05.331724Z","iopub.status.idle":"2022-07-19T08:56:05.343211Z","shell.execute_reply.started":"2022-07-19T08:56:05.331675Z","shell.execute_reply":"2022-07-19T08:56:05.342611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Random Forest","metadata":{}},{"cell_type":"code","source":"\n\nrandom_forest = RandomForestClassifier(n_estimators=100)\nrandom_forest.fit(X_train, y_train)\nY_pred9 = random_forest.predict(X_test)\nrandom_forest.score(X_train, y_train)\nacc_random_forest = round(random_forest.score(X_train, y_train) * 100, 2)\nacc_random_forest\n","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:56:05.521658Z","iopub.execute_input":"2022-07-19T08:56:05.521951Z","iopub.status.idle":"2022-07-19T08:56:05.812673Z","shell.execute_reply.started":"2022-07-19T08:56:05.521919Z","shell.execute_reply":"2022-07-19T08:56:05.811753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### RF and DT are clearly overfitting.","metadata":{}},{"cell_type":"code","source":"models = pd.DataFrame({\n    'Model_Name': ['Support Vector Machines', 'KNN', 'Logistic Regression', \n              'Random Forest', 'Naive Bayes', 'Perceptron', \n              'Stochastic Gradient Decent', 'Linear SVC', \n              'Decision Tree'],\n    'Score': [acc, acc_knn, acc_log, \n              acc_random_forest, acc_gaussian, acc_perceptron, \n              acc_sgd, acc_linear_svc, acc_decision_tree]})\nmodels.sort_values(by='Score', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T08:59:29.154290Z","iopub.execute_input":"2022-07-19T08:59:29.155139Z","iopub.status.idle":"2022-07-19T08:59:29.169069Z","shell.execute_reply.started":"2022-07-19T08:59:29.155102Z","shell.execute_reply":"2022-07-19T08:59:29.168431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\n        \"PassengerId\": df_test[\"PassengerId\"],\n        \"Survived\": Y_pred5\n    })","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:00:13.450404Z","iopub.execute_input":"2022-07-19T09:00:13.451095Z","iopub.status.idle":"2022-07-19T09:00:13.456898Z","shell.execute_reply.started":"2022-07-19T09:00:13.451033Z","shell.execute_reply":"2022-07-19T09:00:13.455968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:00:25.976524Z","iopub.execute_input":"2022-07-19T09:00:25.976994Z","iopub.status.idle":"2022-07-19T09:00:25.983328Z","shell.execute_reply.started":"2022-07-19T09:00:25.976946Z","shell.execute_reply":"2022-07-19T09:00:25.982716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\nReferences : \nFor submission of my first ever Notebook , I am greatly thankful for the other notebooks on Kaggle from which I could learn.\n\nWould like to get input from people to enhance my learning.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}