{"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)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\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-08-07T10:47:42.874721Z","iopub.execute_input":"2022-08-07T10:47:42.875880Z","iopub.status.idle":"2022-08-07T10:47:44.147155Z","shell.execute_reply.started":"2022-08-07T10:47:42.875782Z","shell.execute_reply":"2022-08-07T10:47:44.145529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"DATA ANALYSIS","metadata":{}},{"cell_type":"code","source":"#reading data\ntraindf=pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntestdf=pd.read_csv(\"/kaggle/input/titanic/test.csv\")\nfinaldf=pd.read_csv(\"/kaggle/input/titanic/gender_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.150063Z","iopub.execute_input":"2022-08-07T10:47:44.150532Z","iopub.status.idle":"2022-08-07T10:47:44.187101Z","shell.execute_reply.started":"2022-08-07T10:47:44.150492Z","shell.execute_reply":"2022-08-07T10:47:44.186070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.188373Z","iopub.execute_input":"2022-08-07T10:47:44.188725Z","iopub.status.idle":"2022-08-07T10:47:44.214487Z","shell.execute_reply.started":"2022-08-07T10:47:44.188696Z","shell.execute_reply":"2022-08-07T10:47:44.213677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking missing values in training data**","metadata":{}},{"cell_type":"code","source":"traindf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.215400Z","iopub.execute_input":"2022-08-07T10:47:44.215759Z","iopub.status.idle":"2022-08-07T10:47:44.226867Z","shell.execute_reply.started":"2022-08-07T10:47:44.215730Z","shell.execute_reply":"2022-08-07T10:47:44.225589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Handling missing values in training data:**\nfor numeric column like Age,fill the missing values with median and for categorical columns with mode.Since there can be more than 1 mode in a data,I filled it with the first value in the mode.","metadata":{}},{"cell_type":"code","source":"traindf.loc[traindf[\"Age\"].isna()==True,\"Age\"]=traindf[\"Age\"].median()\ntraindf.loc[traindf[\"Cabin\"].isna()==True,\"Cabin\"]=traindf[\"Cabin\"].mode()[0]\ntraindf.loc[traindf[\"Embarked\"].isna()==True,\"Embarked\"]=traindf[\"Embarked\"].mode()[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.229624Z","iopub.execute_input":"2022-08-07T10:47:44.230010Z","iopub.status.idle":"2022-08-07T10:47:44.252230Z","shell.execute_reply.started":"2022-08-07T10:47:44.229975Z","shell.execute_reply":"2022-08-07T10:47:44.250872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.254012Z","iopub.execute_input":"2022-08-07T10:47:44.254366Z","iopub.status.idle":"2022-08-07T10:47:44.265827Z","shell.execute_reply.started":"2022-08-07T10:47:44.254335Z","shell.execute_reply":"2022-08-07T10:47:44.264822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.267376Z","iopub.execute_input":"2022-08-07T10:47:44.267747Z","iopub.status.idle":"2022-08-07T10:47:44.280350Z","shell.execute_reply.started":"2022-08-07T10:47:44.267716Z","shell.execute_reply":"2022-08-07T10:47:44.279122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Handling missing values in testing data**","metadata":{}},{"cell_type":"code","source":"testdf.loc[testdf[\"Age\"].isna()==True,\"Age\"]=testdf[\"Age\"].median()\ntestdf.loc[testdf[\"Fare\"].isna()==True,\"Fare\"]=testdf[\"Fare\"].median()\ntestdf.loc[testdf[\"Cabin\"].isna()==True,\"Cabin\"]=testdf[\"Cabin\"].mode()[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.282012Z","iopub.execute_input":"2022-08-07T10:47:44.282909Z","iopub.status.idle":"2022-08-07T10:47:44.294047Z","shell.execute_reply.started":"2022-08-07T10:47:44.282875Z","shell.execute_reply":"2022-08-07T10:47:44.292870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.295344Z","iopub.execute_input":"2022-08-07T10:47:44.295741Z","iopub.status.idle":"2022-08-07T10:47:44.307990Z","shell.execute_reply.started":"2022-08-07T10:47:44.295709Z","shell.execute_reply":"2022-08-07T10:47:44.306660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Separating numeric and categorical columns**","metadata":{}},{"cell_type":"code","source":"numdf=traindf.select_dtypes([\"int64\",\"float64\"])\ncatdf=traindf.select_dtypes([\"object\"])","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.309386Z","iopub.execute_input":"2022-08-07T10:47:44.309762Z","iopub.status.idle":"2022-08-07T10:47:44.328031Z","shell.execute_reply.started":"2022-08-07T10:47:44.309731Z","shell.execute_reply":"2022-08-07T10:47:44.326478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"numdf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.329712Z","iopub.execute_input":"2022-08-07T10:47:44.330081Z","iopub.status.idle":"2022-08-07T10:47:44.350541Z","shell.execute_reply.started":"2022-08-07T10:47:44.330049Z","shell.execute_reply":"2022-08-07T10:47:44.349186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catdf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.351741Z","iopub.execute_input":"2022-08-07T10:47:44.352150Z","iopub.status.idle":"2022-08-07T10:47:44.369372Z","shell.execute_reply.started":"2022-08-07T10:47:44.352117Z","shell.execute_reply":"2022-08-07T10:47:44.368078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catdf.drop(columns=[\"Name\",\"Ticket\",\"Cabin\"],inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.371172Z","iopub.execute_input":"2022-08-07T10:47:44.371567Z","iopub.status.idle":"2022-08-07T10:47:44.382287Z","shell.execute_reply.started":"2022-08-07T10:47:44.371538Z","shell.execute_reply":"2022-08-07T10:47:44.381292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catdf[\"Sex\"]=catdf[\"Sex\"].map({\"male\":0,\"female\":1})\ncatdf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.386391Z","iopub.execute_input":"2022-08-07T10:47:44.386938Z","iopub.status.idle":"2022-08-07T10:47:44.403007Z","shell.execute_reply.started":"2022-08-07T10:47:44.386905Z","shell.execute_reply":"2022-08-07T10:47:44.401785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catdf[\"Embarked\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.404331Z","iopub.execute_input":"2022-08-07T10:47:44.405198Z","iopub.status.idle":"2022-08-07T10:47:44.415576Z","shell.execute_reply.started":"2022-08-07T10:47:44.405160Z","shell.execute_reply":"2022-08-07T10:47:44.414002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"catdf[\"Embarked\"]=catdf[\"Embarked\"].map({\"S\":0,\"C\":1,\"Q\":2})\ncatdf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.417880Z","iopub.execute_input":"2022-08-07T10:47:44.418430Z","iopub.status.idle":"2022-08-07T10:47:44.432931Z","shell.execute_reply.started":"2022-08-07T10:47:44.418385Z","shell.execute_reply":"2022-08-07T10:47:44.431914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"traindf2=pd.concat([numdf,catdf],axis=1)\ntraindf2.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.434343Z","iopub.execute_input":"2022-08-07T10:47:44.434765Z","iopub.status.idle":"2022-08-07T10:47:44.457823Z","shell.execute_reply.started":"2022-08-07T10:47:44.434724Z","shell.execute_reply":"2022-08-07T10:47:44.456544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=list(traindf2.columns)\na.remove(\"Survived\")\ntestdf2=testdf[a]\ntestdf2.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.459495Z","iopub.execute_input":"2022-08-07T10:47:44.460216Z","iopub.status.idle":"2022-08-07T10:47:44.480930Z","shell.execute_reply.started":"2022-08-07T10:47:44.460172Z","shell.execute_reply":"2022-08-07T10:47:44.479892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdf2[\"Embarked\"]=testdf2[\"Embarked\"].map({\"S\":0,\"C\":1,\"Q\":2})\ntestdf2[\"Sex\"]=testdf2[\"Sex\"].map({\"male\":0,\"female\":1})\ntestdf2.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.482953Z","iopub.execute_input":"2022-08-07T10:47:44.484624Z","iopub.status.idle":"2022-08-07T10:47:44.506122Z","shell.execute_reply.started":"2022-08-07T10:47:44.484548Z","shell.execute_reply":"2022-08-07T10:47:44.504822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**heatmap to test the relationship between columns**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,7))\nsns.heatmap(traindf2.corr(),annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:44.508076Z","iopub.execute_input":"2022-08-07T10:47:44.508542Z","iopub.status.idle":"2022-08-07T10:47:45.195148Z","shell.execute_reply.started":"2022-08-07T10:47:44.508499Z","shell.execute_reply":"2022-08-07T10:47:45.193681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**columns with very less relationship with survived:**\nPassengerId,Age,SibSp and Parch\n","metadata":{}},{"cell_type":"code","source":"#deleting the columns with very less relationship\nxtrain=traindf2[[\"Pclass\",\"Fare\",\"Sex\",\"Embarked\",\"Age\"]]\nytrain=traindf2[\"Survived\"]","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:45.197582Z","iopub.execute_input":"2022-08-07T10:47:45.198520Z","iopub.status.idle":"2022-08-07T10:47:45.207713Z","shell.execute_reply.started":"2022-08-07T10:47:45.198470Z","shell.execute_reply":"2022-08-07T10:47:45.206369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Predictions using Random Forest classifier**","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrfc=RandomForestClassifier(n_jobs=-1)\nrfc.fit(xtrain,ytrain)\nrfc.score(xtrain,ytrain)","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:45.209952Z","iopub.execute_input":"2022-08-07T10:47:45.210848Z","iopub.status.idle":"2022-08-07T10:47:46.143370Z","shell.execute_reply.started":"2022-08-07T10:47:45.210778Z","shell.execute_reply":"2022-08-07T10:47:46.141915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytrain_pred=pd.Series(rfc.predict(xtrain))\nytrain_pred.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.145533Z","iopub.execute_input":"2022-08-07T10:47:46.146141Z","iopub.status.idle":"2022-08-07T10:47:46.261820Z","shell.execute_reply.started":"2022-08-07T10:47:46.146093Z","shell.execute_reply":"2022-08-07T10:47:46.260679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model Evaluation using confusion matrix and classification report**","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix,classification_report\nprint(confusion_matrix(ytrain,ytrain_pred))\nprint(classification_report(ytrain,ytrain_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.262974Z","iopub.execute_input":"2022-08-07T10:47:46.263732Z","iopub.status.idle":"2022-08-07T10:47:46.281354Z","shell.execute_reply.started":"2022-08-07T10:47:46.263697Z","shell.execute_reply":"2022-08-07T10:47:46.279929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The model looks decent.","metadata":{}},{"cell_type":"code","source":"ytestpred=pd.Series(rfc.predict(testdf2[xtrain.columns]))\nytestpred.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.283430Z","iopub.execute_input":"2022-08-07T10:47:46.283875Z","iopub.status.idle":"2022-08-07T10:47:46.401382Z","shell.execute_reply.started":"2022-08-07T10:47:46.283832Z","shell.execute_reply":"2022-08-07T10:47:46.400180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(confusion_matrix(finaldf.Survived,ytestpred))\nprint(classification_report(finaldf.Survived,ytestpred))","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.403035Z","iopub.execute_input":"2022-08-07T10:47:46.404896Z","iopub.status.idle":"2022-08-07T10:47:46.418208Z","shell.execute_reply.started":"2022-08-07T10:47:46.404854Z","shell.execute_reply":"2022-08-07T10:47:46.416796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.concat([testdf.PassengerId,ytestpred],axis=1)\ndf.rename(columns={0:\"Survived\"},inplace=True)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.420116Z","iopub.execute_input":"2022-08-07T10:47:46.420834Z","iopub.status.idle":"2022-08-07T10:47:46.437392Z","shell.execute_reply.started":"2022-08-07T10:47:46.420793Z","shell.execute_reply":"2022-08-07T10:47:46.435119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"recpredictions.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-07T10:47:46.439650Z","iopub.execute_input":"2022-08-07T10:47:46.440825Z","iopub.status.idle":"2022-08-07T10:47:46.449651Z","shell.execute_reply.started":"2022-08-07T10:47:46.440748Z","shell.execute_reply":"2022-08-07T10:47:46.448674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}