{"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\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-14T05:34:53.227209Z","iopub.execute_input":"2022-07-14T05:34:53.227643Z","iopub.status.idle":"2022-07-14T05:34:53.236961Z","shell.execute_reply.started":"2022-07-14T05:34:53.227605Z","shell.execute_reply":"2022-07-14T05:34:53.236143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Loading the Train data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/titanic/train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.372619Z","iopub.execute_input":"2022-07-14T05:34:53.373210Z","iopub.status.idle":"2022-07-14T05:34:53.396745Z","shell.execute_reply.started":"2022-07-14T05:34:53.373178Z","shell.execute_reply":"2022-07-14T05:34:53.395582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Loading the test data","metadata":{}},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/titanic/test.csv\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.398220Z","iopub.execute_input":"2022-07-14T05:34:53.398549Z","iopub.status.idle":"2022-07-14T05:34:53.420813Z","shell.execute_reply.started":"2022-07-14T05:34:53.398521Z","shell.execute_reply":"2022-07-14T05:34:53.419404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.669061Z","iopub.execute_input":"2022-07-14T05:34:53.669459Z","iopub.status.idle":"2022-07-14T05:34:53.676475Z","shell.execute_reply.started":"2022-07-14T05:34:53.669427Z","shell.execute_reply":"2022-07-14T05:34:53.675099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.820143Z","iopub.execute_input":"2022-07-14T05:34:53.821313Z","iopub.status.idle":"2022-07-14T05:34:53.827312Z","shell.execute_reply.started":"2022-07-14T05:34:53.821268Z","shell.execute_reply":"2022-07-14T05:34:53.826353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploratory Data Analysis","metadata":{}},{"cell_type":"markdown","source":"#### Information of the dataFrame","metadata":{}},{"cell_type":"code","source":"df = pd.concat([train_df,test_df],axis =0)\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.969683Z","iopub.execute_input":"2022-07-14T05:34:53.970272Z","iopub.status.idle":"2022-07-14T05:34:53.991088Z","shell.execute_reply.started":"2022-07-14T05:34:53.970240Z","shell.execute_reply":"2022-07-14T05:34:53.989865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:53.993110Z","iopub.execute_input":"2022-07-14T05:34:53.993503Z","iopub.status.idle":"2022-07-14T05:34:54.012981Z","shell.execute_reply.started":"2022-07-14T05:34:53.993470Z","shell.execute_reply":"2022-07-14T05:34:54.012203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:54.115712Z","iopub.execute_input":"2022-07-14T05:34:54.116590Z","iopub.status.idle":"2022-07-14T05:34:54.128239Z","shell.execute_reply.started":"2022-07-14T05:34:54.116545Z","shell.execute_reply":"2022-07-14T05:34:54.127163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['Age'].fillna(df['Age'].mean(),inplace = True)\ndf['Embarked'].fillna(df['Embarked'].mode()[0],inplace = True)\ndf['Fare'].fillna(df['Fare'].mean(),inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:54.265948Z","iopub.execute_input":"2022-07-14T05:34:54.266359Z","iopub.status.idle":"2022-07-14T05:34:54.275246Z","shell.execute_reply.started":"2022-07-14T05:34:54.266310Z","shell.execute_reply":"2022-07-14T05:34:54.274390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.hist(figsize = (17,15),color = 'y',edgecolor = 'r')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:54.420089Z","iopub.execute_input":"2022-07-14T05:34:54.420543Z","iopub.status.idle":"2022-07-14T05:34:55.369332Z","shell.execute_reply.started":"2022-07-14T05:34:54.420504Z","shell.execute_reply":"2022-07-14T05:34:55.368425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.drop(['Name','Cabin','Ticket'],axis = 1,inplace = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.370892Z","iopub.execute_input":"2022-07-14T05:34:55.371385Z","iopub.status.idle":"2022-07-14T05:34:55.376801Z","shell.execute_reply.started":"2022-07-14T05:34:55.371354Z","shell.execute_reply":"2022-07-14T05:34:55.375714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in df.columns:\n    if(df[i].dtype == 'object'):\n        print(i)\n        df[i].hist(figsize = (4,4),color = 'y',edgecolor = 'r')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.378007Z","iopub.execute_input":"2022-07-14T05:34:55.378474Z","iopub.status.idle":"2022-07-14T05:34:55.604862Z","shell.execute_reply.started":"2022-07-14T05:34:55.378445Z","shell.execute_reply":"2022-07-14T05:34:55.604008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle  = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.607573Z","iopub.execute_input":"2022-07-14T05:34:55.608965Z","iopub.status.idle":"2022-07-14T05:34:55.614640Z","shell.execute_reply.started":"2022-07-14T05:34:55.608911Z","shell.execute_reply":"2022-07-14T05:34:55.613347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Sex\"] = le.fit_transform(df[\"Sex\"])\ndf.Embarked = le.fit_transform(df.Embarked)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.616009Z","iopub.execute_input":"2022-07-14T05:34:55.616818Z","iopub.status.idle":"2022-07-14T05:34:55.630044Z","shell.execute_reply.started":"2022-07-14T05:34:55.616771Z","shell.execute_reply":"2022-07-14T05:34:55.628694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.632056Z","iopub.execute_input":"2022-07-14T05:34:55.632932Z","iopub.status.idle":"2022-07-14T05:34:55.654844Z","shell.execute_reply.started":"2022-07-14T05:34:55.632879Z","shell.execute_reply":"2022-07-14T05:34:55.653674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.656053Z","iopub.execute_input":"2022-07-14T05:34:55.656558Z","iopub.status.idle":"2022-07-14T05:34:55.661853Z","shell.execute_reply.started":"2022-07-14T05:34:55.656524Z","shell.execute_reply":"2022-07-14T05:34:55.661065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in ['PassengerId', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch','Fare', 'Embarked']:\n    print(i)\n    sns.kdeplot(x=i, data=df[df['Survived'].notnull()], hue='Survived')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:55.663222Z","iopub.execute_input":"2022-07-14T05:34:55.663583Z","iopub.status.idle":"2022-07-14T05:34:57.358945Z","shell.execute_reply.started":"2022-07-14T05:34:55.663554Z","shell.execute_reply":"2022-07-14T05:34:57.358101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df[df['Survived'].notnull()]\ntest_df = df[df['Survived'].isnull()]","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.360262Z","iopub.execute_input":"2022-07-14T05:34:57.361072Z","iopub.status.idle":"2022-07-14T05:34:57.367726Z","shell.execute_reply.started":"2022-07-14T05:34:57.361034Z","shell.execute_reply":"2022-07-14T05:34:57.366758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.drop(['Survived'],axis =1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.371151Z","iopub.execute_input":"2022-07-14T05:34:57.371796Z","iopub.status.idle":"2022-07-14T05:34:57.381362Z","shell.execute_reply.started":"2022-07-14T05:34:57.371764Z","shell.execute_reply":"2022-07-14T05:34:57.380384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.382668Z","iopub.execute_input":"2022-07-14T05:34:57.386019Z","iopub.status.idle":"2022-07-14T05:34:57.405386Z","shell.execute_reply.started":"2022-07-14T05:34:57.385986Z","shell.execute_reply":"2022-07-14T05:34:57.404246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.406925Z","iopub.execute_input":"2022-07-14T05:34:57.408593Z","iopub.status.idle":"2022-07-14T05:34:57.424016Z","shell.execute_reply.started":"2022-07-14T05:34:57.408558Z","shell.execute_reply":"2022-07-14T05:34:57.422850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train_df.drop(['Survived','PassengerId'],axis =1)   # PassengerId is not usefull in the survival prediction\ny = train_df.Survived","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.425314Z","iopub.execute_input":"2022-07-14T05:34:57.425963Z","iopub.status.idle":"2022-07-14T05:34:57.435633Z","shell.execute_reply.started":"2022-07-14T05:34:57.425929Z","shell.execute_reply":"2022-07-14T05:34:57.434670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.436545Z","iopub.execute_input":"2022-07-14T05:34:57.436865Z","iopub.status.idle":"2022-07-14T05:34:57.459094Z","shell.execute_reply.started":"2022-07-14T05:34:57.436839Z","shell.execute_reply":"2022-07-14T05:34:57.458390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x = test_df.drop(['PassengerId'],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.460165Z","iopub.execute_input":"2022-07-14T05:34:57.460880Z","iopub.status.idle":"2022-07-14T05:34:57.465897Z","shell.execute_reply.started":"2022-07-14T05:34:57.460846Z","shell.execute_reply":"2022-07-14T05:34:57.465198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.467096Z","iopub.execute_input":"2022-07-14T05:34:57.467604Z","iopub.status.idle":"2022-07-14T05:34:57.481821Z","shell.execute_reply.started":"2022-07-14T05:34:57.467572Z","shell.execute_reply":"2022-07-14T05:34:57.480803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model : RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nrandom_forest = RandomForestClassifier(n_estimators = 120,max_depth =7)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.482894Z","iopub.execute_input":"2022-07-14T05:34:57.483801Z","iopub.status.idle":"2022-07-14T05:34:57.488806Z","shell.execute_reply.started":"2022-07-14T05:34:57.483767Z","shell.execute_reply":"2022-07-14T05:34:57.487996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest.fit(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.489982Z","iopub.execute_input":"2022-07-14T05:34:57.490459Z","iopub.status.idle":"2022-07-14T05:34:57.683783Z","shell.execute_reply.started":"2022-07-14T05:34:57.490428Z","shell.execute_reply":"2022-07-14T05:34:57.682785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest.score(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.685270Z","iopub.execute_input":"2022-07-14T05:34:57.685893Z","iopub.status.idle":"2022-07-14T05:34:57.720697Z","shell.execute_reply.started":"2022-07-14T05:34:57.685850Z","shell.execute_reply":"2022-07-14T05:34:57.719602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest.predict(test_x)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.766774Z","iopub.execute_input":"2022-07-14T05:34:57.767063Z","iopub.status.idle":"2022-07-14T05:34:57.799137Z","shell.execute_reply.started":"2022-07-14T05:34:57.767037Z","shell.execute_reply":"2022-07-14T05:34:57.798374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(test_df.PassengerId)","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.800312Z","iopub.execute_input":"2022-07-14T05:34:57.800842Z","iopub.status.idle":"2022-07-14T05:34:57.805722Z","shell.execute_reply.started":"2022-07-14T05:34:57.800809Z","shell.execute_reply":"2022-07-14T05:34:57.804890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result['Survived'] = pd.DataFrame(random_forest.predict(test_x))","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.806768Z","iopub.execute_input":"2022-07-14T05:34:57.807480Z","iopub.status.idle":"2022-07-14T05:34:57.840273Z","shell.execute_reply.started":"2022-07-14T05:34:57.807450Z","shell.execute_reply":"2022-07-14T05:34:57.839163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result['Survived']","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_forest.score(test_x,result['Survived'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.841623Z","iopub.execute_input":"2022-07-14T05:34:57.841941Z","iopub.status.idle":"2022-07-14T05:34:57.851621Z","shell.execute_reply.started":"2022-07-14T05:34:57.841914Z","shell.execute_reply":"2022-07-14T05:34:57.850648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.Survived = result.Survived.astype('int')","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:42:36.515584Z","iopub.execute_input":"2022-07-14T05:42:36.515991Z","iopub.status.idle":"2022-07-14T05:42:36.522299Z","shell.execute_reply.started":"2022-07-14T05:42:36.515960Z","shell.execute_reply":"2022-07-14T05:42:36.520780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:42:45.491959Z","iopub.execute_input":"2022-07-14T05:42:45.492612Z","iopub.status.idle":"2022-07-14T05:42:45.502691Z","shell.execute_reply.started":"2022-07-14T05:42:45.492577Z","shell.execute_reply":"2022-07-14T05:42:45.501384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('my_submission.csv', index=False)\nprint(\"Your submission was successfully saved!\")\n","metadata":{"execution":{"iopub.status.busy":"2022-07-14T05:34:57.852926Z","iopub.execute_input":"2022-07-14T05:34:57.853772Z","iopub.status.idle":"2022-07-14T05:34:57.865279Z","shell.execute_reply.started":"2022-07-14T05:34:57.853739Z","shell.execute_reply":"2022-07-14T05:34:57.864565Z"},"trusted":true},"execution_count":null,"outputs":[]}]}