{"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":"# Importing libraries\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.078044Z","iopub.execute_input":"2022-07-11T16:20:41.078453Z","iopub.status.idle":"2022-07-11T16:20:41.085282Z","shell.execute_reply.started":"2022-07-11T16:20:41.078421Z","shell.execute_reply":"2022-07-11T16:20:41.084531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training data\ntrain = pd.read_csv(\"../input/titanic/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.091192Z","iopub.execute_input":"2022-07-11T16:20:41.091917Z","iopub.status.idle":"2022-07-11T16:20:41.120661Z","shell.execute_reply.started":"2022-07-11T16:20:41.091882Z","shell.execute_reply":"2022-07-11T16:20:41.119849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test data\ntest = pd.read_csv(\"../input/titanic/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.122154Z","iopub.execute_input":"2022-07-11T16:20:41.122625Z","iopub.status.idle":"2022-07-11T16:20:41.145923Z","shell.execute_reply.started":"2022-07-11T16:20:41.122593Z","shell.execute_reply":"2022-07-11T16:20:41.144830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Handling missing data","metadata":{}},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.147297Z","iopub.execute_input":"2022-07-11T16:20:41.147774Z","iopub.status.idle":"2022-07-11T16:20:41.158590Z","shell.execute_reply.started":"2022-07-11T16:20:41.147704Z","shell.execute_reply":"2022-07-11T16:20:41.157604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(rc = {\"figure.figsize\" : (15,8)})\nsns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap=\"cividis\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.161078Z","iopub.execute_input":"2022-07-11T16:20:41.162228Z","iopub.status.idle":"2022-07-11T16:20:41.446743Z","shell.execute_reply.started":"2022-07-11T16:20:41.162172Z","shell.execute_reply":"2022-07-11T16:20:41.445795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.448416Z","iopub.execute_input":"2022-07-11T16:20:41.449064Z","iopub.status.idle":"2022-07-11T16:20:41.460672Z","shell.execute_reply.started":"2022-07-11T16:20:41.449016Z","shell.execute_reply":"2022-07-11T16:20:41.459732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(test.isnull(),yticklabels=False,cbar=False,cmap=\"cividis\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.462323Z","iopub.execute_input":"2022-07-11T16:20:41.462971Z","iopub.status.idle":"2022-07-11T16:20:41.652452Z","shell.execute_reply.started":"2022-07-11T16:20:41.462924Z","shell.execute_reply":"2022-07-11T16:20:41.651609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Missing values in Training dataset**\n* Age\n* Cabin\n* Embarked \n\n**Test dataset**\n* Age\n* Cabin\n* Fare \n","metadata":{}},{"cell_type":"markdown","source":"## Handling missing values\n\n**Finding variation of Age values with different features**","metadata":{}},{"cell_type":"code","source":"fc = sns.displot(x = train.Age,col=train.Pclass)\n# For adding count over bars\nfor i in range(3):\n    ax = fc.facet_axis(0,i)\n    ax.bar_label(ax.containers[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:41.653589Z","iopub.execute_input":"2022-07-11T16:20:41.654179Z","iopub.status.idle":"2022-07-11T16:20:42.844782Z","shell.execute_reply.started":"2022-07-11T16:20:41.654141Z","shell.execute_reply":"2022-07-11T16:20:42.843941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Medians of each class \")\nprint(train.groupby(\"Pclass\")[\"Age\"].median())\nsns.boxplot(x = train.Pclass,y = train.Age,palette=\"rainbow\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:42.846152Z","iopub.execute_input":"2022-07-11T16:20:42.846990Z","iopub.status.idle":"2022-07-11T16:20:43.055085Z","shell.execute_reply.started":"2022-07-11T16:20:42.846942Z","shell.execute_reply":"2022-07-11T16:20:43.053994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Variation of Age with Sex**","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:09:37.551535Z","iopub.execute_input":"2022-07-11T14:09:37.551943Z","iopub.status.idle":"2022-07-11T14:09:37.557889Z","shell.execute_reply.started":"2022-07-11T14:09:37.551909Z","shell.execute_reply":"2022-07-11T14:09:37.556596Z"}}},{"cell_type":"code","source":"fc = sns.displot(x = train.Age,col = train.Sex, palette=\"rainbow\")\nfor i in range(2):\n    ax = fc.facet_axis(0,i)\n    ax.bar_label(ax.containers[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:43.056484Z","iopub.execute_input":"2022-07-11T16:20:43.057643Z","iopub.status.idle":"2022-07-11T16:20:44.195285Z","shell.execute_reply.started":"2022-07-11T16:20:43.057578Z","shell.execute_reply":"2022-07-11T16:20:44.194567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.groupby(\"Sex\")[\"Age\"].median())\nsns.boxplot(x = train.Sex,y = train.Age, palette=\"rainbow\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:44.198081Z","iopub.execute_input":"2022-07-11T16:20:44.198634Z","iopub.status.idle":"2022-07-11T16:20:44.430143Z","shell.execute_reply.started":"2022-07-11T16:20:44.198583Z","shell.execute_reply":"2022-07-11T16:20:44.429036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Conclusion\n* **Medians** vary significantly with both **Sex and Pclass**\n* We can see from box plot, in comparison to Sex **variation of Age with Pclass is more significant**","metadata":{}},{"cell_type":"code","source":"def setMissingAge(cols):\n    Age = cols[0]\n    Pclass = cols[1]\n    if pd.isnull(Age):\n        if Pclass == 1:\n            return 37\n        elif Pclass == 2:\n            return 29\n        else:\n            return 24\n    else:\n        return Age\ntrain[\"Age\"] = train[[\"Age\",\"Pclass\"]].apply(setMissingAge,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:44.431621Z","iopub.execute_input":"2022-07-11T16:20:44.432353Z","iopub.status.idle":"2022-07-11T16:20:44.458191Z","shell.execute_reply.started":"2022-07-11T16:20:44.432310Z","shell.execute_reply":"2022-07-11T16:20:44.457208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* As there are many null values in cabin so **dropping cabin column**.\n* Effect of 2 null values in embarked will be insignificant then leaving it as it is.","metadata":{}},{"cell_type":"code","source":"train.drop(\"Cabin\",axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:44.459266Z","iopub.execute_input":"2022-07-11T16:20:44.459589Z","iopub.status.idle":"2022-07-11T16:20:44.465790Z","shell.execute_reply.started":"2022-07-11T16:20:44.459562Z","shell.execute_reply":"2022-07-11T16:20:44.464778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Variation of Age with Pclass**","metadata":{}},{"cell_type":"code","source":"sns.displot(x = train.Embarked,col= train.Pclass)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:44.467490Z","iopub.execute_input":"2022-07-11T16:20:44.468185Z","iopub.status.idle":"2022-07-11T16:20:45.155190Z","shell.execute_reply.started":"2022-07-11T16:20:44.468136Z","shell.execute_reply":"2022-07-11T16:20:45.153908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Variation of Age with Sex**","metadata":{}},{"cell_type":"code","source":"sns.displot(x = train.Embarked,col=train.Sex)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.156805Z","iopub.execute_input":"2022-07-11T16:20:45.157532Z","iopub.status.idle":"2022-07-11T16:20:45.652088Z","shell.execute_reply.started":"2022-07-11T16:20:45.157481Z","shell.execute_reply":"2022-07-11T16:20:45.650840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[pd.isnull(train[\"Embarked\"])]","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-11T16:20:45.653654Z","iopub.execute_input":"2022-07-11T16:20:45.654142Z","iopub.status.idle":"2022-07-11T16:20:45.671903Z","shell.execute_reply.started":"2022-07-11T16:20:45.654097Z","shell.execute_reply":"2022-07-11T16:20:45.670785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The two null values have \n* Gender : Female\n* Pclass : 1\n\nFrom above displots we can see that most passengers having gender female and pclass 1 have embarked from \"S\", hence **replacing null with Southampton (S)**.","metadata":{}},{"cell_type":"code","source":"train.loc[pd.isnull(train[\"Embarked\"]),\"Embarked\"] = \"S\"","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.673630Z","iopub.execute_input":"2022-07-11T16:20:45.674289Z","iopub.status.idle":"2022-07-11T16:20:45.683494Z","shell.execute_reply.started":"2022-07-11T16:20:45.674246Z","shell.execute_reply":"2022-07-11T16:20:45.682692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.684479Z","iopub.execute_input":"2022-07-11T16:20:45.684836Z","iopub.status.idle":"2022-07-11T16:20:45.702728Z","shell.execute_reply.started":"2022-07-11T16:20:45.684800Z","shell.execute_reply":"2022-07-11T16:20:45.701816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*No Null value found in training data.*","metadata":{}},{"cell_type":"markdown","source":"## Handling missing values in Test data","metadata":{}},{"cell_type":"code","source":"test.groupby(\"Pclass\")[\"Age\"].median()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.704236Z","iopub.execute_input":"2022-07-11T16:20:45.704669Z","iopub.status.idle":"2022-07-11T16:20:45.715270Z","shell.execute_reply.started":"2022-07-11T16:20:45.704627Z","shell.execute_reply":"2022-07-11T16:20:45.714253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"There is significant variation of median values of Pclass with Age.","metadata":{}},{"cell_type":"code","source":"def setMissingAgeTest(cols):\n    Age = cols[0]\n    Pclass = cols[1]\n    if pd.isnull(Age):\n        if Pclass == 1:\n            return 42\n        elif Pclass == 2:\n            return 26\n        else:\n            return 24\n    else:\n        return Age\ntest[\"Age\"] = test[[\"Age\",\"Pclass\"]].apply(setMissingAgeTest,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.716841Z","iopub.execute_input":"2022-07-11T16:20:45.717491Z","iopub.status.idle":"2022-07-11T16:20:45.737810Z","shell.execute_reply.started":"2022-07-11T16:20:45.717457Z","shell.execute_reply":"2022-07-11T16:20:45.736702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Filling missing values in Fare**","metadata":{}},{"cell_type":"code","source":"print(test.groupby(\"Pclass\")[\"Fare\"].median())\nsns.boxplot(y=test.Pclass,x=test.Fare,orient='h')","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:45.739402Z","iopub.execute_input":"2022-07-11T16:20:45.740016Z","iopub.status.idle":"2022-07-11T16:20:46.013641Z","shell.execute_reply.started":"2022-07-11T16:20:45.739981Z","shell.execute_reply":"2022-07-11T16:20:46.012680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[pd.isnull(test[\"Fare\"])]","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.015240Z","iopub.execute_input":"2022-07-11T16:20:46.015811Z","iopub.status.idle":"2022-07-11T16:20:46.031372Z","shell.execute_reply.started":"2022-07-11T16:20:46.015771Z","shell.execute_reply":"2022-07-11T16:20:46.030426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# As this passenger belongs to Pclass 3 assigning fare = 7.8958 .i.e median of Pclass 3\ntest.loc[pd.isnull(test[\"Fare\"]),\"Fare\"] = 7.8958","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.032602Z","iopub.execute_input":"2022-07-11T16:20:46.033341Z","iopub.status.idle":"2022-07-11T16:20:46.045323Z","shell.execute_reply.started":"2022-07-11T16:20:46.033304Z","shell.execute_reply":"2022-07-11T16:20:46.044544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop(\"Cabin\",axis=1,inplace =True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.046530Z","iopub.execute_input":"2022-07-11T16:20:46.047301Z","iopub.status.idle":"2022-07-11T16:20:46.062315Z","shell.execute_reply.started":"2022-07-11T16:20:46.047269Z","shell.execute_reply":"2022-07-11T16:20:46.061474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.064005Z","iopub.execute_input":"2022-07-11T16:20:46.064556Z","iopub.status.idle":"2022-07-11T16:20:46.083958Z","shell.execute_reply.started":"2022-07-11T16:20:46.064446Z","shell.execute_reply":"2022-07-11T16:20:46.082974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*No null value present in test data*","metadata":{}},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"# Relation of Sex with Passenger Survival\nax = sns.countplot(x=train.Sex,hue=train.Survived,palette=\"Pastel1\")\nax.bar_label(ax.containers[0])\nax.bar_label(ax.containers[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.085583Z","iopub.execute_input":"2022-07-11T16:20:46.086248Z","iopub.status.idle":"2022-07-11T16:20:46.325606Z","shell.execute_reply.started":"2022-07-11T16:20:46.086192Z","shell.execute_reply":"2022-07-11T16:20:46.324854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Female passengers have high survival rate compared to male**","metadata":{"execution":{"iopub.status.busy":"2022-07-11T14:42:09.272948Z","iopub.execute_input":"2022-07-11T14:42:09.273379Z","iopub.status.idle":"2022-07-11T14:42:09.279546Z","shell.execute_reply.started":"2022-07-11T14:42:09.273344Z","shell.execute_reply":"2022-07-11T14:42:09.278229Z"}}},{"cell_type":"code","source":"# Relation of Pclass with Passenger Survival\nax = sns.countplot(x = train.Pclass, hue = train.Survived,palette=\"Pastel1\")\nax.bar_label(ax.containers[0])\nax.bar_label(ax.containers[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.326899Z","iopub.execute_input":"2022-07-11T16:20:46.327416Z","iopub.status.idle":"2022-07-11T16:20:46.590364Z","shell.execute_reply.started":"2022-07-11T16:20:46.327377Z","shell.execute_reply":"2022-07-11T16:20:46.589554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Passengers from **Pclass 3 have survived least**\n* **Pclass 1 passengers have highest survival rate**","metadata":{}},{"cell_type":"code","source":"# How travelling with family affected survival rate ?\nax = sns.countplot(x = train.SibSp, hue = train.Survived,palette=\"Pastel1\")\nax.bar_label(ax.containers[0])\nax.bar_label(ax.containers[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.591662Z","iopub.execute_input":"2022-07-11T16:20:46.592268Z","iopub.status.idle":"2022-07-11T16:20:46.940920Z","shell.execute_reply.started":"2022-07-11T16:20:46.592231Z","shell.execute_reply":"2022-07-11T16:20:46.939894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(x = train.Parch, hue = train.Survived,palette=\"Pastel1\")\nax.bar_label(ax.containers[0])\nax.bar_label(ax.containers[1])","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:46.945042Z","iopub.execute_input":"2022-07-11T16:20:46.945428Z","iopub.status.idle":"2022-07-11T16:20:47.283035Z","shell.execute_reply.started":"2022-07-11T16:20:46.945396Z","shell.execute_reply":"2022-07-11T16:20:47.281823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating new features \"relatives\" and \"travelled_alone\" to determine how travelling alone or with family affected Survival ?\ntrain[\"relatives\"] = train[\"SibSp\"] + train[\"Parch\"]\ntrain.loc[train['relatives'] > 0,'travelled_alone'] = 0\ntrain.loc[train['relatives'] == 0, 'travelled_alone'] = 1\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:47.284225Z","iopub.execute_input":"2022-07-11T16:20:47.285053Z","iopub.status.idle":"2022-07-11T16:20:47.310384Z","shell.execute_reply.started":"2022-07-11T16:20:47.285013Z","shell.execute_reply":"2022-07-11T16:20:47.309098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating new features \"relatives\" and \"travelled_alone\" to determine how travelling alone or with family affected Survival ?\ntest[\"relatives\"] = test[\"SibSp\"] + test[\"Parch\"]\ntest.loc[test['relatives'] > 0,'travelled_alone'] = 0\ntest.loc[test['relatives'] == 0, 'travelled_alone'] = 1\ntest","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:47.311619Z","iopub.execute_input":"2022-07-11T16:20:47.312283Z","iopub.status.idle":"2022-07-11T16:20:47.342772Z","shell.execute_reply.started":"2022-07-11T16:20:47.312247Z","shell.execute_reply":"2022-07-11T16:20:47.341523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.barplot(x = 'relatives',y = 'Survived',data = train)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:47.344263Z","iopub.execute_input":"2022-07-11T16:20:47.344858Z","iopub.status.idle":"2022-07-11T16:20:47.889399Z","shell.execute_reply.started":"2022-07-11T16:20:47.344817Z","shell.execute_reply":"2022-07-11T16:20:47.888274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x = train.travelled_alone,hue = train.Survived)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:47.890869Z","iopub.execute_input":"2022-07-11T16:20:47.891260Z","iopub.status.idle":"2022-07-11T16:20:48.125875Z","shell.execute_reply.started":"2022-07-11T16:20:47.891224Z","shell.execute_reply":"2022-07-11T16:20:48.124847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Conclusion**\n* Passengers who travelled with **1 to 3 relatives** have survived most.\n* Passengers who **travelled alone have very less survival rate**.","metadata":{}},{"cell_type":"code","source":"# Effect of fare\nsns.displot(x = train.Fare,row = train.Survived,kde = True,col = train.Pclass)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:48.127206Z","iopub.execute_input":"2022-07-11T16:20:48.127780Z","iopub.status.idle":"2022-07-11T16:20:50.732827Z","shell.execute_reply.started":"2022-07-11T16:20:48.127718Z","shell.execute_reply":"2022-07-11T16:20:50.731973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Fare varies with passenger class\n* Passengers who died most belonged to Pclass 3.","metadata":{}},{"cell_type":"code","source":"# Variation of Survival with Embarked\nsns.countplot(x = train.Embarked,hue =train.Survived)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:50.734163Z","iopub.execute_input":"2022-07-11T16:20:50.734722Z","iopub.status.idle":"2022-07-11T16:20:50.985725Z","shell.execute_reply.started":"2022-07-11T16:20:50.734687Z","shell.execute_reply":"2022-07-11T16:20:50.984716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"People who embarked from **Southampton** have least survival rate","metadata":{}},{"cell_type":"markdown","source":"## One-hot encoding","metadata":{}},{"cell_type":"code","source":"sex = pd.get_dummies(train[\"Sex\"])\nsex.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:50.987178Z","iopub.execute_input":"2022-07-11T16:20:50.988234Z","iopub.status.idle":"2022-07-11T16:20:51.002114Z","shell.execute_reply.started":"2022-07-11T16:20:50.988184Z","shell.execute_reply":"2022-07-11T16:20:51.000848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked = pd.get_dummies(train[\"Embarked\"])\nembarked.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.003609Z","iopub.execute_input":"2022-07-11T16:20:51.004592Z","iopub.status.idle":"2022-07-11T16:20:51.018408Z","shell.execute_reply.started":"2022-07-11T16:20:51.004538Z","shell.execute_reply":"2022-07-11T16:20:51.017331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Dropping columns which are not required\ntrain.drop([\"Sex\",\"Embarked\",\"Name\",\"Ticket\",\"SibSp\",\"Parch\"],axis=1,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.019543Z","iopub.execute_input":"2022-07-11T16:20:51.020856Z","iopub.status.idle":"2022-07-11T16:20:51.028540Z","shell.execute_reply.started":"2022-07-11T16:20:51.020720Z","shell.execute_reply":"2022-07-11T16:20:51.027712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.concat([train,sex,embarked],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.030256Z","iopub.execute_input":"2022-07-11T16:20:51.030909Z","iopub.status.idle":"2022-07-11T16:20:51.042799Z","shell.execute_reply.started":"2022-07-11T16:20:51.030839Z","shell.execute_reply":"2022-07-11T16:20:51.041984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sex_test = pd.get_dummies(test.Sex)\nsex_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.044262Z","iopub.execute_input":"2022-07-11T16:20:51.045109Z","iopub.status.idle":"2022-07-11T16:20:51.065314Z","shell.execute_reply.started":"2022-07-11T16:20:51.045076Z","shell.execute_reply":"2022-07-11T16:20:51.064478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embarked_test = pd.get_dummies(test.Embarked)\nembarked_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.066841Z","iopub.execute_input":"2022-07-11T16:20:51.067427Z","iopub.status.idle":"2022-07-11T16:20:51.083017Z","shell.execute_reply.started":"2022-07-11T16:20:51.067381Z","shell.execute_reply":"2022-07-11T16:20:51.082054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.drop([\"Sex\",\"Embarked\",\"Ticket\",\"Name\",\"Parch\",\"SibSp\"],axis=1,inplace=True)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.084324Z","iopub.execute_input":"2022-07-11T16:20:51.085034Z","iopub.status.idle":"2022-07-11T16:20:51.104620Z","shell.execute_reply.started":"2022-07-11T16:20:51.084998Z","shell.execute_reply":"2022-07-11T16:20:51.103859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.concat([test,sex_test,embarked_test],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.105877Z","iopub.execute_input":"2022-07-11T16:20:51.106393Z","iopub.status.idle":"2022-07-11T16:20:51.113212Z","shell.execute_reply.started":"2022-07-11T16:20:51.106358Z","shell.execute_reply":"2022-07-11T16:20:51.112311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.114688Z","iopub.execute_input":"2022-07-11T16:20:51.115181Z","iopub.status.idle":"2022-07-11T16:20:51.136983Z","shell.execute_reply.started":"2022-07-11T16:20:51.115146Z","shell.execute_reply":"2022-07-11T16:20:51.136012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.138209Z","iopub.execute_input":"2022-07-11T16:20:51.139019Z","iopub.status.idle":"2022-07-11T16:20:51.156668Z","shell.execute_reply.started":"2022-07-11T16:20:51.138975Z","shell.execute_reply":"2022-07-11T16:20:51.155469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building Logistic Regression model","metadata":{}},{"cell_type":"code","source":"X_train = train.drop(\"Survived\",axis= 1)\nX_train.head()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-07-11T16:20:51.158309Z","iopub.execute_input":"2022-07-11T16:20:51.159453Z","iopub.status.idle":"2022-07-11T16:20:51.182913Z","shell.execute_reply.started":"2022-07-11T16:20:51.159407Z","shell.execute_reply":"2022-07-11T16:20:51.181827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train[\"Survived\"]\ny_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:20:51.184316Z","iopub.execute_input":"2022-07-11T16:20:51.184783Z","iopub.status.idle":"2022-07-11T16:20:51.199297Z","shell.execute_reply.started":"2022-07-11T16:20:51.184721Z","shell.execute_reply":"2022-07-11T16:20:51.198537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Logistic Regression\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:21:58.692620Z","iopub.execute_input":"2022-07-11T16:21:58.693071Z","iopub.status.idle":"2022-07-11T16:21:58.698032Z","shell.execute_reply.started":"2022-07-11T16:21:58.693035Z","shell.execute_reply":"2022-07-11T16:21:58.696938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier()\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:22:08.838235Z","iopub.execute_input":"2022-07-11T16:22:08.838875Z","iopub.status.idle":"2022-07-11T16:22:09.097991Z","shell.execute_reply.started":"2022-07-11T16:22:08.838844Z","shell.execute_reply":"2022-07-11T16:22:09.096981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:22:11.940626Z","iopub.execute_input":"2022-07-11T16:22:11.941065Z","iopub.status.idle":"2022-07-11T16:22:11.971665Z","shell.execute_reply.started":"2022-07-11T16:22:11.941029Z","shell.execute_reply":"2022-07-11T16:22:11.970634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test.PassengerId, 'Survived': predictions})\noutput.to_csv('titanic_submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:22:47.037086Z","iopub.execute_input":"2022-07-11T16:22:47.037509Z","iopub.status.idle":"2022-07-11T16:22:47.046912Z","shell.execute_reply.started":"2022-07-11T16:22:47.037473Z","shell.execute_reply":"2022-07-11T16:22:47.045936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}