{"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)\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.impute import SimpleImputer\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-28T15:26:19.785825Z","iopub.execute_input":"2022-07-28T15:26:19.786342Z","iopub.status.idle":"2022-07-28T15:26:21.440956Z","shell.execute_reply.started":"2022-07-28T15:26:19.786232Z","shell.execute_reply":"2022-07-28T15:26:21.440045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data =  pd.read_csv('/kaggle/input/titanic/train.csv')\ntest =  pd.read_csv('/kaggle/input/titanic/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:26:28.012602Z","iopub.execute_input":"2022-07-28T15:26:28.013036Z","iopub.status.idle":"2022-07-28T15:26:28.043014Z","shell.execute_reply.started":"2022-07-28T15:26:28.013001Z","shell.execute_reply":"2022-07-28T15:26:28.041753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['Sex'] = data['Sex'].replace(['female'], 1)\ndata['Sex'] = data['Sex'].replace(['male'], 0)\ndata['Embarked'] = data['Embarked'].replace(['S'], 0)\ndata['Embarked'] = data['Embarked'].replace(['Q'], 1)\ndata['Embarked'] = data['Embarked'].replace(['C'], 2)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:26:30.303572Z","iopub.execute_input":"2022-07-28T15:26:30.304003Z","iopub.status.idle":"2022-07-28T15:26:30.350399Z","shell.execute_reply.started":"2022-07-28T15:26:30.303952Z","shell.execute_reply":"2022-07-28T15:26:30.349522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data1 = data.drop(['Cabin'], axis=1)\ndata1 = data1.dropna()\ndata1.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:26:32.661721Z","iopub.execute_input":"2022-07-28T15:26:32.662533Z","iopub.status.idle":"2022-07-28T15:26:32.682821Z","shell.execute_reply.started":"2022-07-28T15:26:32.662486Z","shell.execute_reply":"2022-07-28T15:26:32.681687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y =data1['Survived']\nX = data1.drop(['PassengerId','Survived','Name', 'Ticket'], axis=1)\nX.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:26:34.030115Z","iopub.execute_input":"2022-07-28T15:26:34.030686Z","iopub.status.idle":"2022-07-28T15:26:34.053229Z","shell.execute_reply.started":"2022-07-28T15:26:34.030624Z","shell.execute_reply":"2022-07-28T15:26:34.051698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X,y, random_state=42)\nforest = RandomForestClassifier(n_estimators = 70, max_features=3,  max_depth=5, random_state=42)\nforest.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:27:56.674856Z","iopub.execute_input":"2022-07-28T15:27:56.675302Z","iopub.status.idle":"2022-07-28T15:27:56.835689Z","shell.execute_reply.started":"2022-07-28T15:27:56.675260Z","shell.execute_reply":"2022-07-28T15:27:56.834274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(forest.score(X_train, y_train))\nforest.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:27:58.269272Z","iopub.execute_input":"2022-07-28T15:27:58.269692Z","iopub.status.idle":"2022-07-28T15:27:58.309805Z","shell.execute_reply.started":"2022-07-28T15:27:58.269658Z","shell.execute_reply":"2022-07-28T15:27:58.308310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['Sex'] = test['Sex'].replace(['female'], 1)\ntest['Sex'] = test['Sex'].replace(['male'], 0)\ntest['Embarked'] = test['Embarked'].replace(['S'], 0)\ntest['Embarked'] = test['Embarked'].replace(['Q'], 1)\ntest['Embarked'] = test['Embarked'].replace(['C'], 2)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:28:01.012286Z","iopub.execute_input":"2022-07-28T15:28:01.012682Z","iopub.status.idle":"2022-07-28T15:28:01.026349Z","shell.execute_reply.started":"2022-07-28T15:28:01.012653Z","shell.execute_reply":"2022-07-28T15:28:01.025273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1 = test.drop(['Cabin'], axis=1)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:33:35.846174Z","iopub.execute_input":"2022-07-28T15:33:35.846582Z","iopub.status.idle":"2022-07-28T15:33:35.853244Z","shell.execute_reply.started":"2022-07-28T15:33:35.846547Z","shell.execute_reply":"2022-07-28T15:33:35.852111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testx = test1.drop(['PassengerId','Name', 'Ticket'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:33:39.579486Z","iopub.execute_input":"2022-07-28T15:33:39.579870Z","iopub.status.idle":"2022-07-28T15:33:39.587312Z","shell.execute_reply.started":"2022-07-28T15:33:39.579840Z","shell.execute_reply":"2022-07-28T15:33:39.585608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_imputer = SimpleImputer()\ntestxx = my_imputer.fit_transform(testx)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:34:15.174048Z","iopub.execute_input":"2022-07-28T15:34:15.175764Z","iopub.status.idle":"2022-07-28T15:34:15.197787Z","shell.execute_reply.started":"2022-07-28T15:34:15.175699Z","shell.execute_reply":"2022-07-28T15:34:15.193371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = forest.predict(testxx)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:34:20.766781Z","iopub.execute_input":"2022-07-28T15:34:20.767453Z","iopub.status.idle":"2022-07-28T15:34:20.793959Z","shell.execute_reply.started":"2022-07-28T15:34:20.767398Z","shell.execute_reply":"2022-07-28T15:34:20.792677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'PassengerId': test1.PassengerId, 'Survived': predictions})\n# you could use any filename. We choose submission here\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T15:34:40.652320Z","iopub.execute_input":"2022-07-28T15:34:40.652732Z","iopub.status.idle":"2022-07-28T15:34:40.663795Z","shell.execute_reply.started":"2022-07-28T15:34:40.652700Z","shell.execute_reply":"2022-07-28T15:34:40.662819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}