{"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":"#!kaggle competitions download -c titanic","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.089527Z","iopub.execute_input":"2022-07-29T10:19:07.090368Z","iopub.status.idle":"2022-07-29T10:19:07.095721Z","shell.execute_reply.started":"2022-07-29T10:19:07.090318Z","shell.execute_reply":"2022-07-29T10:19:07.094820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from zipfile import ZipFile","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.097428Z","iopub.execute_input":"2022-07-29T10:19:07.098209Z","iopub.status.idle":"2022-07-29T10:19:07.107074Z","shell.execute_reply.started":"2022-07-29T10:19:07.098169Z","shell.execute_reply":"2022-07-29T10:19:07.105825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with ZipFile('titanic.zip', 'r') as zipObj:\n#    zipObj.extractall()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.109421Z","iopub.execute_input":"2022-07-29T10:19:07.110156Z","iopub.status.idle":"2022-07-29T10:19:07.118801Z","shell.execute_reply.started":"2022-07-29T10:19:07.110110Z","shell.execute_reply":"2022-07-29T10:19:07.117444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.120592Z","iopub.execute_input":"2022-07-29T10:19:07.120967Z","iopub.status.idle":"2022-07-29T10:19:07.129666Z","shell.execute_reply.started":"2022-07-29T10:19:07.120934Z","shell.execute_reply":"2022-07-29T10:19:07.128683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_theme()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.131177Z","iopub.execute_input":"2022-07-29T10:19:07.132002Z","iopub.status.idle":"2022-07-29T10:19:07.142205Z","shell.execute_reply.started":"2022-07-29T10:19:07.131960Z","shell.execute_reply":"2022-07-29T10:19:07.141140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/titanic/train.csv')\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.144040Z","iopub.execute_input":"2022-07-29T10:19:07.144660Z","iopub.status.idle":"2022-07-29T10:19:07.186774Z","shell.execute_reply.started":"2022-07-29T10:19:07.144597Z","shell.execute_reply":"2022-07-29T10:19:07.185313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.189023Z","iopub.execute_input":"2022-07-29T10:19:07.189763Z","iopub.status.idle":"2022-07-29T10:19:07.203376Z","shell.execute_reply.started":"2022-07-29T10:19:07.189701Z","shell.execute_reply":"2022-07-29T10:19:07.201202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.205346Z","iopub.execute_input":"2022-07-29T10:19:07.205935Z","iopub.status.idle":"2022-07-29T10:19:07.240141Z","shell.execute_reply.started":"2022-07-29T10:19:07.205841Z","shell.execute_reply":"2022-07-29T10:19:07.239151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.242431Z","iopub.execute_input":"2022-07-29T10:19:07.243075Z","iopub.status.idle":"2022-07-29T10:19:07.284519Z","shell.execute_reply.started":"2022-07-29T10:19:07.243030Z","shell.execute_reply":"2022-07-29T10:19:07.283624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.285986Z","iopub.execute_input":"2022-07-29T10:19:07.286519Z","iopub.status.idle":"2022-07-29T10:19:07.298968Z","shell.execute_reply.started":"2022-07-29T10:19:07.286486Z","shell.execute_reply":"2022-07-29T10:19:07.297242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"null_age = train_df['Age'].isna()\nnull_embarked = train_df['Embarked'].isna()\n\ntrain_df['Age'].fillna(train_df['Age'].median(), inplace=True)\ntrain_df['Age'].fillna(train_df['Embarked'].mode()[0], inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.300851Z","iopub.execute_input":"2022-07-29T10:19:07.302095Z","iopub.status.idle":"2022-07-29T10:19:07.312149Z","shell.execute_reply.started":"2022-07-29T10:19:07.302050Z","shell.execute_reply":"2022-07-29T10:19:07.310917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.histplot(data=train_df, x=\"Age\", kde=True)\nplt.title(\"Distribution plot of ages\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.313781Z","iopub.execute_input":"2022-07-29T10:19:07.314778Z","iopub.status.idle":"2022-07-29T10:19:07.783418Z","shell.execute_reply.started":"2022-07-29T10:19:07.314739Z","shell.execute_reply":"2022-07-29T10:19:07.781700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.histplot(data=train_df, x=\"Fare\", kde=True)\nplt.title(\"Distribution plot of fare\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:07.785584Z","iopub.execute_input":"2022-07-29T10:19:07.786564Z","iopub.status.idle":"2022-07-29T10:19:08.253810Z","shell.execute_reply.started":"2022-07-29T10:19:07.786505Z","shell.execute_reply":"2022-07-29T10:19:08.252791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.countplot(data=train_df, x=\"Sex\", hue=\"Survived\")\nplt.legend(title=\"Survived or not\", labels=['No', 'Yes'])\nplt.title(\"Survived or not based on Sex\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:08.255297Z","iopub.execute_input":"2022-07-29T10:19:08.255643Z","iopub.status.idle":"2022-07-29T10:19:08.492081Z","shell.execute_reply.started":"2022-07-29T10:19:08.255598Z","shell.execute_reply":"2022-07-29T10:19:08.490533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.countplot(data=train_df, x=\"Pclass\", hue=\"Survived\")\nplt.legend(title=\"Survived or not\", labels=['No', 'Yes'])\nplt.title(\"Survived or not based on Pclass\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:08.496491Z","iopub.execute_input":"2022-07-29T10:19:08.496904Z","iopub.status.idle":"2022-07-29T10:19:08.764903Z","shell.execute_reply.started":"2022-07-29T10:19:08.496872Z","shell.execute_reply":"2022-07-29T10:19:08.763560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\ntemp = train_df.copy()\ntemp['Age'] = pd.cut(temp['Age'], bins=[0,12,20,40,120], labels=['Children','Teenage','Adult','Elder'])\nsns.countplot(data=temp, x=\"Age\", hue=\"Survived\")\nplt.legend(title=\"survived\", labels=['No', 'Yes'])\nplt.title(\"Survived or not based on fare\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:08.767147Z","iopub.execute_input":"2022-07-29T10:19:08.767530Z","iopub.status.idle":"2022-07-29T10:19:09.047400Z","shell.execute_reply.started":"2022-07-29T10:19:08.767489Z","shell.execute_reply":"2022-07-29T10:19:09.046060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\ntemp['Fare'] = pd.cut(temp['Fare'], bins=[0,8,16,32,110], labels=['Low_fare','Median_fare', 'Average_fare', 'High_fare'])\nsns.countplot(data=temp, x=\"Fare\", hue=\"Survived\")\nplt.legend(title=\"survived\", labels=['No', 'Yes'])\nplt.title(\"Survived or not based on fare\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:09.048953Z","iopub.execute_input":"2022-07-29T10:19:09.049317Z","iopub.status.idle":"2022-07-29T10:19:09.331022Z","shell.execute_reply.started":"2022-07-29T10:19:09.049286Z","shell.execute_reply":"2022-07-29T10:19:09.329775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.countplot(data=train_df, x=\"SibSp\", hue=\"Survived\")\nplt.legend(title=\"Survived or not\", labels=['No', 'Yes'], loc=\"upper right\")\nplt.title(\"Survived or not based on SibSp\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:09.333914Z","iopub.execute_input":"2022-07-29T10:19:09.334353Z","iopub.status.idle":"2022-07-29T10:19:09.615830Z","shell.execute_reply.started":"2022-07-29T10:19:09.334311Z","shell.execute_reply":"2022-07-29T10:19:09.614767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.countplot(data=train_df, x=\"Parch\", hue=\"Survived\")\nplt.legend(title=\"Survived or not\", labels=['No', 'Yes'], loc=\"upper right\")\nplt.title(\"Survived or not based on Parch\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:09.617850Z","iopub.execute_input":"2022-07-29T10:19:09.618321Z","iopub.status.idle":"2022-07-29T10:19:09.857949Z","shell.execute_reply.started":"2022-07-29T10:19:09.618276Z","shell.execute_reply":"2022-07-29T10:19:09.856570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 8))\n\nsns.countplot(data=train_df, x=\"Embarked\", hue=\"Survived\")\nplt.legend(title=\"Survived or not\", labels=['No', 'Yes'], loc=\"upper right\")\nplt.title(\"Survived or not based on Embarked\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:09.859327Z","iopub.execute_input":"2022-07-29T10:19:09.859676Z","iopub.status.idle":"2022-07-29T10:19:10.116086Z","shell.execute_reply.started":"2022-07-29T10:19:09.859645Z","shell.execute_reply":"2022-07-29T10:19:10.114628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"EDA results:\n- Most female was rescued\n- People who paid more for the fare were more likely to be saved\n- People who were in a better class had a higher chance of being saved\n- People who had 1 sibling had 50% of survival","metadata":{}},{"cell_type":"code","source":"def clean_data(df):\n    df.drop(['PassengerId', 'Name', 'Ticket', 'Cabin'], inplace=True, axis=1)\n    df['Age'] = pd.cut(df['Age'], bins=[0,12,20,40,120], labels=['Children', 'Teenage', 'Adult', 'Elder'])\n    df['Fare'] = pd.cut(df['Fare'], bins=[0,7.91,14.45,31,120], labels=['Low_fare', 'median_fare', 'Average_fare', 'high_fare'])\n    df = pd.get_dummies(df, columns = [\"Sex\", \"Age\", \"Embarked\", \"Fare\", \"Pclass\"])\n    df.drop(['SibSp', 'Parch'], inplace=True, axis=1)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.118309Z","iopub.execute_input":"2022-07-29T10:19:10.118654Z","iopub.status.idle":"2022-07-29T10:19:10.127374Z","shell.execute_reply.started":"2022-07-29T10:19:10.118600Z","shell.execute_reply":"2022-07-29T10:19:10.126198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = clean_data(train_df)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.128713Z","iopub.execute_input":"2022-07-29T10:19:10.129033Z","iopub.status.idle":"2022-07-29T10:19:10.165859Z","shell.execute_reply.started":"2022-07-29T10:19:10.129005Z","shell.execute_reply":"2022-07-29T10:19:10.164933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = train_df['Survived'].to_numpy()\ntrain_df.drop(['Survived'], inplace=True, axis=1)\nX = train_df.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.166915Z","iopub.execute_input":"2022-07-29T10:19:10.167794Z","iopub.status.idle":"2022-07-29T10:19:10.174525Z","shell.execute_reply.started":"2022-07-29T10:19:10.167760Z","shell.execute_reply":"2022-07-29T10:19:10.173701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(Y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.176149Z","iopub.execute_input":"2022-07-29T10:19:10.176864Z","iopub.status.idle":"2022-07-29T10:19:10.188944Z","shell.execute_reply.started":"2022-07-29T10:19:10.176820Z","shell.execute_reply":"2022-07-29T10:19:10.187444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.190450Z","iopub.execute_input":"2022-07-29T10:19:10.190902Z","iopub.status.idle":"2022-07-29T10:19:10.578203Z","shell.execute_reply.started":"2022-07-29T10:19:10.190870Z","shell.execute_reply":"2022-07-29T10:19:10.576959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.579941Z","iopub.execute_input":"2022-07-29T10:19:10.580415Z","iopub.status.idle":"2022-07-29T10:19:10.589102Z","shell.execute_reply.started":"2022-07-29T10:19:10.580366Z","shell.execute_reply":"2022-07-29T10:19:10.587752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=800,\n                             min_samples_split=12,\n                             max_features='auto',oob_score=True,\n                             random_state=1,n_jobs=-1)\nmodel.fit(X_train, Y_train)\nY_train_pred = model.predict(X_train)\nY_val_pred = model.predict(X_val)\nprint(f\"Train accuracy is {accuracy_score(Y_train, Y_train_pred)}\")\nprint(f\"Validation accuracy is {accuracy_score(Y_val, Y_val_pred)}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:10.590258Z","iopub.execute_input":"2022-07-29T10:19:10.590580Z","iopub.status.idle":"2022-07-29T10:19:13.960703Z","shell.execute_reply.started":"2022-07-29T10:19:10.590551Z","shell.execute_reply":"2022-07-29T10:19:13.959185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(confusion_matrix(Y, np.concatenate([Y_train_pred, Y_val_pred], axis=0)),annot=True,fmt='3.0f',cmap=\"Accent_r\")\nplt.xlabel(\"ŷ (predicted)\")\nplt.ylabel(\"y (ground true)\")\nplt.title('Confusion Matrix', y=1, size=15);","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:13.962407Z","iopub.execute_input":"2022-07-29T10:19:13.963220Z","iopub.status.idle":"2022-07-29T10:19:14.203131Z","shell.execute_reply.started":"2022-07-29T10:19:13.963168Z","shell.execute_reply":"2022-07-29T10:19:14.201699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('../input/titanic/test.csv')\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:14.204814Z","iopub.execute_input":"2022-07-29T10:19:14.205293Z","iopub.status.idle":"2022-07-29T10:19:14.243075Z","shell.execute_reply.started":"2022-07-29T10:19:14.205254Z","shell.execute_reply":"2022-07-29T10:19:14.241807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = clean_data(test_df)\nX_test = test_df.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:14.244715Z","iopub.execute_input":"2022-07-29T10:19:14.245096Z","iopub.status.idle":"2022-07-29T10:19:14.263782Z","shell.execute_reply.started":"2022-07-29T10:19:14.245054Z","shell.execute_reply":"2022-07-29T10:19:14.262536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:14.265333Z","iopub.execute_input":"2022-07-29T10:19:14.265751Z","iopub.status.idle":"2022-07-29T10:19:14.794180Z","shell.execute_reply.started":"2022-07-29T10:19:14.265718Z","shell.execute_reply":"2022-07-29T10:19:14.792738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:14.796347Z","iopub.execute_input":"2022-07-29T10:19:14.796927Z","iopub.status.idle":"2022-07-29T10:19:14.808456Z","shell.execute_reply.started":"2022-07-29T10:19:14.796868Z","shell.execute_reply":"2022-07-29T10:19:14.807040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"solution = pd.DataFrame({'PassengerId': range(892, 1310), 'Survived': Y_test})\nsolution.to_csv('submission.csv', sep=',', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T10:19:14.814862Z","iopub.execute_input":"2022-07-29T10:19:14.815286Z","iopub.status.idle":"2022-07-29T10:19:14.828009Z","shell.execute_reply.started":"2022-07-29T10:19:14.815251Z","shell.execute_reply":"2022-07-29T10:19:14.826526Z"},"trusted":true},"execution_count":null,"outputs":[]}]}