{"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":"import numpy as np \nimport pandas as pd \n\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T12:31:05.482070Z","iopub.execute_input":"2022-07-25T12:31:05.482906Z","iopub.status.idle":"2022-07-25T12:31:05.488429Z","shell.execute_reply.started":"2022-07-25T12:31:05.482857Z","shell.execute_reply":"2022-07-25T12:31:05.487179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/titanic/train.csv')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.116411Z","iopub.status.idle":"2022-07-25T12:28:24.117070Z","shell.execute_reply.started":"2022-07-25T12:28:24.116725Z","shell.execute_reply":"2022-07-25T12:28:24.116753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('../input/titanic/test.csv')\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.119761Z","iopub.status.idle":"2022-07-25T12:28:24.121044Z","shell.execute_reply.started":"2022-07-25T12:28:24.120672Z","shell.execute_reply":"2022-07-25T12:28:24.120704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.122456Z","iopub.status.idle":"2022-07-25T12:28:24.123470Z","shell.execute_reply.started":"2022-07-25T12:28:24.123093Z","shell.execute_reply":"2022-07-25T12:28:24.123126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.128885Z","iopub.execute_input":"2022-07-25T12:28:24.129252Z","iopub.status.idle":"2022-07-25T12:28:24.135846Z","shell.execute_reply.started":"2022-07-25T12:28:24.129223Z","shell.execute_reply":"2022-07-25T12:28:24.134924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"women = train_data.loc[train_data.Sex == 'female'][\"Survived\"]\nrate_women = (sum(women)/len(women))*100\n\nprint(\"% of women who survived:\", rate_women)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.146170Z","iopub.execute_input":"2022-07-25T12:28:24.146769Z","iopub.status.idle":"2022-07-25T12:28:24.155082Z","shell.execute_reply.started":"2022-07-25T12:28:24.146737Z","shell.execute_reply":"2022-07-25T12:28:24.153902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(women)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.168696Z","iopub.execute_input":"2022-07-25T12:28:24.169708Z","iopub.status.idle":"2022-07-25T12:28:24.176823Z","shell.execute_reply.started":"2022-07-25T12:28:24.169660Z","shell.execute_reply":"2022-07-25T12:28:24.175877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"men = train_data.loc[train_data.Sex == 'male'][\"Survived\"]\nrate_men = (sum(men)/len(men))*100\n\nprint(\"% of women who survived:\", rate_men)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.189831Z","iopub.execute_input":"2022-07-25T12:28:24.191032Z","iopub.status.idle":"2022-07-25T12:28:24.199844Z","shell.execute_reply.started":"2022-07-25T12:28:24.190971Z","shell.execute_reply":"2022-07-25T12:28:24.198892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_data[\"Survived\"]\n\nfeatures = [\"Pclass\", \"Sex\", \"SibSp\", \"Parch\"]\nX = pd.get_dummies(train_data[features])\nX_test = pd.get_dummies(test_data[features])\n\nmodel = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)\nmodel.fit(X, y)\npredictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.209329Z","iopub.execute_input":"2022-07-25T12:28:24.210331Z","iopub.status.idle":"2022-07-25T12:28:24.436088Z","shell.execute_reply.started":"2022-07-25T12:28:24.210287Z","shell.execute_reply":"2022-07-25T12:28:24.434877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})\noutput.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T12:28:24.437977Z","iopub.execute_input":"2022-07-25T12:28:24.438331Z","iopub.status.idle":"2022-07-25T12:28:24.445662Z","shell.execute_reply.started":"2022-07-25T12:28:24.438300Z","shell.execute_reply":"2022-07-25T12:28:24.444855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}